From d8471f53ce18d36607420fd65eac0a4c87dea567 Mon Sep 17 00:00:00 2001 From: Kees Kluskens Date: Tue, 7 Aug 2018 23:30:20 +0200 Subject: [PATCH 1/2] Hardcode modules: false for only babel preset env See #521 for the reasoning. I am not happy at all with the implementation, but it works. Let me know if there is a better way I missed. Fixes #521, #477, #485 --- src/index.js | 22 ++++++++++++ test/loader.test.js | 84 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 87 insertions(+), 19 deletions(-) diff --git a/src/index.js b/src/index.js index 353fd4c8..8246e5a6 100644 --- a/src/index.js +++ b/src/index.js @@ -85,6 +85,28 @@ async function loader(source, inputSourceMap, overrides) { ); } + if (this.version > 1 && programmaticOptions.presets) { + programmaticOptions.presets.forEach((preset, i) => { + let name = preset; + let opts = {}; + if (Array.isArray(preset)) { + name = preset[0]; + opts = preset[1]; + } + if ( + ["env", "babel-preset-env", "@babel/preset-env"].includes(name) && + (!opts || (opts && !opts.hasOwnProperty("modules"))) + ) { + const newOpts = { modules: false }; + if (Array.isArray(preset)) { + preset[1] = Object.assign({}, opts, newOpts); + } else { + programmaticOptions.presets[i] = [name, newOpts]; + } + } + }); + } + const config = babel.loadPartialConfig(programmaticOptions); if (config) { let options = config.options; diff --git a/test/loader.test.js b/test/loader.test.js index db08b8a8..0047ae38 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -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 @@ -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); @@ -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: { @@ -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: { @@ -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); +}); From 3b21637bc13730738333d613cee0689e5d9acba1 Mon Sep 17 00:00:00 2001 From: Kees Kluskens Date: Tue, 7 Aug 2018 23:41:23 +0200 Subject: [PATCH 2/2] Playing code gulf --- src/index.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 8246e5a6..ae3aaea5 100644 --- a/src/index.js +++ b/src/index.js @@ -87,18 +87,15 @@ async function loader(source, inputSourceMap, overrides) { if (this.version > 1 && programmaticOptions.presets) { programmaticOptions.presets.forEach((preset, i) => { - let name = preset; - let opts = {}; - if (Array.isArray(preset)) { - name = preset[0]; - opts = preset[1]; - } + 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 (Array.isArray(preset)) { + if (presetIsArray) { preset[1] = Object.assign({}, opts, newOpts); } else { programmaticOptions.presets[i] = [name, newOpts];