diff --git a/lib/NullFactory.js b/lib/NullFactory.js index d3b2e32dc33..90ede1fe3f1 100644 --- a/lib/NullFactory.js +++ b/lib/NullFactory.js @@ -2,9 +2,11 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -function NullFactory() {} -module.exports = NullFactory; +"use strict"; -NullFactory.prototype.create = function(data, callback) { - return callback(); -}; +class NullFactory { + create(data, callback) { + return callback(); + } +} +module.exports = NullFactory; diff --git a/lib/OptionsApply.js b/lib/OptionsApply.js index 016c9f77841..3b1ec316485 100644 --- a/lib/OptionsApply.js +++ b/lib/OptionsApply.js @@ -2,9 +2,9 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -function OptionsApply() {} -module.exports = OptionsApply; - -OptionsApply.prototype.process = function(options, compiler) { +"use strict"; -}; +class OptionsApply { + process(options, compiler) {} +} +module.exports = OptionsApply; diff --git a/lib/ProgressPlugin.js b/lib/ProgressPlugin.js index 5fbda81649d..9059c0767f6 100644 --- a/lib/ProgressPlugin.js +++ b/lib/ProgressPlugin.js @@ -2,188 +2,189 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -function ProgressPlugin(options) { - if(typeof options === "function") { - options = { - handler: options - }; +"use strict"; + +class ProgressPlugin { + + constructor(options) { + if(typeof options === "function") { + options = { + handler: options + }; + } + options = options || {}; + this.profile = options.profile; + this.handler = options.handler; } - options = options || {}; - this.profile = options.profile; - this.handler = options.handler; -} -module.exports = ProgressPlugin; -ProgressPlugin.prototype.apply = function(compiler) { - var handler = this.handler || defaultHandler; - var profile = this.profile; - if(compiler.compilers) { - var states = new Array(compiler.compilers.length); - compiler.compilers.forEach(function(compiler, idx) { - compiler.apply(new ProgressPlugin(function(p, msg) { - states[idx] = Array.prototype.slice.apply(arguments); - handler.apply(null, [ - states.map(function(state) { - return state && state[0] || 0; - }).reduce(function(a, b) { - return a + b; - }) / states.length, - "[" + idx + "] " + msg - ].concat(Array.prototype.slice.call(arguments, 2))); - })); - }); - } else { - var lastModulesCount = 0; - var moduleCount = 500; - var doneModules = 0; - var activeModules = []; + apply(compiler) { + const handler = this.handler || defaultHandler; + const profile = this.profile; + if(compiler.compilers) { + const states = new Array(compiler.compilers.length); + compiler.compilers.forEach(function(compiler, idx) { + compiler.apply(new ProgressPlugin((p, msg) => { + states[idx] = Array.prototype.slice.apply(arguments); + handler.apply(null, [ + states.map(state => state && state[0] || 0).reduce((a, b) => a + b) / states.length, + `[${idx}] ${msg}` + ].concat(Array.prototype.slice.call(arguments, 2))); + })); + }); + } else { + let lastModulesCount = 0; + let moduleCount = 500; + let doneModules = 0; + const activeModules = []; - var update = function update(module) { - handler( - 0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6, - "building modules", - doneModules + "/" + moduleCount + " modules", - activeModules.length + " active", - activeModules[activeModules.length - 1] - ); - }; + const update = function update(module) { + handler( + 0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6, + "building modules", + `${doneModules}/${moduleCount} modules`, + `${activeModules.length} active`, + activeModules[activeModules.length - 1] + ); + }; - var moduleDone = function moduleDone(module) { - doneModules++; - var ident = module.identifier(); - if(ident) { - var idx = activeModules.indexOf(ident); - if(idx >= 0) activeModules.splice(idx, 1); - } - update(); - }; - compiler.plugin("compilation", function(compilation) { - if(compilation.compiler.isChild()) return; - lastModulesCount = moduleCount; - moduleCount = 0; - doneModules = 0; - handler(0, "compiling"); - compilation.plugin("build-module", function(module) { - moduleCount++; - var ident = module.identifier(); + const moduleDone = function moduleDone(module) { + doneModules++; + const ident = module.identifier(); if(ident) { - activeModules.push(ident); + const idx = activeModules.indexOf(ident); + if(idx >= 0) activeModules.splice(idx, 1); } update(); - }); - compilation.plugin("failed-module", moduleDone); - compilation.plugin("succeed-module", moduleDone); - var syncHooks = { - "seal": [0.71, "sealing"], - "optimize": [0.72, "optimizing"], - "optimize-modules-basic": [0.73, "basic module optimization"], - "optimize-modules": [0.74, "module optimization"], - "optimize-modules-advanced": [0.75, "advanced module optimization"], - "optimize-chunks-basic": [0.76, "basic chunk optimization"], - "optimize-chunks": [0.77, "chunk optimization"], - "optimize-chunks-advanced": [0.78, "advanced chunk optimization"], - // optimize-tree - "revive-modules": [0.80, "module reviving"], - "optimize-module-order": [0.81, "module order optimization"], - "optimize-module-ids": [0.82, "module id optimization"], - "revive-chunks": [0.83, "chunk reviving"], - "optimize-chunk-order": [0.84, "chunk order optimization"], - "optimize-chunk-ids": [0.85, "chunk id optimization"], - "before-hash": [0.86, "hashing"], - "before-module-assets": [0.87, "module assets processing"], - "before-chunk-assets": [0.88, "chunk assets processing"], - "additional-chunk-assets": [0.89, "additional chunk assets processing"], - "record": [0.90, "recording"] }; - Object.keys(syncHooks).forEach(function(name) { - var pass = 0; - var settings = syncHooks[name]; - compilation.plugin(name, function() { - if(pass++ > 0) - handler(settings[0], settings[1], "pass " + pass); - else - handler(settings[0], settings[1]); + compiler.plugin("compilation", function(compilation) { + if(compilation.compiler.isChild()) return; + lastModulesCount = moduleCount; + moduleCount = 0; + doneModules = 0; + handler(0, "compiling"); + compilation.plugin("build-module", function(module) { + moduleCount++; + const ident = module.identifier(); + if(ident) { + activeModules.push(ident); + } + update(); + }); + compilation.plugin("failed-module", moduleDone); + compilation.plugin("succeed-module", moduleDone); + const syncHooks = { + "seal": [0.71, "sealing"], + "optimize": [0.72, "optimizing"], + "optimize-modules-basic": [0.73, "basic module optimization"], + "optimize-modules": [0.74, "module optimization"], + "optimize-modules-advanced": [0.75, "advanced module optimization"], + "optimize-chunks-basic": [0.76, "basic chunk optimization"], + "optimize-chunks": [0.77, "chunk optimization"], + "optimize-chunks-advanced": [0.78, "advanced chunk optimization"], + // optimize-tree + "revive-modules": [0.80, "module reviving"], + "optimize-module-order": [0.81, "module order optimization"], + "optimize-module-ids": [0.82, "module id optimization"], + "revive-chunks": [0.83, "chunk reviving"], + "optimize-chunk-order": [0.84, "chunk order optimization"], + "optimize-chunk-ids": [0.85, "chunk id optimization"], + "before-hash": [0.86, "hashing"], + "before-module-assets": [0.87, "module assets processing"], + "before-chunk-assets": [0.88, "chunk assets processing"], + "additional-chunk-assets": [0.89, "additional chunk assets processing"], + "record": [0.90, "recording"] + }; + Object.keys(syncHooks).forEach(name => { + let pass = 0; + const settings = syncHooks[name]; + compilation.plugin(name, () => { + if(pass++ > 0) + handler(settings[0], settings[1], `pass ${pass}`); + else + handler(settings[0], settings[1]); + }); + }); + compilation.plugin("optimize-tree", (chunks, modules, callback) => { + handler(0.79, "module and chunk tree optimization"); + callback(); + }); + compilation.plugin("additional-assets", callback => { + handler(0.91, "additional asset processing"); + callback(); + }); + compilation.plugin("optimize-chunk-assets", (chunks, callback) => { + handler(0.92, "chunk asset optimization"); + callback(); + }); + compilation.plugin("optimize-assets", (assets, callback) => { + handler(0.94, "asset optimization"); + callback(); }); }); - compilation.plugin("optimize-tree", function(chunks, modules, callback) { - handler(0.79, "module and chunk tree optimization"); - callback(); - }); - compilation.plugin("additional-assets", function(callback) { - handler(0.91, "additional asset processing"); - callback(); - }); - compilation.plugin("optimize-chunk-assets", function(chunks, callback) { - handler(0.92, "chunk asset optimization"); + compiler.plugin("emit", (compilation, callback) => { + handler(0.95, "emitting"); callback(); }); - compilation.plugin("optimize-assets", function(assets, callback) { - handler(0.94, "asset optimization"); - callback(); + compiler.plugin("done", () => { + handler(1, ""); }); - }); - compiler.plugin("emit", function(compilation, callback) { - handler(0.95, "emitting"); - callback(); - }); - compiler.plugin("done", function() { - handler(1, ""); - }); - } + } - var lineCaretPosition = 0, - lastState, lastStateTime; + let lineCaretPosition = 0, + lastState, lastStateTime; - function defaultHandler(percentage, msg) { - var state = msg; - var details = Array.prototype.slice.call(arguments, 2); - if(percentage < 1) { - percentage = Math.floor(percentage * 100); - msg = percentage + "% " + msg; - if(percentage < 100) { - msg = " " + msg; - } - if(percentage < 10) { - msg = " " + msg; - } - details.forEach(function(detail) { - if(!detail) return; - if(detail.length > 40) { - detail = "..." + detail.substr(detail.length - 37); + function defaultHandler(percentage, msg) { + let state = msg; + const details = Array.prototype.slice.call(arguments, 2); + if(percentage < 1) { + percentage = Math.floor(percentage * 100); + msg = `${percentage}% ${msg}`; + if(percentage < 100) { + msg = ` ${msg}`; } - msg += " " + detail; - }); - } - if(profile) { - state = state.replace(/^\d+\/\d+\s+/, ""); - if(percentage === 0) { - lastState = null; - lastStateTime = +new Date(); - } else if(state !== lastState || percentage === 1) { - var now = +new Date(); - if(lastState) { - var stateMsg = (now - lastStateTime) + "ms " + lastState; - goToLineStart(stateMsg); - process.stderr.write(stateMsg + "\n"); - lineCaretPosition = 0; + if(percentage < 10) { + msg = ` ${msg}`; } - lastState = state; - lastStateTime = now; + details.forEach(detail => { + if(!detail) return; + if(detail.length > 40) { + detail = `...${detail.substr(detail.length - 37)}`; + } + msg += ` ${detail}`; + }); } + if(profile) { + state = state.replace(/^\d+\/\d+\s+/, ""); + if(percentage === 0) { + lastState = null; + lastStateTime = +new Date(); + } else if(state !== lastState || percentage === 1) { + const now = +new Date(); + if(lastState) { + const stateMsg = `${now - lastStateTime}ms ${lastState}`; + goToLineStart(stateMsg); + process.stderr.write(stateMsg + "\n"); + lineCaretPosition = 0; + } + lastState = state; + lastStateTime = now; + } + } + goToLineStart(msg); + process.stderr.write(msg); } - goToLineStart(msg); - process.stderr.write(msg); - } - function goToLineStart(nextMessage) { - var str = ""; - for(; lineCaretPosition > nextMessage.length; lineCaretPosition--) { - str += "\b \b"; - } - for(var i = 0; i < lineCaretPosition; i++) { - str += "\b"; + function goToLineStart(nextMessage) { + let str = ""; + for(; lineCaretPosition > nextMessage.length; lineCaretPosition--) { + str += "\b \b"; + } + for(var i = 0; i < lineCaretPosition; i++) { + str += "\b"; + } + lineCaretPosition = nextMessage.length; + if(str) process.stderr.write(str); } - lineCaretPosition = nextMessage.length; - if(str) process.stderr.write(str); } -}; +} +module.exports = ProgressPlugin; diff --git a/lib/ProvidePlugin.js b/lib/ProvidePlugin.js index 403189a2d48..fdcfaf48a96 100644 --- a/lib/ProvidePlugin.js +++ b/lib/ProvidePlugin.js @@ -2,51 +2,54 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -var ParserHelpers = require("./ParserHelpers"); -var ConstDependency = require("./dependencies/ConstDependency"); +"use strict"; -var NullFactory = require("./NullFactory"); +const ParserHelpers = require("./ParserHelpers"); +const ConstDependency = require("./dependencies/ConstDependency"); -function ProvidePlugin(definitions) { - this.definitions = definitions; -} -module.exports = ProvidePlugin; -ProvidePlugin.prototype.apply = function(compiler) { - var definitions = this.definitions; - compiler.plugin("compilation", function(compilation, params) { - compilation.dependencyFactories.set(ConstDependency, new NullFactory()); - compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template()); - params.normalModuleFactory.plugin("parser", function(parser, parserOptions) { - Object.keys(definitions).forEach(function(name) { - var request = [].concat(definitions[name]); - var splittedName = name.split("."); - if(splittedName.length > 0) { - splittedName.slice(1).forEach(function(_, i) { - var name = splittedName.slice(0, i + 1).join("."); - parser.plugin("can-rename " + name, ParserHelpers.approve); - }); - } - parser.plugin("expression " + name, function(expr) { - var nameIdentifier = name; - var scopedName = name.indexOf(".") >= 0; - var expression = "require(" + JSON.stringify(request[0]) + ")"; - if(scopedName) { - nameIdentifier = "__webpack_provided_" + name.replace(/\./g, "_dot_"); - } - if(request.length > 1) { - expression += request.slice(1).map(function(r) { - return "[" + JSON.stringify(r) + "]"; - }).join(""); - } - if(!ParserHelpers.addParsedVariableToModule(this, nameIdentifier, expression)) { - return false; - } - if(scopedName) { - ParserHelpers.toConstantDependency(nameIdentifier).bind(this)(expr); +const NullFactory = require("./NullFactory"); + +class ProvidePlugin { + constructor(definitions) { + this.definitions = definitions; + } + + apply(compiler) { + const definitions = this.definitions; + compiler.plugin("compilation", (compilation, params) => { + compilation.dependencyFactories.set(ConstDependency, new NullFactory()); + compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template()); + params.normalModuleFactory.plugin("parser", (parser, parserOptions) => { + Object.keys(definitions).forEach(name => { + var request = [].concat(definitions[name]); + var splittedName = name.split("."); + if(splittedName.length > 0) { + splittedName.slice(1).forEach((_, i) => { + const name = splittedName.slice(0, i + 1).join("."); + parser.plugin(`can-rename ${name}`, ParserHelpers.approve); + }); } - return true; + parser.plugin(`expression ${name}`, function(expr) { + let nameIdentifier = name; + const scopedName = name.indexOf(".") >= 0; + let expression = `require(${JSON.stringify(request[0])})`; + if(scopedName) { + nameIdentifier = `__webpack_provided_${name.replace(/\./g, "_dot_")}`; + } + if(request.length > 1) { + expression += request.slice(1).map(r => `[${JSON.stringify(r)}]`).join(""); + } + if(!ParserHelpers.addParsedVariableToModule(this, nameIdentifier, expression)) { + return false; + } + if(scopedName) { + ParserHelpers.toConstantDependency(nameIdentifier).bind(this)(expr); + } + return true; + }); }); }); }); - }); -}; + } +} +module.exports = ProvidePlugin;