Skip to content

Commit

Permalink
Merge branch 'master' into bump_prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Mar 29, 2018
2 parents f1092ad + 0f70fcb commit 8d8da4c
Show file tree
Hide file tree
Showing 44 changed files with 482 additions and 129 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -52,7 +52,7 @@ yarn add webpack --dev

<h2 align="center">Introduction</h2>

> This README reflects Webpack v2.x and v3.x. The Webpack v1.x documentation has been deprecated and deleted.
> This README reflects webpack v2.x and v3.x. The webpack v1.x documentation has been deprecated and deleted.
webpack is a bundler for modules. The main purpose is to bundle JavaScript
files for usage in a browser, yet it is also capable of transforming, bundling,
Expand Down
79 changes: 69 additions & 10 deletions bin/webpack.js 100644 → 100755
@@ -1,21 +1,80 @@
#!/usr/bin/env node
function runCommand(command, options) {
const cp = require("child_process");
return new Promise((resolve, reject) => {
const executedCommand = cp.spawn(command, options, {
stdio: "inherit"
});

executedCommand.on("error", error => {
reject(error);
});

executedCommand.on("exit", code => {
if (code === 0) {
resolve(true);
} else {
reject();
}
});
});
}

let webpackCliInstalled = false;
try {
require.resolve("webpack-cli");
webpackCliInstalled = true;
} catch (e) {
} catch (err) {
webpackCliInstalled = false;
}

if (webpackCliInstalled) {
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
if (!webpackCliInstalled) {
const path = require("path");
const fs = require("fs");
const readLine = require("readline");
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));

const packageManager = isYarn ? "yarn" : "npm";
const options = ["install", "-D", "webpack-cli"];

if (isYarn) {
options[0] = "add";
}

const commandToBeRun = `${packageManager} ${options.join(" ")}`;

const question = `Would you like to install webpack-cli? (That will run ${commandToBeRun}) `;

console.error("The CLI moved into a separate package: webpack-cli");
const questionInterface = readLine.createInterface({
input: process.stdin,
output: process.stdout
});
questionInterface.question(question, answer => {
questionInterface.close();
switch (answer.toLowerCase()) {
case "y":
case "yes":
case "1": {
runCommand(packageManager, options)
.then(result => {
return require("webpack-cli"); //eslint-disable-line
})
.catch(error => {
console.error(error);
process.exitCode = 1;
});
break;
}
default: {
console.error(
"It needs to be installed alongside webpack to use the CLI"
);
process.exitCode = 1;
break;
}
}
});
} else {
console.error("The CLI moved into a separate package: webpack-cli.");
console.error(
"Please install 'webpack-cli' in addition to webpack itself to use the CLI."
);
console.error("-> When using npm: npm install webpack-cli -D");
console.error("-> When using yarn: yarn add webpack-cli -D");
process.exitCode = 1;
require("webpack-cli"); // eslint-disable-line
}
8 changes: 6 additions & 2 deletions lib/Compilation.js
Expand Up @@ -158,8 +158,6 @@ class Compilation extends Tapable {
beforeChunkAssets: new SyncHook([]),
additionalChunkAssets: new SyncHook(["chunks"]),

records: new SyncHook(["compilation", "records"]),

additionalAssets: new AsyncSeriesHook([]),
optimizeChunkAssets: new AsyncSeriesHook(["chunks"]),
afterOptimizeChunkAssets: new SyncHook(["chunks"]),
Expand Down Expand Up @@ -253,6 +251,9 @@ class Compilation extends Tapable {
this.childrenCounters = {};
this.usedChunkIds = null;
this.usedModuleIds = null;
this.fileTimestamps = undefined;
this.contextTimestamps = undefined;
this.compilationDependencies = undefined;

this._buildingModules = new Map();
this._rebuildingModules = new Map();
Expand All @@ -277,6 +278,9 @@ class Compilation extends Tapable {
if (this.cache && this.cache[cacheName]) {
const cacheModule = this.cache[cacheName];

if (typeof cacheModule.updateCacheModule === "function")
cacheModule.updateCacheModule(module);

let rebuild = true;
if (this.fileTimestamps && this.contextTimestamps) {
rebuild = cacheModule.needRebuild(
Expand Down
17 changes: 7 additions & 10 deletions lib/Compiler.js
Expand Up @@ -94,36 +94,36 @@ class Compiler extends Tapable {
this.resolverFactory.plugin("resolver normal", resolver => {
resolver.plugin(hook, fn);
});
}, "webpack: Using compiler.resolvers.normal is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver normal", resolver => {\n resolver.plugin(/* ... */);\n}); instead.'),
}, "webpack: Using compiler.resolvers.normal is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver normal", resolver => {\n resolver.plugin(/* */);\n}); instead.'),
apply: util.deprecate((...args) => {
this.resolverFactory.plugin("resolver normal", resolver => {
resolver.apply(...args);
});
}, "webpack: Using compiler.resolvers.normal is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver normal", resolver => {\n resolver.apply(/* ... */);\n}); instead.')
}, "webpack: Using compiler.resolvers.normal is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver normal", resolver => {\n resolver.apply(/* */);\n}); instead.')
},
loader: {
plugins: util.deprecate((hook, fn) => {
this.resolverFactory.plugin("resolver loader", resolver => {
resolver.plugin(hook, fn);
});
}, "webpack: Using compiler.resolvers.loader is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver loader", resolver => {\n resolver.plugin(/* ... */);\n}); instead.'),
}, "webpack: Using compiler.resolvers.loader is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver loader", resolver => {\n resolver.plugin(/* */);\n}); instead.'),
apply: util.deprecate((...args) => {
this.resolverFactory.plugin("resolver loader", resolver => {
resolver.apply(...args);
});
}, "webpack: Using compiler.resolvers.loader is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver loader", resolver => {\n resolver.apply(/* ... */);\n}); instead.')
}, "webpack: Using compiler.resolvers.loader is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver loader", resolver => {\n resolver.apply(/* */);\n}); instead.')
},
context: {
plugins: util.deprecate((hook, fn) => {
this.resolverFactory.plugin("resolver context", resolver => {
resolver.plugin(hook, fn);
});
}, "webpack: Using compiler.resolvers.context is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver context", resolver => {\n resolver.plugin(/* ... */);\n}); instead.'),
}, "webpack: Using compiler.resolvers.context is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver context", resolver => {\n resolver.plugin(/* */);\n}); instead.'),
apply: util.deprecate((...args) => {
this.resolverFactory.plugin("resolver context", resolver => {
resolver.apply(...args);
});
}, "webpack: Using compiler.resolvers.context is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver context", resolver => {\n resolver.apply(/* ... */);\n}); instead.')
}, "webpack: Using compiler.resolvers.context is deprecated.\n" + 'Use compiler.resolverFactory.plugin("resolver context", resolver => {\n resolver.apply(/* */);\n}); instead.')
}
};

