From b2e7837288d79d8671fbe316bc58aabe46775ac9 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 19 Jun 2019 13:16:05 +0200 Subject: [PATCH] replace Object.assign with object spread --- lib/Compilation.js | 12 ++-- lib/ContextModule.js | 17 +++--- lib/ContextModuleFactory.js | 40 ++++++------- lib/DelegatedModule.js | 2 +- lib/DelegatedPlugin.js | 12 ++-- lib/JavascriptParser.js | 9 ++- lib/ModuleFilenameHelpers.js | 14 ++--- lib/MultiStats.js | 21 +++---- lib/NodeStuffPlugin.js | 2 +- lib/NormalModule.js | 2 +- lib/OptionsDefaulter.js | 2 +- lib/ProgressPlugin.js | 2 +- lib/ResolverFactory.js | 2 +- lib/Watching.js | 2 +- lib/WebpackOptionsApply.js | 32 ++++------ lib/WebpackOptionsDefaulter.js | 18 +++--- lib/WebpackOptionsValidationError.js | 15 ++--- lib/cache/ResolverCachePlugin.js | 15 +++-- lib/dependencies/ContextDependency.js | 2 +- lib/dependencies/ContextDependencyHelpers.js | 48 +++++++-------- lib/dependencies/ImportParserPlugin.js | 5 +- lib/ids/HashedModuleIdsPlugin.js | 16 +++-- lib/node/NodeSourcePlugin.js | 2 +- lib/optimize/ConcatenatedModule.js | 5 +- lib/optimize/RuntimeChunkPlugin.js | 10 ++-- lib/optimize/SplitChunksPlugin.js | 15 ++--- lib/serialization/ObjectMiddleware.js | 60 +++++++++---------- lib/serialization/Serializer.js | 4 +- lib/stats/DefaultStatsFactoryPlugin.js | 45 ++++++-------- lib/stats/DefaultStatsPrinterPlugin.js | 18 +++--- lib/stats/StatsFactory.js | 10 ++-- lib/stats/StatsPrinter.js | 25 ++++---- lib/util/cleverMerge.js | 2 +- test/Errors.test.js | 2 +- test/TestCases.template.js | 23 +++---- test/cases/loaders/issue-2299/loader/index.js | 16 +++-- test/compareLocations.unittest.js | 12 ++-- .../filter-warnings/webpack.config.js | 13 ++-- test/statsCases/issue-7577/webpack.config.js | 42 ++++++------- .../named-chunk-groups/webpack.config.js | 20 +++---- .../webpack.config.js | 10 ++-- 41 files changed, 281 insertions(+), 343 deletions(-) diff --git a/lib/Compilation.js b/lib/Compilation.js index fdd981ca5..96f2a69cc 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -567,7 +567,7 @@ class Compilation { optionsOrPreset = { preset: optionsOrPreset }; } if (typeof optionsOrPreset === "object" && optionsOrPreset !== null) { - const options = Object.assign({}, optionsOrPreset); + const options = { ...optionsOrPreset }; if (options.preset !== undefined) { this.hooks.statsPreset.for(options.preset).call(options, context); } @@ -2528,12 +2528,10 @@ class Compilation { */ getPath(filename, data) { if (!data.hash) { - data = Object.assign( - { - hash: this.hash - }, - data - ); + data = { + hash: this.hash, + ...data + }; } return this.mainTemplate.getAssetPath(filename, data); } diff --git a/lib/ContextModule.js b/lib/ContextModule.js index 8600264ce..aa2964ef0 100644 --- a/lib/ContextModule.js +++ b/lib/ContextModule.js @@ -93,7 +93,8 @@ class ContextModule extends Module { // Info from Factory this.resolveDependencies = resolveDependencies; /** @type {ContextModuleOptions} */ - this.options = Object.assign({}, options, { + this.options = ({ + ...options, resource: resource, resourceQuery: resourceQuery }); @@ -324,11 +325,10 @@ class ContextModule extends Module { // for the lazy-once mode create a new async dependency block // and add that block to this context if (dependencies.length > 0) { - const block = new AsyncDependenciesBlock( - Object.assign({}, this.options.groupOptions, { - name: this.options.chunkName - }) - ); + const block = new AsyncDependenciesBlock({ + ...this.options.groupOptions, + name: this.options.chunkName + }); for (const dep of dependencies) { block.addDependency(dep); } @@ -360,9 +360,10 @@ class ContextModule extends Module { ); } const block = new AsyncDependenciesBlock( - Object.assign({}, this.options.groupOptions, { + { + ...this.options.groupOptions, name: chunkName - }), + }, dep.loc, dep.userRequest ); diff --git a/lib/ContextModuleFactory.js b/lib/ContextModuleFactory.js index a1d1b2512..c768a144b 100644 --- a/lib/ContextModuleFactory.js +++ b/lib/ContextModuleFactory.js @@ -49,17 +49,15 @@ module.exports = class ContextModuleFactory extends ModuleFactory { const missingDependencies = new Set(); const contextDependencies = new Set(); this.hooks.beforeResolve.callAsync( - Object.assign( - { - context: context, - dependencies: dependencies, - resolveOptions, - fileDependencies, - missingDependencies, - contextDependencies - }, - dependency.options - ), + { + context: context, + dependencies: dependencies, + resolveOptions, + fileDependencies, + missingDependencies, + contextDependencies, + ...dependency.options + }, (err, beforeResolveResult) => { if (err) return callback(err); @@ -145,17 +143,15 @@ module.exports = class ContextModuleFactory extends ModuleFactory { if (err) return callback(err); this.hooks.afterResolve.callAsync( - Object.assign( - { - addon: - loadersPrefix + - result[1].join("!") + - (result[1].length > 0 ? "!" : ""), - resource: result[0], - resolveDependencies: this.resolveDependencies.bind(this) - }, - beforeResolveResult - ), + { + addon: + loadersPrefix + + result[1].join("!") + + (result[1].length > 0 ? "!" : ""), + resource: result[0], + resolveDependencies: this.resolveDependencies.bind(this), + ...beforeResolveResult + }, (err, result) => { if (err) return callback(err); diff --git a/lib/DelegatedModule.js b/lib/DelegatedModule.js index d748c0f95..86069584d 100644 --- a/lib/DelegatedModule.js +++ b/lib/DelegatedModule.js @@ -87,7 +87,7 @@ class DelegatedModule extends Module { * @returns {void} */ build(options, compilation, resolver, fs, callback) { - this.buildMeta = Object.assign({}, this.delegateData.buildMeta); + this.buildMeta = { ...this.delegateData.buildMeta }; this.buildInfo = {}; this.dependencies.length = 0; this.delegatedSourceDependency = new DelegatedSourceDependency( diff --git a/lib/DelegatedPlugin.js b/lib/DelegatedPlugin.js index 300e309dc..a995ae0a3 100644 --- a/lib/DelegatedPlugin.js +++ b/lib/DelegatedPlugin.js @@ -38,14 +38,10 @@ class DelegatedPlugin { ); compiler.hooks.compile.tap("DelegatedPlugin", ({ normalModuleFactory }) => { - new DelegatedModuleFactoryPlugin( - Object.assign( - { - associatedObjectForCache: compiler.root - }, - this.options - ) - ).apply(normalModuleFactory); + new DelegatedModuleFactoryPlugin({ + associatedObjectForCache: compiler.root, + ...this.options + }).apply(normalModuleFactory); }); } } diff --git a/lib/JavascriptParser.js b/lib/JavascriptParser.js index 34d364732..5719a5e9a 100644 --- a/lib/JavascriptParser.js +++ b/lib/JavascriptParser.js @@ -2409,11 +2409,10 @@ class JavascriptParser { static parse(code, options) { const type = options ? options.sourceType : "module"; - const parserOptions = Object.assign( - Object.create(null), - defaultParserOptions, - options - ); + const parserOptions = { + ...defaultParserOptions, + ...options + }; if (type === "auto") { parserOptions.sourceType = "module"; diff --git a/lib/ModuleFilenameHelpers.js b/lib/ModuleFilenameHelpers.js index 9b00fe5d5..5ff0d733c 100644 --- a/lib/ModuleFilenameHelpers.js +++ b/lib/ModuleFilenameHelpers.js @@ -60,17 +60,15 @@ ModuleFilenameHelpers.createFilename = ( options, { requestShortener, chunkGraph } ) => { - const opts = Object.assign( - { - namespace: "", - moduleFilenameTemplate: "" - }, - typeof options === "object" + const opts = { + namespace: "", + moduleFilenameTemplate: "", + ...(typeof options === "object" ? options : { moduleFilenameTemplate: options - } - ); + }) + }; let absoluteResourcePath; let hash; diff --git a/lib/MultiStats.js b/lib/MultiStats.js index e59641bcf..8b925730b 100644 --- a/lib/MultiStats.js +++ b/lib/MultiStats.js @@ -47,13 +47,12 @@ class MultiStats { ? options.children[idx] : options.children; return stat.compilation.createStatsOptions( - Object.assign( - {}, - baseOptions, - childOptions && typeof childOptions === "object" + { + ...baseOptions, + ...(childOptions && typeof childOptions === "object" ? childOptions - : { preset: childOptions } - ), + : { preset: childOptions }) + }, context ); }); @@ -101,11 +100,12 @@ class MultiStats { if (!j.errors) return arr; return arr.concat( j.errors.map(obj => { - return Object.assign({}, obj, { + return { + ...obj, compilerPath: obj.compilerPath ? `${j.name}.${obj.compilerPath}` : j.name - }); + }; }) ); }, []); @@ -113,11 +113,12 @@ class MultiStats { if (!j.warnings) return arr; return arr.concat( j.warnings.map(obj => { - return Object.assign({}, obj, { + return { + ...obj, compilerPath: obj.compilerPath ? `${j.name}.${obj.compilerPath}` : j.name - }); + }; }) ); }, []); diff --git a/lib/NodeStuffPlugin.js b/lib/NodeStuffPlugin.js index 69e515611..c08a5662b 100644 --- a/lib/NodeStuffPlugin.js +++ b/lib/NodeStuffPlugin.js @@ -38,7 +38,7 @@ class NodeStuffPlugin { let localOptions = options; if (parserOptions.node) { - localOptions = Object.assign({}, localOptions, parserOptions.node); + localOptions = { ...localOptions, ...parserOptions.node }; } const setModuleConstant = (expressionName, fn) => { diff --git a/lib/NormalModule.js b/lib/NormalModule.js index f5395267b..aeb7b4e13 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -468,7 +468,7 @@ class NormalModule extends Module { */ markModuleAsErrored(error) { // Restore build meta from successful build to keep importing state - this.buildMeta = Object.assign({}, this._lastSuccessfulBuildMeta); + this.buildMeta = { ...this._lastSuccessfulBuildMeta }; this.error = error; this.errors.push(error); } diff --git a/lib/OptionsDefaulter.js b/lib/OptionsDefaulter.js index af8018448..01019fbfb 100644 --- a/lib/OptionsDefaulter.js +++ b/lib/OptionsDefaulter.js @@ -32,7 +32,7 @@ class OptionsDefaulter { } process(options) { - options = Object.assign({}, options); + options = { ...options }; for (let name in this.defaults) { switch (this.config[name]) { case undefined: diff --git a/lib/ProgressPlugin.js b/lib/ProgressPlugin.js index 044bbbaac..0630739cf 100644 --- a/lib/ProgressPlugin.js +++ b/lib/ProgressPlugin.js @@ -110,7 +110,7 @@ class ProgressPlugin { options = options || {}; validateOptions(schema, options, "Progress Plugin"); - options = Object.assign({}, ProgressPlugin.defaultOptions, options); + options = { ...ProgressPlugin.defaultOptions, ...options }; this.profile = options.profile; this.handler = options.handler; diff --git a/lib/ResolverFactory.js b/lib/ResolverFactory.js index 0eb2c36f0..c2c1d7ba1 100644 --- a/lib/ResolverFactory.js +++ b/lib/ResolverFactory.js @@ -67,7 +67,7 @@ module.exports = class ResolverFactory { * @returns {Resolver} the resolver */ _create(type, resolveOptions) { - const originalResolveOptions = Object.assign({}, resolveOptions); + const originalResolveOptions = { ...resolveOptions }; resolveOptions = this.hooks.resolveOptions.for(type).call(resolveOptions); const resolver = Factory.createResolver(resolveOptions); if (!resolver) { diff --git a/lib/Watching.js b/lib/Watching.js index 826535c4c..7044b7e01 100644 --- a/lib/Watching.js +++ b/lib/Watching.js @@ -37,7 +37,7 @@ class Watching { aggregateTimeout: watchOptions }; } else if (watchOptions && typeof watchOptions === "object") { - this.watchOptions = Object.assign({}, watchOptions); + this.watchOptions = { ...watchOptions }; } else { this.watchOptions = {}; } diff --git a/lib/WebpackOptionsApply.js b/lib/WebpackOptionsApply.js index 002301329..faff67b9f 100644 --- a/lib/WebpackOptionsApply.js +++ b/lib/WebpackOptionsApply.js @@ -617,33 +617,27 @@ class WebpackOptionsApply extends OptionsApply { compiler.resolverFactory.hooks.resolveOptions .for("normal") .tap("WebpackOptionsApply", resolveOptions => { - return Object.assign( - { - fileSystem: compiler.inputFileSystem - }, - cachedCleverMerge(options.resolve, resolveOptions) - ); + return { + fileSystem: compiler.inputFileSystem, + ...cachedCleverMerge(options.resolve, resolveOptions) + }; }); compiler.resolverFactory.hooks.resolveOptions .for("context") .tap("WebpackOptionsApply", resolveOptions => { - return Object.assign( - { - fileSystem: compiler.inputFileSystem, - resolveToContext: true - }, - cachedCleverMerge(options.resolve, resolveOptions) - ); + return { + fileSystem: compiler.inputFileSystem, + resolveToContext: true, + ...cachedCleverMerge(options.resolve, resolveOptions) + }; }); compiler.resolverFactory.hooks.resolveOptions .for("loader") .tap("WebpackOptionsApply", resolveOptions => { - return Object.assign( - { - fileSystem: compiler.inputFileSystem - }, - cachedCleverMerge(options.resolveLoader, resolveOptions) - ); + return { + fileSystem: compiler.inputFileSystem, + ...cachedCleverMerge(options.resolveLoader, resolveOptions) + }; }); compiler.hooks.afterResolvers.call(compiler); return options; diff --git a/lib/WebpackOptionsDefaulter.js b/lib/WebpackOptionsDefaulter.js index 4f71ff0b2..c60c21a7e 100644 --- a/lib/WebpackOptionsDefaulter.js +++ b/lib/WebpackOptionsDefaulter.js @@ -35,7 +35,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { constructor() { super(); - this.set("experiments", "call", value => Object.assign({}, value)); + this.set("experiments", "call", value => ({ ...value })); this.set("experiments.mjs", false); this.set("experiments.importAwait", false); this.set("experiments.importAsync", false); @@ -60,7 +60,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { if (!value) { return false; } - value = Object.assign({}, value); + value = { ...value }; if (value.type === "filesystem") { if (value.name === undefined) { value.name = @@ -95,7 +95,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { this.set("context", process.cwd()); this.set("target", "web"); - this.set("module", "call", value => Object.assign({}, value)); + this.set("module", "call", value => ({ ...value })); this.set("module.unknownContextRequest", "."); this.set("module.unknownContextRegExp", false); this.set("module.unknownContextRecursive", true); @@ -159,7 +159,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { } else if (typeof value !== "object") { return {}; } else { - return Object.assign({}, value); + return { ...value }; } }); @@ -236,7 +236,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { if (typeof value === "boolean") { return value; } else { - return Object.assign({}, value); + return { ...value }; } }); this.set("node.global", "make", options => { @@ -277,7 +277,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { (!isProductionLikeMode(options) || !isWebLikeTarget(options)) ) return false; - return Object.assign({}, value); + return { ...value }; }); this.set("performance.maxAssetSize", 250000); this.set("performance.maxEntrypointSize", 250000); @@ -285,7 +285,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { isProductionLikeMode(options) ? "warning" : false ); - this.set("optimization", "call", value => Object.assign({}, value)); + this.set("optimization", "call", value => ({ ...value })); this.set("optimization.removeAvailableModules", true); this.set("optimization.removeEmptyChunks", true); this.set("optimization.mergeDuplicateChunks", true); @@ -404,7 +404,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { } }); - this.set("resolve", "call", value => Object.assign({}, value)); + this.set("resolve", "call", value => ({ ...value })); this.set("resolve.cache", "make", options => !!options.cache); this.set("resolve.modules", ["node_modules"]); this.set("resolve.extensions", "make", options => @@ -436,7 +436,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { } }); - this.set("resolveLoader", "call", value => Object.assign({}, value)); + this.set("resolveLoader", "call", value => ({ ...value })); this.set("resolveLoader.cache", "make", options => !!options.cache); this.set("resolveLoader.mainFields", ["loader", "main"]); this.set("resolveLoader.extensions", [".js"]); diff --git a/lib/WebpackOptionsValidationError.js b/lib/WebpackOptionsValidationError.js index 9400aacb4..e6806b208 100644 --- a/lib/WebpackOptionsValidationError.js +++ b/lib/WebpackOptionsValidationError.js @@ -240,16 +240,11 @@ class WebpackOptionsValidationError extends WebpackError { 0, err.children.length - 1 ); - return WebpackOptionsValidationError.formatValidationError( - Object.assign({}, lastChild, { - children: remainingChildren, - parentSchema: Object.assign( - {}, - err.parentSchema, - lastChild.parentSchema - ) - }) - ); + return WebpackOptionsValidationError.formatValidationError({ + ...lastChild, + children: remainingChildren, + parentSchema: { ...err.parentSchema, ...lastChild.parentSchema } + }); } const children = filterChildren(err.children); if (children.length === 1) { diff --git a/lib/cache/ResolverCachePlugin.js b/lib/cache/ResolverCachePlugin.js index 7cccbe07c..baea82922 100644 --- a/lib/cache/ResolverCachePlugin.js +++ b/lib/cache/ResolverCachePlugin.js @@ -42,18 +42,17 @@ class ResolverCachePlugin { request, callback ) => { - const newRequest = Object.assign( - { - _ResolverCachePluginCacheMiss: true - }, - request - ); - const newResolveContext = Object.assign({}, resolveContext, { + const newRequest = { + _ResolverCachePluginCacheMiss: true, + ...request + }; + const newResolveContext = { + ...resolveContext, stack: new Set(), missing: new Set(), fileDependencies: new Set(), contextDependencies: new Set() - }); + }; const propagate = key => { if (resolveContext[key]) { for (const dep of newResolveContext[key]) { diff --git a/lib/dependencies/ContextDependency.js b/lib/dependencies/ContextDependency.js index aea393aa3..2157df5a4 100644 --- a/lib/dependencies/ContextDependency.js +++ b/lib/dependencies/ContextDependency.js @@ -35,7 +35,7 @@ class ContextDependency extends Dependency { this.options && (this.options.regExp.global || this.options.regExp.sticky) ) { - this.options = Object.assign({}, this.options, { regExp: null }); + this.options = { ...this.options, regExp: null }; this.hadGlobalOrStickyRegExp = true; } diff --git a/lib/dependencies/ContextDependencyHelpers.js b/lib/dependencies/ContextDependencyHelpers.js index 51c16295b..e7dc2b3d4 100644 --- a/lib/dependencies/ContextDependencyHelpers.js +++ b/lib/dependencies/ContextDependencyHelpers.js @@ -93,15 +93,13 @@ exports.create = (Dep, range, param, expr, options, contextOptions, parser) => { `^${quotemeta(prefix)}${innerRegExp}${quotemeta(postfix)}$` ); const dep = new Dep( - Object.assign( - { - request: context + query, - recursive: options.wrappedContextRecursive, - regExp, - mode: "sync" - }, - contextOptions - ), + { + request: context + query, + recursive: options.wrappedContextRecursive, + regExp, + mode: "sync", + ...contextOptions + }, range, valueRange ); @@ -174,15 +172,13 @@ exports.create = (Dep, range, param, expr, options, contextOptions, parser) => { )}$` ); const dep = new Dep( - Object.assign( - { - request: context + query, - recursive: options.wrappedContextRecursive, - regExp, - mode: "sync" - }, - contextOptions - ), + { + request: context + query, + recursive: options.wrappedContextRecursive, + regExp, + mode: "sync", + ...contextOptions + }, range, valueRange ); @@ -214,15 +210,13 @@ exports.create = (Dep, range, param, expr, options, contextOptions, parser) => { return dep; } else { const dep = new Dep( - Object.assign( - { - request: options.exprContextRequest, - recursive: options.exprContextRecursive, - regExp: options.exprContextRegExp, - mode: "sync" - }, - contextOptions - ), + { + request: options.exprContextRequest, + recursive: options.exprContextRecursive, + regExp: options.exprContextRegExp, + mode: "sync", + ...contextOptions + }, range, param.range ); diff --git a/lib/dependencies/ImportParserPlugin.js b/lib/dependencies/ImportParserPlugin.js index a2a293910..a3368af0d 100644 --- a/lib/dependencies/ImportParserPlugin.js +++ b/lib/dependencies/ImportParserPlugin.js @@ -175,9 +175,10 @@ class ImportParserPlugin { parser.state.current.addDependency(dep); } else { const depBlock = new ImportDependenciesBlock( - Object.assign(groupOptions, { + { + ...groupOptions, name: chunkName - }), + }, expr.loc, param.string, expr.range diff --git a/lib/ids/HashedModuleIdsPlugin.js b/lib/ids/HashedModuleIdsPlugin.js index e8012ff50..e2b1a5ee6 100644 --- a/lib/ids/HashedModuleIdsPlugin.js +++ b/lib/ids/HashedModuleIdsPlugin.js @@ -23,15 +23,13 @@ class HashedModuleIdsPlugin { validateOptions(schema, options || {}, "Hashed Module Ids Plugin"); /** @type {HashedModuleIdsPluginOptions} */ - this.options = Object.assign( - { - context: null, - hashFunction: "md4", - hashDigest: "base64", - hashDigestLength: 4 - }, - options - ); + this.options = { + context: null, + hashFunction: "md4", + hashDigest: "base64", + hashDigestLength: 4, + ...options + }; } apply(compiler) { diff --git a/lib/node/NodeSourcePlugin.js b/lib/node/NodeSourcePlugin.js index 3de8060b7..4618db905 100644 --- a/lib/node/NodeSourcePlugin.js +++ b/lib/node/NodeSourcePlugin.js @@ -38,7 +38,7 @@ module.exports = class NodeSourcePlugin { let localOptions = options; if (parserOptions.node) { - localOptions = Object.assign({}, localOptions, parserOptions.node); + localOptions = { ...localOptions, ...parserOptions.node }; } if (localOptions.global) { diff --git a/lib/optimize/ConcatenatedModule.js b/lib/optimize/ConcatenatedModule.js index 3334dc893..642019310 100644 --- a/lib/optimize/ConcatenatedModule.js +++ b/lib/optimize/ConcatenatedModule.js @@ -1231,9 +1231,10 @@ class ConcatenatedModule extends Module { moduleToInfoMap ); - const innerContext = Object.assign({}, context, { + const innerContext = { + ...context, dependencyTemplates: innerDependencyTemplates - }); + }; const set = new Set([ RuntimeGlobals.exports, // TODO check if really used diff --git a/lib/optimize/RuntimeChunkPlugin.js b/lib/optimize/RuntimeChunkPlugin.js index 84e89cc30..3782af421 100644 --- a/lib/optimize/RuntimeChunkPlugin.js +++ b/lib/optimize/RuntimeChunkPlugin.js @@ -11,12 +11,10 @@ const { STAGE_ADVANCED } = require("../OptimizationStages"); class RuntimeChunkPlugin { constructor(options) { - this.options = Object.assign( - { - name: entrypoint => `runtime~${entrypoint.name}` - }, - options - ); + this.options = { + name: entrypoint => `runtime~${entrypoint.name}`, + ...options + }; } /** diff --git a/lib/optimize/SplitChunksPlugin.js b/lib/optimize/SplitChunksPlugin.js index 4a5c33278..285db357a 100644 --- a/lib/optimize/SplitChunksPlugin.js +++ b/lib/optimize/SplitChunksPlugin.js @@ -239,7 +239,7 @@ const normalizeSizes = value => { javascript: value }; } else if (typeof value === "object" && value !== null) { - return Object.assign({}, value); + return { ...value }; } else { return {}; } @@ -383,9 +383,7 @@ const normalizeCacheGroups = cacheGroups => { if (result) { const groups = Array.isArray(result) ? result : [result]; for (const group of groups) { - results.push( - createCacheGroupSource(Object.assign({ key }, group)) - ); + results.push(createCacheGroupSource({ key, ...group })); } } } else { @@ -393,9 +391,7 @@ const normalizeCacheGroups = cacheGroups => { checkTest(option.test, module, context) && checkModuleType(option.type, module) ) { - results.push( - createCacheGroupSource(Object.assign({ key }, option)) - ); + results.push(createCacheGroupSource({ key, ...option })); } } } @@ -1006,10 +1002,7 @@ module.exports = class SplitChunksPlugin { hasNonZeroSizes(item.cacheGroup.minRemainingSize) ) { const chunk = validChunks[0]; - const chunkSizes = Object.assign( - {}, - chunkGraph.getChunkModulesSizes(chunk) - ); + const chunkSizes = { ...chunkGraph.getChunkModulesSizes(chunk) }; for (const key of Object.keys(item.sizes)) { chunkSizes[key] -= item.sizes[key]; } diff --git a/lib/serialization/ObjectMiddleware.js b/lib/serialization/ObjectMiddleware.js index 69dd1d168..ade1e742e 100644 --- a/lib/serialization/ObjectMiddleware.js +++ b/lib/serialization/ObjectMiddleware.js @@ -261,32 +261,30 @@ class ObjectMiddleware extends SerializerMiddleware { let currentPosTypeLookup = 0; const objectTypeLookup = new Map(); const cycleStack = new Set(); - const ctx = Object.assign( - { - write(value) { - process(value); - }, - snapshot() { - return { - length: result.length, - cycleStackSize: cycleStack.size, - referenceableSize: referenceable.size, - currentPos, - objectTypeLookupSize: objectTypeLookup.size, - currentPosTypeLookup - }; - }, - rollback(snapshot) { - result.length = snapshot.length; - setSetSize(cycleStack, snapshot.cycleStackSize); - setMapSize(referenceable, snapshot.referenceableSize); - currentPos = snapshot.currentPos; - setMapSize(objectTypeLookup, snapshot.objectTypeLookupSize); - currentPosTypeLookup = snapshot.currentPosTypeLookup; - } + const ctx = { + write(value) { + process(value); }, - context - ); + snapshot() { + return { + length: result.length, + cycleStackSize: cycleStack.size, + referenceableSize: referenceable.size, + currentPos, + objectTypeLookupSize: objectTypeLookup.size, + currentPosTypeLookup + }; + }, + rollback(snapshot) { + result.length = snapshot.length; + setSetSize(cycleStack, snapshot.cycleStackSize); + setMapSize(referenceable, snapshot.referenceableSize); + currentPos = snapshot.currentPos; + setMapSize(objectTypeLookup, snapshot.objectTypeLookupSize); + currentPosTypeLookup = snapshot.currentPosTypeLookup; + }, + ...context + }; const process = item => { // check if we can emit a reference const ref = referenceable.get(item); @@ -394,14 +392,12 @@ class ObjectMiddleware extends SerializerMiddleware { let currentPosTypeLookup = 0; const objectTypeLookup = new Map(); const result = []; - const ctx = Object.assign( - { - read() { - return decodeValue(); - } + const ctx = { + read() { + return decodeValue(); }, - context - ); + ...context + }; const decodeValue = () => { const item = read(); diff --git a/lib/serialization/Serializer.js b/lib/serialization/Serializer.js index 3a633fe52..bf0b2da7b 100644 --- a/lib/serialization/Serializer.js +++ b/lib/serialization/Serializer.js @@ -11,7 +11,7 @@ class Serializer { } serialize(obj, context) { - const ctx = Object.assign({}, context, this.context); + const ctx = { ...context, ...this.context }; return new Promise((resolve, reject) => resolve( this.middlewares.reduce((last, middleware) => { @@ -32,7 +32,7 @@ class Serializer { } deserialize(context) { - const ctx = Object.assign({}, context, this.context); + const ctx = { ...context, ...this.context }; return Promise.resolve().then(() => this.middlewares.reduceRight((last, middleware) => { if (last instanceof Promise) diff --git a/lib/stats/DefaultStatsFactoryPlugin.js b/lib/stats/DefaultStatsFactoryPlugin.js index 795081c23..1ad15f38f 100644 --- a/lib/stats/DefaultStatsFactoryPlugin.js +++ b/lib/stats/DefaultStatsFactoryPlugin.js @@ -50,6 +50,7 @@ const { * @property {string} modulesSort * @property {string} assetsSort * @property {Function[]} excludeAssets + * @property {Function[]} excludeModules * @property {Function[]} warningsFilter * @property {number} maxModules * @property {any} _env @@ -708,33 +709,25 @@ const FILTER = { if (excluded) return false; } }, - "compilation.modules": Object.assign( - { - excludeModules: EXCLUDE_MODULES_FILTER("module"), - "!orphanModules": (module, { compilation: { chunkGraph } }) => { - if (chunkGraph.getNumberOfModuleChunks(module) === 0) return false; - } + "compilation.modules": ({ + excludeModules: EXCLUDE_MODULES_FILTER("module"), + "!orphanModules": (module, { compilation: { chunkGraph } }) => { + if (chunkGraph.getNumberOfModuleChunks(module) === 0) return false; }, - BASE_MODULES_FILTER - ), - "module.modules": Object.assign( - { - excludeModules: EXCLUDE_MODULES_FILTER("nested") - }, - BASE_MODULES_FILTER - ), - "chunk.modules": Object.assign( - { - excludeModules: EXCLUDE_MODULES_FILTER("chunk") - }, - BASE_MODULES_FILTER - ), - "chunk.rootModules": Object.assign( - { - excludeModules: EXCLUDE_MODULES_FILTER("root-of-chunk") - }, - BASE_MODULES_FILTER - ) + ...BASE_MODULES_FILTER + }), + "module.modules": ({ + excludeModules: EXCLUDE_MODULES_FILTER("nested"), + ...BASE_MODULES_FILTER + }), + "chunk.modules": ({ + excludeModules: EXCLUDE_MODULES_FILTER("chunk"), + ...BASE_MODULES_FILTER + }), + "chunk.rootModules": ({ + excludeModules: EXCLUDE_MODULES_FILTER("root-of-chunk"), + ...BASE_MODULES_FILTER + }) }; /** @type {Record boolean | undefined>} */ diff --git a/lib/stats/DefaultStatsPrinterPlugin.js b/lib/stats/DefaultStatsPrinterPlugin.js index 1bde4918c..7859effb9 100644 --- a/lib/stats/DefaultStatsPrinterPlugin.js +++ b/lib/stats/DefaultStatsPrinterPlugin.js @@ -73,11 +73,10 @@ const SIMPLE_PRINTERS = { "compilation.entrypoints": (entrypoints, context, printer) => Array.isArray(entrypoints) ? undefined - : printer.print( - context.type, - Object.values(entrypoints), - Object.assign({}, context, { chunkGroupKind: "Entrypoint" }) - ), + : printer.print(context.type, Object.values(entrypoints), ({ + ...context, + chunkGroupKind: "Entrypoint" + })), "compilation.namedChunkGroups": (namedChunkGroups, context, printer) => { if (!Array.isArray(namedChunkGroups)) { const { @@ -90,11 +89,10 @@ const SIMPLE_PRINTERS = { !Object.prototype.hasOwnProperty.call(entrypoints, group.name) ); } - return printer.print( - context.type, - chunkGroups, - Object.assign({}, context, { chunkGroupKind: "Chunk Group" }) - ); + return printer.print(context.type, chunkGroups, ({ + ...context, + chunkGroupKind: "Chunk Group" + })); } }, "compilation.assetsByChunkName": () => "", diff --git a/lib/stats/StatsFactory.js b/lib/stats/StatsFactory.js index 3912b3aaf..f77e52ed4 100644 --- a/lib/stats/StatsFactory.js +++ b/lib/stats/StatsFactory.js @@ -90,10 +90,11 @@ class StatsFactory { } create(type, data, baseContext) { - const context = Object.assign({}, baseContext, { + const context = { + ...baseContext, type, [type]: data - }); + }; if (Array.isArray(data)) { // run filter on unsorted items const items = forEachLevelFilter( @@ -125,9 +126,10 @@ class StatsFactory { // for each item const resultItems = items2.map((item, i) => { - const itemContext = Object.assign({}, context, { + const itemContext = { + ...context, _index: i - }); + }; // run getItemName const itemName = forEachLevel(this.hooks.getItemName, `${type}[]`, h => diff --git a/lib/stats/StatsPrinter.js b/lib/stats/StatsPrinter.js index 4a1085ac1..cbb673555 100644 --- a/lib/stats/StatsPrinter.js +++ b/lib/stats/StatsPrinter.js @@ -62,10 +62,11 @@ class StatsPrinter { } print(type, object, baseContext) { - const context = Object.assign({}, baseContext, { + const context = { + ...baseContext, type, [type]: object - }); + }; let printResult = forEachLevel(this.hooks.print, type, hook => hook.call(object, context) @@ -77,9 +78,10 @@ class StatsPrinter { h.call(sortedItems, context) ); const printedItems = sortedItems.map((item, i) => { - const itemContext = Object.assign({}, context, { + const itemContext = { + ...context, _index: i - }); + }; const itemName = forEachLevel( this.hooks.getItemName, `${type}[]`, @@ -105,15 +107,12 @@ class StatsPrinter { h.call(elements, context) ); const printedElements = elements.map(element => { - const content = this.print( - `${type}.${element}`, - object[element], - Object.assign({}, context, { - _parent: object, - _element: element, - [element]: object[element] - }) - ); + const content = this.print(`${type}.${element}`, object[element], { + ...context, + _parent: object, + _element: element, + [element]: object[element] + }); return { element, content }; }); printResult = forEachLevel(this.hooks.printElements, type, h => diff --git a/lib/util/cleverMerge.js b/lib/util/cleverMerge.js index 5fdcdeed8..d840f0cfc 100644 --- a/lib/util/cleverMerge.js +++ b/lib/util/cleverMerge.js @@ -41,7 +41,7 @@ const cachedCleverMerge = (first, second) => { * @returns {object} merged object of first and second object */ const cleverMerge = (first, second) => { - const newObject = Object.assign({}, first); + const newObject = { ...first }; for (const key of Object.keys(second)) { if (!(key in newObject)) { newObject[key] = second[key]; diff --git a/test/Errors.test.js b/test/Errors.test.js index c2ad2c767..52bc47f98 100644 --- a/test/Errors.test.js +++ b/test/Errors.test.js @@ -92,7 +92,7 @@ const defaults = { async function compile(options) { const stats = await new Promise((resolve, reject) => { - const compiler = webpack(Object.assign({}, defaults.options, options)); + const compiler = webpack({ ...defaults.options, ...options }); if (options.mode === "production") { if (options.optimization) options.optimization.minimize = true; else options.optimization = { minimize: true }; diff --git a/test/TestCases.template.js b/test/TestCases.template.js index cef57fcf3..3036d17ab 100644 --- a/test/TestCases.template.js +++ b/test/TestCases.template.js @@ -97,11 +97,10 @@ const describeCases = config => { mode: config.mode || "none", optimization: config.mode ? NO_EMIT_ON_ERRORS_OPTIMIZATIONS - : Object.assign( - {}, - config.optimization, - DEFAULT_OPTIMIZATIONS - ), + : { + ...config.optimization, + ...DEFAULT_OPTIMIZATIONS + }, performance: { hints: false }, @@ -109,15 +108,11 @@ const describeCases = config => { __dirname: "mock", __filename: "mock" }, - cache: - config.cache && - Object.assign( - { - loglevel: "warning", - cacheDirectory - }, - config.cache - ), + cache: config.cache && { + loglevel: "warning", + cacheDirectory, + ...config.cache + }, output: { pathinfo: true, path: outputDirectory, diff --git a/test/cases/loaders/issue-2299/loader/index.js b/test/cases/loaders/issue-2299/loader/index.js index cb27f7cf2..0f27efe43 100644 --- a/test/cases/loaders/issue-2299/loader/index.js +++ b/test/cases/loaders/issue-2299/loader/index.js @@ -6,20 +6,26 @@ module.exports = function(content) { json.imports, function(url, callback) { this.loadModule(url, function(err, source, map, module) { - if(err) { + if (err) { return callback(err); } callback(null, JSON.parse(source)); }); }.bind(this), function(err, results) { - if(err) { + if (err) { return cb(err); } // Combine all the results into one object and return it - cb(null, "module.exports = " + JSON.stringify(results.reduce(function(prev, result) { - return Object.assign({}, prev, result); - }, json))); + cb( + null, + "module.exports = " + + JSON.stringify( + results.reduce(function(prev, result) { + return { ...prev, ...result }; + }, json) + ) + ); } ); }; diff --git a/test/compareLocations.unittest.js b/test/compareLocations.unittest.js index fce4d3dd7..17e4e1590 100644 --- a/test/compareLocations.unittest.js +++ b/test/compareLocations.unittest.js @@ -2,13 +2,11 @@ const { compareLocations } = require("../lib/util/comparators"); const createPosition = overrides => { - return Object.assign( - { - line: 10, - column: 5 - }, - overrides - ); + return { + line: 10, + column: 5, + ...overrides + }; }; const createLocation = (start, end, index) => { diff --git a/test/statsCases/filter-warnings/webpack.config.js b/test/statsCases/filter-warnings/webpack.config.js index a6d75daff..79f6ad88b 100644 --- a/test/statsCases/filter-warnings/webpack.config.js +++ b/test/statsCases/filter-warnings/webpack.config.js @@ -46,11 +46,8 @@ module.exports = [ ["should not filter"], [/should not filter/], [warnings => false] -].map(filter => - Object.assign({}, baseConfig, { - name: Array.isArray(filter) ? `[${filter}]` : `${filter}`, - stats: Object.assign({}, baseConfig.stats, { - warningsFilter: filter - }) - }) -); +].map(filter => ({ + ...baseConfig, + name: Array.isArray(filter) ? `[${filter}]` : `${filter}`, + stats: { ...baseConfig.stats, warningsFilter: filter } +})); diff --git a/test/statsCases/issue-7577/webpack.config.js b/test/statsCases/issue-7577/webpack.config.js index 3eab28df6..63ea3ce88 100644 --- a/test/statsCases/issue-7577/webpack.config.js +++ b/test/statsCases/issue-7577/webpack.config.js @@ -16,31 +16,25 @@ const base = { } }; module.exports = [ - Object.assign( - { - entry: "./a.js", - output: { - filename: "a-[name]-[chunkhash].js" - } + { + entry: "./a.js", + output: { + filename: "a-[name]-[chunkhash].js" }, - base - ), - Object.assign( - { - entry: "./b.js", - output: { - filename: "b-[name]-[chunkhash].js" - } + ...base + }, + { + entry: "./b.js", + output: { + filename: "b-[name]-[chunkhash].js" }, - base - ), - Object.assign( - { - entry: "./c.js", - output: { - filename: "c-[name]-[chunkhash].js" - } + ...base + }, + { + entry: "./c.js", + output: { + filename: "c-[name]-[chunkhash].js" }, - base - ) + ...base + } ]; diff --git a/test/statsCases/named-chunk-groups/webpack.config.js b/test/statsCases/named-chunk-groups/webpack.config.js index 617a63e18..a68fd6ff8 100644 --- a/test/statsCases/named-chunk-groups/webpack.config.js +++ b/test/statsCases/named-chunk-groups/webpack.config.js @@ -32,16 +32,12 @@ const config = { }; module.exports = [ - Object.assign( - { - stats: Object.assign({ entrypoints: false, chunkGroups: true }, stats) - }, - config - ), - Object.assign( - { - stats: Object.assign({ entrypoints: true, chunkGroups: true }, stats) - }, - config - ) + { + stats: { entrypoints: false, chunkGroups: true, ...stats }, + ...config + }, + { + stats: { entrypoints: true, chunkGroups: true, ...stats }, + ...config + } ]; diff --git a/test/statsCases/runtime-chunk-integration/webpack.config.js b/test/statsCases/runtime-chunk-integration/webpack.config.js index 6ee9324a2..5dd633798 100644 --- a/test/statsCases/runtime-chunk-integration/webpack.config.js +++ b/test/statsCases/runtime-chunk-integration/webpack.config.js @@ -18,7 +18,8 @@ const baseConfig = { ] }; -const withoutNamedEntry = Object.assign({}, baseConfig, { +const withoutNamedEntry = { + ...baseConfig, name: "base", entry: { main1: "./main1" @@ -26,9 +27,10 @@ const withoutNamedEntry = Object.assign({}, baseConfig, { optimization: { runtimeChunk: "single" } -}); +}; -const withNamedEntry = Object.assign({}, baseConfig, { +const withNamedEntry = { + ...baseConfig, name: "manifest is named entry", entry: { main1: "./main1", @@ -39,6 +41,6 @@ const withNamedEntry = Object.assign({}, baseConfig, { name: "manifest" } } -}); +}; module.exports = [withoutNamedEntry, withNamedEntry];