Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use async config loading if available #825

Merged
merged 4 commits into from Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, 14.x, 15.x]
webpack-version: [latest, next]
include:
- node: 14.x
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -42,6 +42,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
4 changes: 3 additions & 1 deletion src/index.js
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/babelrc.mjs
@@ -0,0 +1,3 @@
export default {
presets: ["@babel/preset-env"]
};
54 changes: 54 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 { satisfies } from "semver";
import webpack from "webpack";
import createTestDirectory from "./helpers/createTestDirectory";

Expand Down Expand Up @@ -127,3 +128,56 @@ test.cb(
});
},
);

test.cb("should load ESM config files", t => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we differentiate this test based on process.version?

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it, but I don't know if Node team will backport ESM to 12 or even 10 in the future and this test begin to fail. But okay, I'll redo it with semver check of Node version. As I remember 13.3 was the first one without --experimental flag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's only test in on 13.3 for now. If they'll backport it we can update the test.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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 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),
[],
);
} 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();
});
});