Skip to content

Commit d8cff97

Browse files
fix: use async config loading if available (#825)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
1 parent 989103c commit d8cff97

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
matrix:
2222
os: [ubuntu-latest, windows-latest]
23-
node-version: [10.x, 12.x, 14.x]
23+
node-version: [10.x, 12.x, 14.x, 15.x]
2424
webpack-version: [latest, next]
2525
include:
2626
- node: 14.x

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"react-intl": "^3.3.2",
4343
"react-intl-webpack-plugin": "^0.3.0",
4444
"rimraf": "^3.0.0",
45+
"semver": "7.0.0",
4546
"webpack": "^4.0.0"
4647
},
4748
"scripts": {

src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ async function loader(source, inputSourceMap, overrides) {
159159
);
160160
}
161161

162-
const config = babel.loadPartialConfig(
162+
// babel.loadPartialConfigAsync is available in v7.8.0+
163+
const { loadPartialConfigAsync = babel.loadPartialConfig } = babel;
164+
const config = await loadPartialConfigAsync(
163165
injectCaller(programmaticOptions, this.target),
164166
);
165167
if (config) {

test/fixtures/babelrc.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
presets: ["@babel/preset-env"]
3+
};

test/loader.test.js

+54
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from "ava";
22
import fs from "fs";
33
import path from "path";
44
import rimraf from "rimraf";
5+
import { satisfies } from "semver";
56
import webpack from "webpack";
67
import createTestDirectory from "./helpers/createTestDirectory";
78

@@ -127,3 +128,56 @@ test.cb(
127128
});
128129
},
129130
);
131+
132+
test.cb("should load ESM config files", t => {
133+
const config = Object.assign({}, globalConfig, {
134+
entry: path.join(__dirname, "fixtures/constant.js"),
135+
output: {
136+
path: t.context.directory,
137+
},
138+
module: {
139+
rules: [
140+
{
141+
test: /\.js$/,
142+
loader: babelLoader,
143+
exclude: /node_modules/,
144+
options: {
145+
// Use relative path starting with a dot to satisfy module loader.
146+
// https://github.com/nodejs/node/issues/31710
147+
// File urls doesn't work with current resolve@1.12.0 package.
148+
extends: (
149+
"." +
150+
path.sep +
151+
path.relative(
152+
process.cwd(),
153+
path.resolve(__dirname, "fixtures/babelrc.mjs"),
154+
)
155+
).replace(/\\/g, "/"),
156+
babelrc: false,
157+
},
158+
},
159+
],
160+
},
161+
});
162+
163+
webpack(config, (err, stats) => {
164+
t.is(err, null);
165+
// Node supports ESM without a flag starting from 12.13.0 and 13.2.0.
166+
if (satisfies(process.version, `^12.13.0 || >=13.2.0`)) {
167+
t.deepEqual(
168+
stats.compilation.errors.map(e => e.message),
169+
[],
170+
);
171+
} else {
172+
t.is(stats.compilation.errors.length, 1);
173+
const moduleBuildError = stats.compilation.errors[0];
174+
const babelLoaderError = moduleBuildError.error;
175+
t.true(babelLoaderError instanceof Error);
176+
// Error messages are slightly different between versions:
177+
// "modules aren't supported" or "modules not supported".
178+
t.regex(babelLoaderError.message, /supported/i);
179+
}
180+
t.is(stats.compilation.warnings.length, 0);
181+
t.end();
182+
});
183+
});

0 commit comments

Comments
 (0)