| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { JAVASCRIPT_TYPE } = require("./ModuleSourceTypeConstants");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("webpack-sources").Source} Source */
|
|---|
| 11 | /** @typedef {import("./ChunkGraph")} ChunkGraph */
|
|---|
| 12 | /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
|
|---|
| 13 | /** @typedef {import("./ConcatenationScope")} ConcatenationScope */
|
|---|
| 14 | /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
|
|---|
| 15 | /** @typedef {import("./Module").CodeGenerationResultData} CodeGenerationResultData */
|
|---|
| 16 | /** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
|
|---|
| 17 | /** @typedef {import("./Module").RuntimeRequirements} RuntimeRequirements */
|
|---|
| 18 | /** @typedef {import("./Module").SourceType} SourceType */
|
|---|
| 19 | /** @typedef {import("./Module").SourceTypes} SourceTypes */
|
|---|
| 20 | /** @typedef {import("./ModuleGraph")} ModuleGraph */
|
|---|
| 21 | /** @typedef {import("./NormalModule")} NormalModule */
|
|---|
| 22 | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
|---|
| 23 | /** @typedef {import("./util/Hash")} Hash */
|
|---|
| 24 | /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Defines the generate context type used by this module.
|
|---|
| 28 | * @typedef {object} GenerateContext
|
|---|
| 29 | * @property {DependencyTemplates} dependencyTemplates mapping from dependencies to templates
|
|---|
| 30 | * @property {RuntimeTemplate} runtimeTemplate the runtime template
|
|---|
| 31 | * @property {ModuleGraph} moduleGraph the module graph
|
|---|
| 32 | * @property {ChunkGraph} chunkGraph the chunk graph
|
|---|
| 33 | * @property {RuntimeRequirements} runtimeRequirements the requirements for runtime
|
|---|
| 34 | * @property {RuntimeSpec} runtime the runtime
|
|---|
| 35 | * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
|
|---|
| 36 | * @property {CodeGenerationResults=} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
|
|---|
| 37 | * @property {SourceType} type which kind of code should be generated
|
|---|
| 38 | * @property {() => CodeGenerationResultData=} getData get access to the code generation data
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Defines the generate error fn callback.
|
|---|
| 43 | * @callback GenerateErrorFn
|
|---|
| 44 | * @param {Error} error the error
|
|---|
| 45 | * @param {NormalModule} module module for which the code should be generated
|
|---|
| 46 | * @param {GenerateContext} generateContext context for generate
|
|---|
| 47 | * @returns {Source | null} generated code
|
|---|
| 48 | */
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Represents the generator runtime component.
|
|---|
| 52 | * @typedef {object} UpdateHashContext
|
|---|
| 53 | * @property {NormalModule} module the module
|
|---|
| 54 | * @property {ChunkGraph} chunkGraph
|
|---|
| 55 | * @property {RuntimeSpec} runtime
|
|---|
| 56 | * @property {RuntimeTemplate=} runtimeTemplate
|
|---|
| 57 | */
|
|---|
| 58 |
|
|---|
| 59 | class Generator {
|
|---|
| 60 | /**
|
|---|
| 61 | * Returns generator by type.
|
|---|
| 62 | * @param {{ [key in SourceType]?: Generator }} map map of types
|
|---|
| 63 | * @returns {ByTypeGenerator} generator by type
|
|---|
| 64 | */
|
|---|
| 65 | static byType(map) {
|
|---|
| 66 | return new ByTypeGenerator(map);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /* istanbul ignore next */
|
|---|
| 70 | /**
|
|---|
| 71 | * Returns the source types available for this module.
|
|---|
| 72 | * @abstract
|
|---|
| 73 | * @param {NormalModule} module fresh module
|
|---|
| 74 | * @returns {SourceTypes} available types (do not mutate)
|
|---|
| 75 | */
|
|---|
| 76 | getTypes(module) {
|
|---|
| 77 | const AbstractMethodError = require("./errors/AbstractMethodError");
|
|---|
| 78 |
|
|---|
| 79 | throw new AbstractMethodError();
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /* istanbul ignore next */
|
|---|
| 83 | /**
|
|---|
| 84 | * Returns the estimated size for the requested source type.
|
|---|
| 85 | * @abstract
|
|---|
| 86 | * @param {NormalModule} module the module
|
|---|
| 87 | * @param {SourceType=} type source type
|
|---|
| 88 | * @returns {number} estimate size of the module
|
|---|
| 89 | */
|
|---|
| 90 | getSize(module, type) {
|
|---|
| 91 | const AbstractMethodError = require("./errors/AbstractMethodError");
|
|---|
| 92 |
|
|---|
| 93 | throw new AbstractMethodError();
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /* istanbul ignore next */
|
|---|
| 97 | /**
|
|---|
| 98 | * Generates generated code for this runtime module.
|
|---|
| 99 | * @abstract
|
|---|
| 100 | * @param {NormalModule} module module for which the code should be generated
|
|---|
| 101 | * @param {GenerateContext} generateContext context for generate
|
|---|
| 102 | * @returns {Source | null} generated code
|
|---|
| 103 | */
|
|---|
| 104 | generate(
|
|---|
| 105 | module,
|
|---|
| 106 | { dependencyTemplates, runtimeTemplate, moduleGraph, type }
|
|---|
| 107 | ) {
|
|---|
| 108 | const AbstractMethodError = require("./errors/AbstractMethodError");
|
|---|
| 109 |
|
|---|
| 110 | throw new AbstractMethodError();
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * Returns the reason this module cannot be concatenated, when one exists.
|
|---|
| 115 | * @param {NormalModule} module module for which the bailout reason should be determined
|
|---|
| 116 | * @param {ConcatenationBailoutReasonContext} context context
|
|---|
| 117 | * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
|
|---|
| 118 | */
|
|---|
| 119 | getConcatenationBailoutReason(module, context) {
|
|---|
| 120 | return `Module Concatenation is not implemented for ${this.constructor.name}`;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * Updates the hash with the data contributed by this instance.
|
|---|
| 125 | * @param {Hash} hash hash that will be modified
|
|---|
| 126 | * @param {UpdateHashContext} updateHashContext context for updating hash
|
|---|
| 127 | */
|
|---|
| 128 | updateHash(hash, { module, runtime }) {
|
|---|
| 129 | // no nothing
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * @this {ByTypeGenerator}
|
|---|
| 135 | * @type {GenerateErrorFn}
|
|---|
| 136 | */
|
|---|
| 137 | function generateError(error, module, generateContext) {
|
|---|
| 138 | const type = generateContext.type;
|
|---|
| 139 | const generator =
|
|---|
| 140 | /** @type {Generator & { generateError?: GenerateErrorFn }} */
|
|---|
| 141 | (this.map[type]);
|
|---|
| 142 | if (!generator) {
|
|---|
| 143 | throw new Error(`Generator.byType: no generator specified for ${type}`);
|
|---|
| 144 | }
|
|---|
| 145 | if (typeof generator.generateError === "undefined") {
|
|---|
| 146 | return null;
|
|---|
| 147 | }
|
|---|
| 148 | return generator.generateError(error, module, generateContext);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | class ByTypeGenerator extends Generator {
|
|---|
| 152 | /**
|
|---|
| 153 | * Creates an instance of ByTypeGenerator.
|
|---|
| 154 | * @param {{ [key in SourceType]?: Generator }} map map of types
|
|---|
| 155 | */
|
|---|
| 156 | constructor(map) {
|
|---|
| 157 | super();
|
|---|
| 158 | this.map = map;
|
|---|
| 159 | this._types = /** @type {SourceTypes} */ (new Set(Object.keys(map)));
|
|---|
| 160 | /** @type {GenerateErrorFn | undefined} */
|
|---|
| 161 | this.generateError = generateError.bind(this);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /**
|
|---|
| 165 | * Returns the source types available for this module.
|
|---|
| 166 | * @param {NormalModule} module fresh module
|
|---|
| 167 | * @returns {SourceTypes} available types (do not mutate)
|
|---|
| 168 | */
|
|---|
| 169 | getTypes(module) {
|
|---|
| 170 | return this._types;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /**
|
|---|
| 174 | * Returns the estimated size for the requested source type.
|
|---|
| 175 | * @param {NormalModule} module the module
|
|---|
| 176 | * @param {SourceType=} type source type
|
|---|
| 177 | * @returns {number} estimate size of the module
|
|---|
| 178 | */
|
|---|
| 179 | getSize(module, type = JAVASCRIPT_TYPE) {
|
|---|
| 180 | const t = type;
|
|---|
| 181 | const generator = this.map[t];
|
|---|
| 182 | return generator ? generator.getSize(module, t) : 0;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /**
|
|---|
| 186 | * Generates generated code for this runtime module.
|
|---|
| 187 | * @param {NormalModule} module module for which the code should be generated
|
|---|
| 188 | * @param {GenerateContext} generateContext context for generate
|
|---|
| 189 | * @returns {Source | null} generated code
|
|---|
| 190 | */
|
|---|
| 191 | generate(module, generateContext) {
|
|---|
| 192 | const type = generateContext.type;
|
|---|
| 193 | const generator = this.map[type];
|
|---|
| 194 | if (!generator) {
|
|---|
| 195 | throw new Error(`Generator.byType: no generator specified for ${type}`);
|
|---|
| 196 | }
|
|---|
| 197 | return generator.generate(module, generateContext);
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | module.exports = Generator;
|
|---|