add type argument to Module.size

add getSize to Generator
This commit is contained in:
Tobias Koppers 2018-12-04 11:23:40 +01:00
parent dff9278d6b
commit cc34ea42b0
14 changed files with 87 additions and 11 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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");
}

View File

@ -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);
}
/**

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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

View File

@ -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