Expand Down Expand Up @@ -448,10 +448,7 @@ class Compiler extends Tapable {
}

createContextModuleFactory() {
const contextModuleFactory = new ContextModuleFactory(
this.resolverFactory,
this.inputFileSystem
);
const contextModuleFactory = new ContextModuleFactory(this.resolverFactory);
this.hooks.contextModuleFactory.call(contextModuleFactory);
return contextModuleFactory;
}
Expand Down
14 changes: 13 additions & 1 deletion lib/ContextModule.js
Expand Up @@ -43,6 +43,14 @@ class ContextModule extends Module {

if (typeof options.mode !== "string")
throw new Error("options.mode is a required option");

this._identifier = this._createIdentifier();
}

updateCacheModule(module) {
this.resolveDependencies = module.resolveDependencies;
this.options = module.options;
this.resolveOptions = module.resolveOptions;
}

prettyRegExp(regexString) {
Expand All @@ -63,7 +71,7 @@ class ContextModule extends Module {
.join("!");
}

identifier() {
_createIdentifier() {
let identifier = this.context;
if (this.options.resourceQuery)
identifier += ` ${this.options.resourceQuery}`;
Expand All @@ -80,6 +88,10 @@ class ContextModule extends Module {
return identifier;
}

identifier() {
return this._identifier;
}

readableIdentifier(requestShortener) {
let identifier = requestShortener.shorten(this.context);
if (this.options.resourceQuery)
Expand Down
1 change: 1 addition & 0 deletions lib/Dependency.js
Expand Up @@ -10,6 +10,7 @@ class Dependency {
this.module = null;
this.weak = false;
this.optional = false;
this.loc = undefined;
}

getResourceIdentifier() {
Expand Down
2 changes: 1 addition & 1 deletion lib/HotUpdateChunkTemplate.js
Expand Up @@ -47,7 +47,7 @@ module.exports = class HotUpdateChunkTemplate extends Tapable {
hotUpdateChunk.removedModules = removedModules;
const modulesSource = Template.renderChunkModules(
hotUpdateChunk,
() => true,
m => typeof m.source === "function",
moduleTemplate,
dependencyTemplates
);
Expand Down
14 changes: 7 additions & 7 deletions lib/JavascriptGenerator.js
Expand Up @@ -83,9 +83,9 @@ class JavascriptGenerator {
* we can not inject "foo" twice, therefore we just make two IIFEs like so:
* (function(foo, bar, baz){
* (function(foo, some, more){
* ...
* }(...));
* }(...));
*
* }());
* }());
*
* "splitVariablesInUniqueNamedChunks" splits the variables shown above up to this:
* [[foo, bar, baz], [foo, some, more]]
Expand Down Expand Up @@ -177,8 +177,8 @@ class JavascriptGenerator {

/*
* creates the start part of a IIFE around the module to inject a variable name
* (function(...){ <- this part
* }.call(...))
* (function(){ <- this part
* }.call())
*/
variableInjectionFunctionWrapperStartCode(varNames) {
const args = varNames.join(", ");
Expand All @@ -194,8 +194,8 @@ class JavascriptGenerator {

/*
* creates the end part of a IIFE around the module to inject a variable name
* (function(...){
* }.call(...)) <- this part
* (function(){
* }.call()) <- this part
*/
variableInjectionFunctionWrapperEndCode(module, varExpressions, block) {
const firstParam = this.contextArgument(module, block);
Expand Down
12 changes: 6 additions & 6 deletions lib/JavascriptModulesPlugin.js
Expand Up @@ -32,18 +32,18 @@ class JavascriptModulesPlugin {
});
normalModuleFactory.hooks.createGenerator
.for("javascript/auto")
.tap("JavascriptModulesPlugin", options => {
return new JavascriptGenerator(options);
.tap("JavascriptModulesPlugin", () => {
return new JavascriptGenerator();
});
normalModuleFactory.hooks.createGenerator
.for("javascript/dynamic")
.tap("JavascriptModulesPlugin", options => {
return new JavascriptGenerator(options);
.tap("JavascriptModulesPlugin", () => {
return new JavascriptGenerator();
});
normalModuleFactory.hooks.createGenerator
.for("javascript/esm")
.tap("JavascriptModulesPlugin", options => {
return new JavascriptGenerator(options);
.tap("JavascriptModulesPlugin", () => {
return new JavascriptGenerator();
});
compilation.mainTemplate.hooks.renderManifest.tap(
"JavascriptModulesPlugin",
Expand Down
3 changes: 3 additions & 0 deletions lib/Module.js
Expand Up @@ -67,6 +67,8 @@ class Module extends DependenciesBlock {

// delayed operations
this._rewriteChunkInReasons = undefined;

this.useSourceMap = false;
}

get exportsArgument() {
Expand Down Expand Up @@ -336,5 +338,6 @@ Module.prototype.build = null;
Module.prototype.source = null;
Module.prototype.size = null;
Module.prototype.nameForCondition = null;
Module.prototype.updateCacheModule = null;

module.exports = Module;
5 changes: 4 additions & 1 deletion lib/MultiModule.js
Expand Up @@ -15,10 +15,13 @@ class MultiModule extends Module {
// Info from Factory
this.dependencies = dependencies;
this.name = name;
this._identifier = `multi ${this.dependencies
.map(d => d.request)
.join(" ")}`;
}

identifier() {
return `multi ${this.dependencies.map(d => d.request).join(" ")}`;
return this._identifier;
}

readableIdentifier(requestShortener) {
Expand Down
6 changes: 4 additions & 2 deletions lib/NoModeWarning.js
Expand Up @@ -13,8 +13,10 @@ module.exports = class NoModeWarning extends WebpackError {
this.name = "NoModeWarning";
this.message =
"configuration\n" +
"The 'mode' option has not been set. " +
"Set 'mode' option to 'development' or 'production' to enable defaults for this environment. ";
"The 'mode' option has not been set, webpack will fallback to 'production' for this value. " +
"Set 'mode' option to 'development' or 'production' to enable defaults for each environment.\n" +
"You can also set it to 'none' to disable any default behavior. " +
"Learn more: https://webpack.js.org/concepts/mode/";

Error.captureStackTrace(this, this.constructor);
}
Expand Down
9 changes: 9 additions & 0 deletions lib/NormalModule.js
Expand Up @@ -122,6 +122,15 @@ class NormalModule extends Module {
return this.resource;
}

updateCacheModule(module) {
this.userRequest = module.userRequest;
this.parser = module.parser;
this.generator = module.generator;
this.resource = module.resource;
this.loaders = module.loaders;
this.resolveOptions = module.resolveOptions;
}

createSourceForAsset(name, content, sourceMap) {
if (!sourceMap) {
return new RawSource(content);
Expand Down
10 changes: 2 additions & 8 deletions lib/OptionsDefaulter.js
Expand Up @@ -43,18 +43,12 @@ class OptionsDefaulter {
setProperty(
options,
name,
this.defaults[name].call(this, getProperty(options, name), options),
options
this.defaults[name].call(this, getProperty(options, name), options)
);
break;
case "make":
if (getProperty(options, name) === undefined)
setProperty(
options,
name,
this.defaults[name].call(this, options),
options
);
setProperty(options, name, this.defaults[name].call(this, options));
break;
case "append": {
let oldValue = getProperty(options, name);
Expand Down

0 comments on commit 8d8da4c

Please sign in to comment.