diff --git a/lib/ContextModule.js b/lib/ContextModule.js index ad0f98ce0..479d6a253 100644 --- a/lib/ContextModule.js +++ b/lib/ContextModule.js @@ -875,9 +875,10 @@ webpackEmptyAsyncContext.id = ${JSON.stringify(id)};`; } /** + * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module */ - size() { + size(type) { // base penalty const initialSize = 160; diff --git a/lib/DelegatedModule.js b/lib/DelegatedModule.js index 49da48cf3..47717ab37 100644 --- a/lib/DelegatedModule.js +++ b/lib/DelegatedModule.js @@ -148,9 +148,10 @@ class DelegatedModule extends Module { } /** + * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module */ - size() { + size(type) { return 42; } diff --git a/lib/DllModule.js b/lib/DllModule.js index d816246e2..fc9771712 100644 --- a/lib/DllModule.js +++ b/lib/DllModule.js @@ -85,9 +85,10 @@ class DllModule extends Module { } /** + * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module */ - size() { + size(type) { return 12; } diff --git a/lib/ExternalModule.js b/lib/ExternalModule.js index 6aa0b167a..84dd08fdb 100644 --- a/lib/ExternalModule.js +++ b/lib/ExternalModule.js @@ -244,9 +244,10 @@ class ExternalModule extends Module { } /** + * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module */ - size() { + size(type) { return 42; } diff --git a/lib/Generator.js b/lib/Generator.js index eb0a6e2a7..361f14887 100644 --- a/lib/Generator.js +++ b/lib/Generator.js @@ -39,6 +39,16 @@ class Generator { throw new Error("Generator.getTypes: must be overridden"); } + /** + * @abstract + * @param {NormalModule} module the module + * @param {string=} type source type + * @returns {number} estimate size of the module + */ + getSize(module, type) { + throw new Error("Generator.getSize: must be overridden"); + } + /** * @abstract * @param {NormalModule} module module for which the code should be generated @@ -67,6 +77,17 @@ class ByTypeGenerator extends Generator { return this._types; } + /** + * @param {NormalModule} module the module + * @param {string=} type source type + * @returns {number} estimate size of the module + */ + getSize(module, type) { + const t = type || "javascript"; + const generator = this.map[t]; + return generator ? generator.getSize(module, t) : 0; + } + /** * @param {NormalModule} module module for which the code should be generated * @param {GenerateContext} generateContext context for generate diff --git a/lib/JavascriptGenerator.js b/lib/JavascriptGenerator.js index 16658c285..ca802cc91 100644 --- a/lib/JavascriptGenerator.js +++ b/lib/JavascriptGenerator.js @@ -58,6 +58,19 @@ class JavascriptGenerator extends Generator { return TYPES; } + /** + * @param {NormalModule} module the module + * @param {string=} type source type + * @returns {number} estimate size of the module + */ + getSize(module, type) { + const originalSource = module.originalSource(); + if (!originalSource) { + return 39; + } + return originalSource.size(); + } + /** * @param {NormalModule} module module for which the code should be generated * @param {GenerateContext} generateContext context for generate diff --git a/lib/JsonGenerator.js b/lib/JsonGenerator.js index 26fceabf2..0cbc76182 100644 --- a/lib/JsonGenerator.js +++ b/lib/JsonGenerator.js @@ -35,6 +35,17 @@ class JsonGenerator extends Generator { return TYPES; } + /** + * @param {NormalModule} module the module + * @param {string=} type source type + * @returns {number} estimate size of the module + */ + getSize(module, type) { + let data = module.buildInfo.jsonData; + if (!data) return 0; + return stringifySafe(data).length + 10; + } + /** * @param {NormalModule} module module for which the code should be generated * @param {GenerateContext} generateContext context for generate diff --git a/lib/Module.js b/lib/Module.js index 3b61f233e..77dd46812 100644 --- a/lib/Module.js +++ b/lib/Module.js @@ -557,9 +557,10 @@ class Module extends DependenciesBlock { /** * @abstract + * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module */ - size() { + size(type) { throw new Error("Module.size: Must be overriden"); } diff --git a/lib/NormalModule.js b/lib/NormalModule.js index c335d54ba..410bffa80 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -764,10 +764,11 @@ class NormalModule extends Module { } /** + * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module */ - size() { - return this._source ? this._source.size() : -1; + size(type) { + return this.generator.getSize(this, type); } /** diff --git a/lib/RawModule.js b/lib/RawModule.js index 767397343..ea4b505f6 100644 --- a/lib/RawModule.js +++ b/lib/RawModule.js @@ -39,9 +39,10 @@ class RawModule extends Module { } /** + * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module */ - size() { + size(type) { return this.sourceStr.length; } diff --git a/lib/RuntimeModule.js b/lib/RuntimeModule.js index 3bbcff834..009d26917 100644 --- a/lib/RuntimeModule.js +++ b/lib/RuntimeModule.js @@ -99,9 +99,10 @@ class RuntimeModule extends Module { } /** + * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module */ - size() { + size(type) { return this.getGeneratedCode().length; } diff --git a/lib/optimize/ConcatenatedModule.js b/lib/optimize/ConcatenatedModule.js index e58700a35..21c0f0d71 100644 --- a/lib/optimize/ConcatenatedModule.js +++ b/lib/optimize/ConcatenatedModule.js @@ -529,14 +529,15 @@ class ConcatenatedModule extends Module { } /** + * @param {string=} type the source type for which the size should be estimated * @returns {number} the estimated size of the module */ - size() { + size(type) { // Guess size from embedded modules return this._orderedConcatenationList.reduce((sum, info) => { switch (info.type) { case "concatenated": - return sum + info.module.size(); + return sum + info.module.size(type); case "external": return sum + 5; } diff --git a/lib/wasm/WebAssemblyGenerator.js b/lib/wasm/WebAssemblyGenerator.js index 99086f141..581bc2bea 100644 --- a/lib/wasm/WebAssemblyGenerator.js +++ b/lib/wasm/WebAssemblyGenerator.js @@ -384,6 +384,19 @@ class WebAssemblyGenerator extends Generator { return TYPES; } + /** + * @param {NormalModule} module the module + * @param {string=} type source type + * @returns {number} estimate size of the module + */ + getSize(module, type) { + const originalSource = module.originalSource(); + if (!originalSource) { + return 0; + } + return originalSource.size(); + } + /** * @param {NormalModule} module module for which the code should be generated * @param {GenerateContext} generateContext context for generate diff --git a/lib/wasm/WebAssemblyJavascriptGenerator.js b/lib/wasm/WebAssemblyJavascriptGenerator.js index 008ddb1c0..18d6450d0 100644 --- a/lib/wasm/WebAssemblyJavascriptGenerator.js +++ b/lib/wasm/WebAssemblyJavascriptGenerator.js @@ -28,6 +28,15 @@ class WebAssemblyJavascriptGenerator extends Generator { return TYPES; } + /** + * @param {NormalModule} module the module + * @param {string=} type source type + * @returns {number} estimate size of the module + */ + getSize(module, type) { + return 100 + module.dependencies.length * 5; + } + /** * @param {NormalModule} module module for which the code should be generated * @param {GenerateContext} generateContext context for generate