Skip to content

Commit

Permalink
fix: use async config loading if available
Browse files Browse the repository at this point in the history
  • Loading branch information
the-spyke authored and nicolo-ribaudo committed Nov 8, 2020
1 parent a92949a commit 5152606
Show file tree
Hide file tree
Showing 7 changed files with 771 additions and 375 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -5,6 +5,7 @@
"files": [
"lib"
],
"type": "commonjs",
"main": "lib/index.js",
"engines": {
"node": ">= 6.9"
Expand All @@ -21,9 +22,9 @@
"webpack": ">=2"
},
"devDependencies": {
"@babel/cli": "^7.2.0",
"@babel/core": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/cli": "^7.8.0",
"@babel/core": "^7.8.0",
"@babel/preset-env": "^7.8.0",
"ava": "^2.4.0",
"babel-eslint": "^10.0.1",
"babel-plugin-istanbul": "^5.1.0",
Expand All @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Expand Up @@ -158,9 +158,9 @@ async function loader(source, inputSourceMap, overrides) {
);
}

const config = babel.loadPartialConfig(
injectCaller(programmaticOptions, this.target),
);
// babel.loadPartialConfigAsync is available in v7.8.0+
const config = await (babel.loadPartialConfigAsync ||
babel.loadPartialConfig)(injectCaller(programmaticOptions, this.target));
if (config) {
let options = config.options;
if (overrides && overrides.config) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/babelrc.mjs
@@ -0,0 +1,3 @@
export default {
presets: ["@babel/preset-env"]
};
51 changes: 51 additions & 0 deletions test/loader.test.js
Expand Up @@ -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";

Expand Down Expand Up @@ -127,3 +128,53 @@ 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();
});
});
12 changes: 7 additions & 5 deletions test/sourcemaps.test.js
Expand Up @@ -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();
Expand Down

0 comments on commit 5152606

Please sign in to comment.