Skip to content

Commit

Permalink
feat: Added warning when modules is not false
Browse files Browse the repository at this point in the history
When webpack is version 2 or higher and we're using es2015 or env preset
We throw a warning saying that webpack can't treeshake correctly

Closes #477
  • Loading branch information
wardpeet committed Jun 27, 2017
1 parent 0815f48 commit cdb8bf2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const resolveRc = require("./resolve-rc.js");
const pkg = require("../package.json");
const fs = require("fs");

const presetsToCheckForModules = ["es2015", "env"];
const modulesWarningMsg = `We've noticted the option modules isn't set to false,
which disables treeshaking.`;

/**
* Error thrown by Babel formatted to conform to Webpack reporting.
*/
Expand Down Expand Up @@ -149,6 +153,28 @@ module.exports = function(source, inputSourceMap) {
options.sourceFileName = relative(options.sourceRoot, options.filename);
}

if (this.version > 1 && options.presets) {
options.presets.forEach(preset => {
let name = preset;
let opts = {};

// if we have extra options put it in opts
if (Array.isArray(preset)) {
name = preset[0];
opts = preset[1];
}

if (
presetsToCheckForModules.indexOf(name) > -1 &&
(!opts ||
(opts && !opts.hasOwnProperty("modules")) ||
(opts && opts.modules))
) {
this.emitWarning(new Error(modulesWarningMsg));
}
});
}

const cacheDirectory = options.cacheDirectory;
const cacheIdentifier = options.cacheIdentifier;
const metadataSubscribers = options.metadataSubscribers;
Expand Down
36 changes: 36 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,39 @@ test.cb("should not throw without config", t => {
t.end();
});
});

test.cb("should throw a warning when modules is set to false", t => {
const config = {
entry: path.join(__dirname, "fixtures/basic.js"),
output: {
path: t.context.directory,
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
query: {
presets: [["env", { modules: "umd" }]],
},
},
],
},
};

webpack(config, (err, stats) => {
t.is(err, null);

const warnings = stats.compilation.warnings;
t.true(warnings.length > 0);
if (warnings.length) {
t.is(
warnings[0].message,
"We've noticted the option modules isn't set to false,\nwhich disables treeshaking.",
);
}

t.end();
});
});

0 comments on commit cdb8bf2

Please sign in to comment.