Skip to content

Commit

Permalink
Merge branch 'master' into feature/improve-module-concat-bailout-mess…
Browse files Browse the repository at this point in the history
…ages
  • Loading branch information
sokra committed Jul 25, 2017
2 parents 05b913a + 4b12c56 commit 32264b8
Show file tree
Hide file tree
Showing 131 changed files with 2,868 additions and 2,331 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -36,6 +36,7 @@ module.exports = {
"space-before-function-paren": ["error", "never"],
"space-before-blocks": "error",
"object-curly-spacing": ["error", "always"],
"indent": "off",
"keyword-spacing": ["error", {
"after": false,
"overrides": {
Expand Down
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -69,7 +69,6 @@ within webpack itself use this plugin interface. This makes webpack very
|:--:|:----:|:----------|
|[common-chunks-webpack-plugin][common]|![common-npm]|Generates chunks of common modules shared between entry points and splits them into separate bundles (e.g vendor.bundle.js && app.bundle.js)|
|[extract-text-webpack-plugin][extract]|![extract-npm]|Extracts Text (CSS) from your bundles into a separate file (app.bundle.css)|
|[component-webpack-plugin][component]|![component-npm]|Use components with webpack|
|[compression-webpack-plugin][compression]|![compression-npm]|Prepare compressed versions of assets to serve them with Content-Encoding|
|[i18n-webpack-plugin][i18n]|![i18n-npm]|Adds i18n support to your bundles|
|[html-webpack-plugin][html-plugin]|![html-plugin-npm]| Simplifies creation of HTML files (`index.html`) to serve your bundles|
Expand Down Expand Up @@ -191,8 +190,6 @@ or are automatically applied via regex from your webpack configuration.
|<a href="https://github.com/webpack/mocha-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/mocha.svg"></a>|![mocha-npm]|Tests with mocha (Browser/NodeJS)|
|<a href="https://github.com/MoOx/eslint-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/eslint.svg"></a>|![eslint-npm]|PreLoader for linting code using ESLint|
|<a href="https://github.com/webpack/jslint-loader"><img width="48" height="48" src="http://jshint.com/res/jshint-dark.png"></a>|![jshint-npm]|PreLoader for linting code using JSHint|
|<a href="https://github.com/unindented/jscs-loader"><img width="48" height="48" src="https://worldvectorlogo.com/logos/jscs.svg"></a>|![jscs-npm]|PreLoader for code style checking using JSCS|


[mocha-npm]: https://img.shields.io/npm/v/mocha-loader.svg
[eslint-npm]: https://img.shields.io/npm/v/eslint-loader.svg
Expand Down Expand Up @@ -262,6 +259,7 @@ We have also started a series on our [Medium Publication](https://medium.com/web
_Looking to speak about webpack?_ We'd **love** to review your talk abstract/CFP! You can email it to webpack [at] opencollective [dot] com and we can give pointers or tips!!!

<h3 align="center">Creating your own plugins and loaders</h3>

If you create a loader or plugin, we would <3 for you to open source it, and put it on npm. We follow the `x-loader`, `x-webpack-plugin` naming convention.

<h2 align="center">Support</h2>
Expand Down
22 changes: 22 additions & 0 deletions benchmark/createBenchmark.js
@@ -0,0 +1,22 @@
const webpack = require("webpack");
const path = require("path");

webpack({
context: __dirname,
entry: "./createBenchmark/entry.js",
output: {
path: __dirname,
filename: "benchmark-bundle.js"
},
target: "node",
node: {
__dirname: false
},
plugins: [
new webpack.NamedModulesPlugin(),
new webpack.IgnorePlugin(/^(fsevents|uglify-js)$/),
new webpack.NormalModuleReplacementPlugin(/^.\/loadLoader$/, path.resolve(__dirname, "./createBenchmark/loadLoader"))
]
}, function(err, stats) {
console.log(stats.toString());
});
25 changes: 25 additions & 0 deletions benchmark/createBenchmark/entry.js
@@ -0,0 +1,25 @@
const webpack = require("webpack");
const MemoryFs = require("memory-fs");
const path = require("path");

const testCase = process.argv[2];

const config = {
context: __dirname,
entry: `./${testCase}`,
output: {
path: path.resolve(__dirname, "output-" + testCase)
},
devtool: process.argv[3]
};

const compiler = webpack(config);
compiler.run((err, stats) => {
if(err) {
console.error(err);
} else {
console.log(stats.toString({
errorDetails: true
}));
}
});
3 changes: 3 additions & 0 deletions benchmark/createBenchmark/loadLoader.js
@@ -0,0 +1,3 @@
module.exports = (loader, callback) => {
callback(new Error("Loaders are not supported"));
}
70 changes: 70 additions & 0 deletions benchmark/createTestCases.js
@@ -0,0 +1,70 @@
let avgJs = `
const str = "we" + "do" + "some" + "ops";
for(const x of str.split("")) {
if(x.charCodeAt(0) > 40) {
console.log("omg");
} else {
console.log(Math.random() * 2 + 3 * 2);
}
}
// Some comment
switch(a.b.c.d.f.e.g.h.i) {
case true:
break;
case "magic":
throw new Error("Error!");
case 9:
(function() {
// extra scope
var x = 123;
var y = 456;
var z = x + z * x / y;
x && y && (z = x ? y : x);
}())
}
function a() {}
function b() {}
function c() {}
function d() {}
function e() {}
function f() {}
`

for(var i = 0; i < 2; i++) {
avgJs += `(function() {${avgJs}}());`;
}

const fs = require("fs");
const root = __dirname;

createTree(fs, 100, `${root}/modules-100`);
createTree(fs, 500, `${root}/modules-500`);
createTree(fs, 1000, `${root}/modules-1000`);
createTree(fs, 3000, `${root}/modules-3000`);
createTree(fs, 5000, `${root}/modules-5000`);

function createTree(fs, count, folder) {
fs.mkdirSync(folder);
let remaining = count - 1;

function make(prefix, count, depth) {
if(count === 0) {
fs.writeFileSync(`${folder}/${prefix}.js`, `export default 1;\n${avgJs}`);
} else {
const list = [];
for(let i = 0; i < count; i++) {
if(remaining-- <= 0) break;
if(depth <= 4 && i >= 3 && i <= 4) {
list.push(`const module${i} = import("./${prefix}-${i}");\ncounter += module${i};`);
} else {
list.push(`import module${i} from "./${prefix}-${i}";\ncounter += module${i};`);
}
make(`${prefix}-${i}`, depth > 4 || count > 30 ? 0 : count + depth + i ** 2, depth + 1);
}
fs.writeFileSync(`${folder}/${prefix}.js`, `let counter = 0;\n${list.join("\n")};\nexport default counter;\n${avgJs}`)
}
}
make("index", 2, 0);
}
1 change: 1 addition & 0 deletions bin/config-optimist.js
Expand Up @@ -2,6 +2,7 @@ module.exports = function(optimist) {
optimist
.boolean("help").alias("help", "h").alias("help", "?").describe("help")
.string("config").describe("config", "Path to the config file")
.string("config-name").describe("config-name", "Name of the config to use")
.string("env").describe("env", "Environment passed to the config, when it is a function")
.string("context").describe("context", "The root directory for resolving entry point and stats")
.string("entry").describe("entry", "The entry point")
Expand Down
14 changes: 11 additions & 3 deletions bin/config-yargs.js
Expand Up @@ -9,7 +9,7 @@ var OPTIMIZE_GROUP = "Optimizing options:";
module.exports = function(yargs) {
yargs
.help("help")
.alias("help", "h", "?")
.alias("help", "h")
.version()
.alias("version", "v")
.options({
Expand All @@ -20,6 +20,12 @@ module.exports = function(yargs) {
defaultDescription: "webpack.config.js or webpackfile.js",
requiresArg: true
},
"config-name": {
type: "string",
describe: "Name of the config to use",
group: CONFIG_GROUP,
requiresArg: true
},
"env": {
describe: "Environment passed to the config, when it is a function",
group: CONFIG_GROUP
Expand Down Expand Up @@ -245,12 +251,14 @@ module.exports = function(yargs) {
"bail": {
type: "boolean",
describe: "Abort the compilation on first error",
group: ADVANCED_GROUP
group: ADVANCED_GROUP,
default: null
},
"profile": {
type: "boolean",
describe: "Profile the compilation and include information in stats",
group: ADVANCED_GROUP
group: ADVANCED_GROUP,
default: null
},
"d": {
type: "boolean",
Expand Down
45 changes: 30 additions & 15 deletions bin/convert-argv.js
Expand Up @@ -2,6 +2,7 @@ var path = require("path");
var fs = require("fs");
fs.existsSync = fs.existsSync || path.existsSync;
var interpret = require("interpret");
var prepareOptions = require("../lib/prepareOptions");

module.exports = function(yargs, argv, convertOptions) {

Expand Down Expand Up @@ -94,13 +95,7 @@ module.exports = function(yargs, argv, convertOptions) {

var requireConfig = function requireConfig(configPath) {
var options = require(configPath);
var isES6DefaultExportedFunc = (
typeof options === "object" && options !== null && typeof options.default === "function"
);
if(typeof options === "function" || isES6DefaultExportedFunc) {
options = isES6DefaultExportedFunc ? options.default : options;
options = options(argv.env, argv);
}
options = prepareOptions(options, argv);
return options;
};

Expand Down Expand Up @@ -135,6 +130,20 @@ module.exports = function(yargs, argv, convertOptions) {
return processConfiguredOptions(options.default);
}

// filter multi-config by name
if(Array.isArray(options) && argv["config-name"]) {
var namedOptions = options.filter(function(opt) {
return opt.name === argv["config-name"];
});
if(namedOptions.length === 0) {
console.error("Configuration with name '" + argv["config-name"] + "' was not found.");
process.exit(-1); // eslint-disable-line
} else if(namedOptions.length === 1) {
return processConfiguredOptions(namedOptions[0]);
}
options = namedOptions;
}

if(Array.isArray(options)) {
options.forEach(processOptions);
} else {
Expand Down Expand Up @@ -284,24 +293,30 @@ module.exports = function(yargs, argv, convertOptions) {
ensureObject(options, "entry");
});

function bindLoaders(arg, collection) {
function bindRules(arg) {
ifArgPair(arg, function(name, binding) {
if(name === null) {
name = binding;
binding += "-loader";
}
options.module[collection].push({
test: new RegExp("\\." + name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$"),
var rule = {
test: new RegExp("\\." + name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$"), // eslint-disable-line no-useless-escape
loader: binding
});
};
if(arg === "module-bind-pre") {
rule.enforce = "pre";
} else if(arg === "module-bind-post") {
rule.enforce = "post";
}
options.module.rules.push(rule);
}, function() {
ensureObject(options, "module");
ensureArray(options.module, collection);
ensureArray(options.module, "rules");
});
}
bindLoaders("module-bind", "loaders");
bindLoaders("module-bind-pre", "preLoaders");
bindLoaders("module-bind-post", "postLoaders");
bindRules("module-bind");
bindRules("module-bind-pre");
bindRules("module-bind-post");

var defineObject;
ifArgPair("define", function(name, value) {
Expand Down
9 changes: 8 additions & 1 deletion hot/log-apply-result.js
Expand Up @@ -20,7 +20,14 @@ module.exports = function(updatedModules, renewedModules) {
} else {
log("info", "[HMR] Updated modules:");
renewedModules.forEach(function(moduleId) {
log("info", "[HMR] - " + moduleId);
if(typeof moduleId === "string" && moduleId.indexOf("!") !== -1) {
var parts = moduleId.split("!");
log.groupCollapsed("info", "[HMR] - " + parts.pop());
log("info", "[HMR] - " + moduleId);
log.groupEnd("info");
} else {
log("info", "[HMR] - " + moduleId);
}
});
var numberIds = renewedModules.every(function(moduleId) {
return typeof moduleId === "number";
Expand Down
42 changes: 36 additions & 6 deletions hot/log.js
@@ -1,14 +1,44 @@
var logLevel = "info";

function dummy() {}

function shouldLog(level) {
var shouldLog = (logLevel === "info" && level === "info") ||
(["info", "warning"].indexOf(logLevel) >= 0 && level === "warning") ||
(["info", "warning", "error"].indexOf(logLevel) >= 0 && level === "error");
return shouldLog;
}

function logGroup(logFn) {
return function(level, msg) {
if(shouldLog(level)) {
logFn(msg);
}
};
}

module.exports = function(level, msg) {
if(logLevel === "info" && level === "info")
return console.log(msg);
if(["info", "warning"].indexOf(logLevel) >= 0 && level === "warning")
return console.warn(msg);
if(["info", "warning", "error"].indexOf(logLevel) >= 0 && level === "error")
return console.error(msg);
if(shouldLog(level)) {
if(level === "info") {
console.log(msg);
} else if(level === "warning") {
console.warn(msg);
} else if(level === "error") {
console.error(msg);
}
}
};

var group = console.group || dummy;
var groupCollapsed = console.groupCollapsed || dummy;
var groupEnd = console.groupEnd || dummy;

module.exports.group = logGroup(group);

module.exports.groupCollapsed = logGroup(groupCollapsed);

module.exports.groupEnd = logGroup(groupEnd);

module.exports.setLogLevel = function(level) {
logLevel = level;
};
2 changes: 1 addition & 1 deletion lib/AmdMainTemplatePlugin.js
Expand Up @@ -17,7 +17,7 @@ class AmdMainTemplatePlugin {
const mainTemplate = compilation.mainTemplate;

compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
const externals = chunk.modules.filter((m) => m.external);
const externals = chunk.getModules().filter((m) => m.external);
const externalsDepsArray = JSON.stringify(externals.map((m) =>
typeof m.request === "object" ? m.request.amd : m.request
));
Expand Down
6 changes: 5 additions & 1 deletion lib/Chunk.js
Expand Up @@ -421,8 +421,12 @@ class Chunk {
};
}

sortModules(sortByFn) {
this._modules.sortWith(sortByFn || sortById);
}

sortItems() {
this._modules.sortWith(sortById);
this.sortModules();
this.origins.sort((a, b) => {
const aIdent = a.module.identifier();
const bIdent = b.module.identifier();
Expand Down
1 change: 1 addition & 0 deletions lib/Compilation.js
Expand Up @@ -80,6 +80,7 @@ class Compilation extends Tapable {
this.children = [];
this.dependencyFactories = new Map();
this.dependencyTemplates = new Map();
this.dependencyTemplates.set("hash", "");
this.childrenCounters = {};
}

Expand Down

0 comments on commit 32264b8

Please sign in to comment.