[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 9 | /** @typedef {import("./ChunkGraph")} ChunkGraph */
|
---|
| 10 | /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
|
---|
| 11 | /** @typedef {import("./Compilation")} Compilation */
|
---|
| 12 | /** @typedef {import("./ConcatenationScope")} ConcatenationScope */
|
---|
| 13 | /** @typedef {import("./DependencyTemplate")} DependencyTemplate */
|
---|
| 14 | /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
|
---|
| 15 | /** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
|
---|
| 16 | /** @typedef {import("./Module").RuntimeRequirements} RuntimeRequirements */
|
---|
| 17 | /** @typedef {import("./Module").SourceTypes} SourceTypes */
|
---|
| 18 | /** @typedef {import("./ModuleGraph")} ModuleGraph */
|
---|
| 19 | /** @typedef {import("./NormalModule")} NormalModule */
|
---|
| 20 | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
---|
| 21 | /** @typedef {import("./util/Hash")} Hash */
|
---|
| 22 | /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * @typedef {object} GenerateContext
|
---|
| 26 | * @property {DependencyTemplates} dependencyTemplates mapping from dependencies to templates
|
---|
| 27 | * @property {RuntimeTemplate} runtimeTemplate the runtime template
|
---|
| 28 | * @property {ModuleGraph} moduleGraph the module graph
|
---|
| 29 | * @property {ChunkGraph} chunkGraph the chunk graph
|
---|
| 30 | * @property {RuntimeRequirements} runtimeRequirements the requirements for runtime
|
---|
| 31 | * @property {RuntimeSpec} runtime the runtime
|
---|
| 32 | * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
|
---|
| 33 | * @property {CodeGenerationResults=} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
|
---|
| 34 | * @property {string} type which kind of code should be generated
|
---|
| 35 | * @property {function(): Map<string, any>=} getData get access to the code generation data
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * @typedef {object} UpdateHashContext
|
---|
| 40 | * @property {NormalModule} module the module
|
---|
| 41 | * @property {ChunkGraph} chunkGraph
|
---|
| 42 | * @property {RuntimeSpec} runtime
|
---|
| 43 | * @property {RuntimeTemplate=} runtimeTemplate
|
---|
| 44 | */
|
---|
| 45 |
|
---|
| 46 | class Generator {
|
---|
| 47 | /**
|
---|
| 48 | * @param {Record<string, Generator>} map map of types
|
---|
| 49 | * @returns {ByTypeGenerator} generator by type
|
---|
| 50 | */
|
---|
| 51 | static byType(map) {
|
---|
| 52 | return new ByTypeGenerator(map);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /* istanbul ignore next */
|
---|
| 56 | /**
|
---|
| 57 | * @abstract
|
---|
| 58 | * @param {NormalModule} module fresh module
|
---|
| 59 | * @returns {SourceTypes} available types (do not mutate)
|
---|
| 60 | */
|
---|
| 61 | getTypes(module) {
|
---|
| 62 | const AbstractMethodError = require("./AbstractMethodError");
|
---|
| 63 | throw new AbstractMethodError();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /* istanbul ignore next */
|
---|
| 67 | /**
|
---|
| 68 | * @abstract
|
---|
| 69 | * @param {NormalModule} module the module
|
---|
| 70 | * @param {string=} type source type
|
---|
| 71 | * @returns {number} estimate size of the module
|
---|
| 72 | */
|
---|
| 73 | getSize(module, type) {
|
---|
| 74 | const AbstractMethodError = require("./AbstractMethodError");
|
---|
| 75 | throw new AbstractMethodError();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /* istanbul ignore next */
|
---|
| 79 | /**
|
---|
| 80 | * @abstract
|
---|
| 81 | * @param {NormalModule} module module for which the code should be generated
|
---|
| 82 | * @param {GenerateContext} generateContext context for generate
|
---|
| 83 | * @returns {Source | null} generated code
|
---|
| 84 | */
|
---|
| 85 | generate(
|
---|
| 86 | module,
|
---|
| 87 | { dependencyTemplates, runtimeTemplate, moduleGraph, type }
|
---|
| 88 | ) {
|
---|
| 89 | const AbstractMethodError = require("./AbstractMethodError");
|
---|
| 90 | throw new AbstractMethodError();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * @param {NormalModule} module module for which the bailout reason should be determined
|
---|
| 95 | * @param {ConcatenationBailoutReasonContext} context context
|
---|
| 96 | * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
|
---|
| 97 | */
|
---|
| 98 | getConcatenationBailoutReason(module, context) {
|
---|
| 99 | return `Module Concatenation is not implemented for ${this.constructor.name}`;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * @param {Hash} hash hash that will be modified
|
---|
| 104 | * @param {UpdateHashContext} updateHashContext context for updating hash
|
---|
| 105 | */
|
---|
| 106 | updateHash(hash, { module, runtime }) {
|
---|
| 107 | // no nothing
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | class ByTypeGenerator extends Generator {
|
---|
| 112 | /**
|
---|
| 113 | * @param {Record<string, Generator>} map map of types
|
---|
| 114 | */
|
---|
| 115 | constructor(map) {
|
---|
| 116 | super();
|
---|
| 117 | this.map = map;
|
---|
| 118 | this._types = new Set(Object.keys(map));
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
| 122 | * @param {NormalModule} module fresh module
|
---|
| 123 | * @returns {SourceTypes} available types (do not mutate)
|
---|
| 124 | */
|
---|
| 125 | getTypes(module) {
|
---|
| 126 | return this._types;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
| 130 | * @param {NormalModule} module the module
|
---|
| 131 | * @param {string=} type source type
|
---|
| 132 | * @returns {number} estimate size of the module
|
---|
| 133 | */
|
---|
| 134 | getSize(module, type = "javascript") {
|
---|
| 135 | const t = type;
|
---|
| 136 | const generator = this.map[t];
|
---|
| 137 | return generator ? generator.getSize(module, t) : 0;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
| 141 | * @param {NormalModule} module module for which the code should be generated
|
---|
| 142 | * @param {GenerateContext} generateContext context for generate
|
---|
| 143 | * @returns {Source | null} generated code
|
---|
| 144 | */
|
---|
| 145 | generate(module, generateContext) {
|
---|
| 146 | const type = generateContext.type;
|
---|
| 147 | const generator = this.map[type];
|
---|
| 148 | if (!generator) {
|
---|
| 149 | throw new Error(`Generator.byType: no generator specified for ${type}`);
|
---|
| 150 | }
|
---|
| 151 | return generator.generate(module, generateContext);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | module.exports = Generator;
|
---|