| 1 | function works(test) {
|
|---|
| 2 | try {
|
|---|
| 3 | // Wrap the test in a function to only test the syntax, without executing it
|
|---|
| 4 | (0, eval)(`(() => { ${test} })`);
|
|---|
| 5 | return true;
|
|---|
| 6 | } catch (_error) {
|
|---|
| 7 | return false;
|
|---|
| 8 | }
|
|---|
| 9 | }
|
|---|
| 10 |
|
|---|
| 11 | function getPluginsList(tests) {
|
|---|
| 12 | const plugins = [];
|
|---|
| 13 | for (const [name, cases] of Object.entries(tests)) {
|
|---|
| 14 | if (cases.some(works)) {
|
|---|
| 15 | plugins.push(require.resolve(`@babel/plugin-syntax-${name}`));
|
|---|
| 16 | }
|
|---|
| 17 | }
|
|---|
| 18 | return plugins;
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | const babel7OnlyPlugins = getPluginsList({
|
|---|
| 22 | // ECMAScript 2018
|
|---|
| 23 | "object-rest-spread": ["({ ...{} })", "({ ...x } = {})"], // Babel 7.2.0
|
|---|
| 24 | "async-generators": ["async function* f() {}"], // Babel 7.2.0
|
|---|
| 25 |
|
|---|
| 26 | // ECMAScript 2019
|
|---|
| 27 | "optional-catch-binding": ["try {} catch {}"], // Babel 7.2.0
|
|---|
| 28 | "json-strings": ["'\\u2028'"], // Babel 7.2.0
|
|---|
| 29 |
|
|---|
| 30 | // ECMAScript 2020
|
|---|
| 31 | bigint: ["1n"], // Babel 7.8.0
|
|---|
| 32 | "optional-chaining": ["a?.b"], // Babel 7.9.0
|
|---|
| 33 | "nullish-coalescing-operator": ["a ?? b"], // Babel 7.9.0
|
|---|
| 34 | // import.meta is handled manually
|
|---|
| 35 |
|
|---|
| 36 | // ECMAScript 2021
|
|---|
| 37 | "numeric-separator": ["1_2"],
|
|---|
| 38 | "logical-assignment-operators": ["a ||= b", "a &&= b", "a ??= c"],
|
|---|
| 39 |
|
|---|
| 40 | // ECMAScript 2022
|
|---|
| 41 | "class-properties": [
|
|---|
| 42 | "(class { x = 1 })",
|
|---|
| 43 | "(class { #x = 1 })",
|
|---|
| 44 | "(class { #x() {} })",
|
|---|
| 45 | ],
|
|---|
| 46 | "private-property-in-object": ["(class { #x; m() { #x in y } })"],
|
|---|
| 47 | "class-static-block": ["(class { static {} })"],
|
|---|
| 48 | // top-level await is handled manually
|
|---|
| 49 |
|
|---|
| 50 | // Stage 3
|
|---|
| 51 | // import attributes is handled manually
|
|---|
| 52 | });
|
|---|
| 53 |
|
|---|
| 54 | const commonPlugins = getPluginsList({});
|
|---|
| 55 |
|
|---|
| 56 | // import.meta is only allowed in modules, and modules can only be evaluated
|
|---|
| 57 | // synchronously. For this reason, we cannot detect import.meta support at
|
|---|
| 58 | // runtime. It is supported starting from 10.4, so we can check the version.
|
|---|
| 59 | const major = parseInt(process.versions.node, 10);
|
|---|
| 60 | const minor = parseInt(process.versions.node.match(/^\d+\.(\d+)/)[1], 10);
|
|---|
| 61 | if (major > 10 || (major === 10 && minor >= 4)) {
|
|---|
| 62 | babel7OnlyPlugins.push(require.resolve("@babel/plugin-syntax-import-meta"));
|
|---|
| 63 | }
|
|---|
| 64 | // Same for top level await - it is only supported in modules. It is supported
|
|---|
| 65 | // from 14.3.0
|
|---|
| 66 | if (major > 14 || (major === 14 && minor >= 3)) {
|
|---|
| 67 | babel7OnlyPlugins.push(
|
|---|
| 68 | require.resolve("@babel/plugin-syntax-top-level-await")
|
|---|
| 69 | );
|
|---|
| 70 | }
|
|---|
| 71 | // Similar for import attributes
|
|---|
| 72 | if (
|
|---|
| 73 | major > 20 ||
|
|---|
| 74 | (major === 20 && minor >= 10) ||
|
|---|
| 75 | (major === 18 && minor >= 20)
|
|---|
| 76 | ) {
|
|---|
| 77 | babel7OnlyPlugins.push(
|
|---|
| 78 | require.resolve("@babel/plugin-syntax-import-attributes")
|
|---|
| 79 | );
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | module.exports = ({ version }) => ({
|
|---|
| 83 | plugins: version.startsWith("7.")
|
|---|
| 84 | ? babel7OnlyPlugins.concat(commonPlugins)
|
|---|
| 85 | : commonPlugins,
|
|---|
| 86 | });
|
|---|