Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ async function loader(source, inputSourceMap, overrides) {
);
}

if (this.version > 1 && programmaticOptions.presets) {
programmaticOptions.presets.forEach((preset, i) => {
const presetIsArray = Array.isArray(preset);
const name = presetIsArray ? preset[0] : preset;
const opts = presetIsArray ? preset[1] : {};
if (
["env", "babel-preset-env", "@babel/preset-env"].includes(name) &&
(!opts || (opts && !opts.hasOwnProperty("modules")))
) {
const newOpts = { modules: false };
if (presetIsArray) {
preset[1] = Object.assign({}, opts, newOpts);
} else {
programmaticOptions.presets[i] = [name, newOpts];
}
}
});
}

const config = babel.loadPartialConfig(programmaticOptions);
if (config) {
let options = config.options;
Expand Down
84 changes: 65 additions & 19 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,34 @@ import createTestDirectory from "./helpers/createTestDirectory";

const outputDir = path.join(__dirname, "output/loader");
const babelLoader = path.join(__dirname, "../lib");
const globalConfig = {
mode: "development",
entry: path.join(__dirname, "fixtures/basic.js"),
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
options: {
presets: ["@babel/preset-env"],

const generateConfigWithPresets = (presets, context = false) => {
const globalConfig = {
mode: "development",
entry: path.join(__dirname, "fixtures/basic.js"),
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
options: {
presets,
},
exclude: /node_modules/,
},
exclude: /node_modules/,
},
],
},
],
},
};

if (!context) {
return globalConfig;
}

return Object.assign(globalConfig, {
output: {
path: context.directory,
},
});
};

// Create a separate directory for each test so that the tests
Expand All @@ -37,11 +50,7 @@ test.cb.beforeEach(t => {
test.cb.afterEach(t => rimraf(t.context.directory, t.end));

test.cb("should transpile the code snippet", t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
});
const config = generateConfigWithPresets(["@babel/env"], t.context);

webpack(config, (err, stats) => {
t.is(err, null);
Expand All @@ -65,6 +74,7 @@ test.cb("should transpile the code snippet", t => {
});

test.cb("should not throw error on syntax error", t => {
const globalConfig = generateConfigWithPresets(["@babel/env"], t.context);
const config = Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/syntax.js"),
output: {
Expand Down Expand Up @@ -111,6 +121,7 @@ test.cb("should not throw without config", t => {
test.cb(
"should return compilation errors with the message included in the stack trace",
t => {
const globalConfig = generateConfigWithPresets(["@babel/env"], t.context);
const config = Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/syntax.js"),
output: {
Expand All @@ -127,3 +138,38 @@ test.cb(
});
},
);

test.cb("should not disable modules option when it is explicitly set", t => {
const configs = [
generateConfigWithPresets([["env", { modules: true }]], t.context),
generateConfigWithPresets([["env", { modules: "amd" }]], t.context),
];

const callback = err => {
t.is(err, null);
multiCompiler.compilers.forEach(compiler => {
t.truthy(compiler.options.module.rules[0].options.presets[0][1].modules);
});
t.end();
};
const multiCompiler = webpack(configs, callback);
});

test.cb("should disable modules option when it is not set", t => {
const configs = [
generateConfigWithPresets([["env"]], t.context),
generateConfigWithPresets(["env"], t.context),
generateConfigWithPresets([["env", { modules: false }]], t.context),
generateConfigWithPresets([["@babel/preset-env"]], t.context),
generateConfigWithPresets([["babel-preset-env"]], t.context),
];

const callback = err => {
t.is(err, null);
multiCompiler.compilers.forEach(compiler => {
t.false(compiler.options.module.rules[0].options.presets[0][1].modules);
});
t.end();
};
const multiCompiler = webpack(configs, callback);
});