From 2385e817525164bfb283b07863d3555e46770356 Mon Sep 17 00:00:00 2001 From: Anton Alexandrenok Date: Thu, 27 Feb 2020 16:01:49 +0300 Subject: [PATCH 1/4] fix: use async config loading if available --- .github/workflows/ci.yml | 2 +- package.json | 2 ++ src/index.js | 4 ++- test/fixtures/babelrc.mjs | 3 +++ test/loader.test.js | 54 +++++++++++++++++++++++++++++++++++++++ test/sourcemaps.test.js | 12 +++++---- 6 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/babelrc.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a7bebb1..e6abd527 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest] - node-version: [10.x, 12.x, 14.x] + node-version: [10.x, 12.x, 13.x, 14.x, 15.x] webpack-version: [latest, next] include: - node: 14.x diff --git a/package.json b/package.json index 57a5d97b..d3412442 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "files": [ "lib" ], + "type": "commonjs", "main": "lib/index.js", "engines": { "node": ">= 6.9" @@ -42,6 +43,7 @@ "react-intl": "^3.3.2", "react-intl-webpack-plugin": "^0.3.0", "rimraf": "^3.0.0", + "semver": "7.0.0", "webpack": "^4.0.0" }, "scripts": { diff --git a/src/index.js b/src/index.js index 09458864..a5ddcdc4 100644 --- a/src/index.js +++ b/src/index.js @@ -159,7 +159,9 @@ async function loader(source, inputSourceMap, overrides) { ); } - const config = babel.loadPartialConfig( + // babel.loadPartialConfigAsync is available in v7.8.0+ + const { loadPartialConfigAsync = babel.loadPartialConfig } = babel; + const config = await loadPartialConfigAsync( injectCaller(programmaticOptions, this.target), ); if (config) { diff --git a/test/fixtures/babelrc.mjs b/test/fixtures/babelrc.mjs new file mode 100644 index 00000000..d117b0df --- /dev/null +++ b/test/fixtures/babelrc.mjs @@ -0,0 +1,3 @@ +export default { + presets: ["@babel/preset-env"] +}; diff --git a/test/loader.test.js b/test/loader.test.js index e42567b7..94029748 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -2,6 +2,7 @@ import test from "ava"; import fs from "fs"; import path from "path"; import rimraf from "rimraf"; +import { gte } from "semver"; import webpack from "webpack"; import createTestDirectory from "./helpers/createTestDirectory"; @@ -127,3 +128,56 @@ test.cb( }); }, ); + +test.cb("should load ESM config files", t => { + const config = Object.assign({}, globalConfig, { + entry: path.join(__dirname, "fixtures/constant.js"), + output: { + path: t.context.directory, + }, + module: { + rules: [ + { + test: /\.js$/, + loader: babelLoader, + exclude: /node_modules/, + options: { + // Use relative path starting with a dot to satisfy module loader. + // https://github.com/nodejs/node/issues/31710 + // File urls doesn't work with current resolve@1.12.0 package. + extends: ( + "." + + path.sep + + path.relative( + process.cwd(), + path.resolve(__dirname, "fixtures/babelrc.mjs"), + ) + ).replace(/\\/g, "/"), + babelrc: false, + }, + }, + ], + }, + }); + + webpack(config, (err, stats) => { + t.is(err, null); + // Node supports ESM without a flag starting from 13.2.0. + if (gte(process.version, `13.2.0`)) { + t.deepEqual( + stats.compilation.errors.map(e => e.message), + [], + ); + } else { + t.is(stats.compilation.errors.length, 1); + const moduleBuildError = stats.compilation.errors[0]; + const babelLoaderError = moduleBuildError.error; + t.true(babelLoaderError instanceof Error); + // Error messages are slightly different between versions: + // "modules aren't supported" or "modules not supported". + t.regex(babelLoaderError.message, /supported/i); + } + t.is(stats.compilation.warnings.length, 0); + t.end(); + }); +}); diff --git a/test/sourcemaps.test.js b/test/sourcemaps.test.js index 0230f274..17a0cc64 100644 --- a/test/sourcemaps.test.js +++ b/test/sourcemaps.test.js @@ -171,11 +171,13 @@ test.cb("should output webpack's devtoolModuleFilename option", t => { t.is(err, null); // The full absolute path is included in the sourcemap properly - t.not( - data - .toString() - .indexOf(JSON.stringify(`==${globalConfig.entry}==`)), - -1, + t.regex( + data.toString(), + new RegExp( + // Forward slashes on Windows are common in Babel/Webpack: + // https://github.com/webpack/webpack/pull/1909 + JSON.stringify(`==${globalConfig.entry.replace(/\\/g, "/")}==`), + ), ); t.end(); From a20ba70e651db027be12b208ea23b22a56d7f867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 8 Nov 2020 23:43:14 +0100 Subject: [PATCH 2/4] Remove `"type": "commonjs"` --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index d3412442..80bec2b6 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,6 @@ "files": [ "lib" ], - "type": "commonjs", "main": "lib/index.js", "engines": { "node": ">= 6.9" From 2fecfbb866208f009c6d30d6874164124f2a0661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 8 Nov 2020 23:49:29 +0100 Subject: [PATCH 3/4] Update tests --- test/loader.test.js | 6 +++--- test/sourcemaps.test.js | 12 +++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/test/loader.test.js b/test/loader.test.js index 94029748..370d4dc5 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -2,7 +2,7 @@ import test from "ava"; import fs from "fs"; import path from "path"; import rimraf from "rimraf"; -import { gte } from "semver"; +import { satisfies } from "semver"; import webpack from "webpack"; import createTestDirectory from "./helpers/createTestDirectory"; @@ -162,8 +162,8 @@ test.cb("should load ESM config files", t => { webpack(config, (err, stats) => { t.is(err, null); - // Node supports ESM without a flag starting from 13.2.0. - if (gte(process.version, `13.2.0`)) { + // Node supports ESM without a flag starting from 12.13.0 and 13.2.0. + if (satisfies(process.version, `^12.13.0 || >=13.2.0`)) { t.deepEqual( stats.compilation.errors.map(e => e.message), [], diff --git a/test/sourcemaps.test.js b/test/sourcemaps.test.js index 17a0cc64..0230f274 100644 --- a/test/sourcemaps.test.js +++ b/test/sourcemaps.test.js @@ -171,13 +171,11 @@ test.cb("should output webpack's devtoolModuleFilename option", t => { t.is(err, null); // The full absolute path is included in the sourcemap properly - t.regex( - data.toString(), - new RegExp( - // Forward slashes on Windows are common in Babel/Webpack: - // https://github.com/webpack/webpack/pull/1909 - JSON.stringify(`==${globalConfig.entry.replace(/\\/g, "/")}==`), - ), + t.not( + data + .toString() + .indexOf(JSON.stringify(`==${globalConfig.entry}==`)), + -1, ); t.end(); From 233d389b0486753c9297bf7e1789f4b95f258b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 8 Nov 2020 23:52:03 +0100 Subject: [PATCH 4/4] Don't test on 13 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e6abd527..87b03e25 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest] - node-version: [10.x, 12.x, 13.x, 14.x, 15.x] + node-version: [10.x, 12.x, 14.x, 15.x] webpack-version: [latest, next] include: - node: 14.x