[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 | const { RawSource } = require("webpack-sources");
|
---|
| 9 | const OriginalSource = require("webpack-sources").OriginalSource;
|
---|
| 10 | const Module = require("./Module");
|
---|
| 11 | const { RUNTIME_TYPES } = require("./ModuleSourceTypesConstants");
|
---|
| 12 | const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
|
---|
| 13 |
|
---|
| 14 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 15 | /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
|
---|
| 16 | /** @typedef {import("./Chunk")} Chunk */
|
---|
| 17 | /** @typedef {import("./ChunkGraph")} ChunkGraph */
|
---|
| 18 | /** @typedef {import("./Compilation")} Compilation */
|
---|
| 19 | /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 20 | /** @typedef {import("./Generator").SourceTypes} SourceTypes */
|
---|
| 21 | /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
|
---|
| 22 | /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
|
---|
| 23 | /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
|
---|
| 24 | /** @typedef {import("./RequestShortener")} RequestShortener */
|
---|
| 25 | /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
---|
| 26 | /** @typedef {import("./WebpackError")} WebpackError */
|
---|
| 27 | /** @typedef {import("./util/Hash")} Hash */
|
---|
| 28 | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
---|
| 29 |
|
---|
| 30 | class RuntimeModule extends Module {
|
---|
| 31 | /**
|
---|
| 32 | * @param {string} name a readable name
|
---|
| 33 | * @param {number=} stage an optional stage
|
---|
| 34 | */
|
---|
| 35 | constructor(name, stage = 0) {
|
---|
| 36 | super(WEBPACK_MODULE_TYPE_RUNTIME);
|
---|
| 37 | this.name = name;
|
---|
| 38 | this.stage = stage;
|
---|
| 39 | this.buildMeta = {};
|
---|
| 40 | this.buildInfo = {};
|
---|
| 41 | /** @type {Compilation | undefined} */
|
---|
| 42 | this.compilation = undefined;
|
---|
| 43 | /** @type {Chunk | undefined} */
|
---|
| 44 | this.chunk = undefined;
|
---|
| 45 | /** @type {ChunkGraph | undefined} */
|
---|
| 46 | this.chunkGraph = undefined;
|
---|
| 47 | this.fullHash = false;
|
---|
| 48 | this.dependentHash = false;
|
---|
| 49 | /** @type {string | undefined | null} */
|
---|
| 50 | this._cachedGeneratedCode = undefined;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * @param {Compilation} compilation the compilation
|
---|
| 55 | * @param {Chunk} chunk the chunk
|
---|
| 56 | * @param {ChunkGraph} chunkGraph the chunk graph
|
---|
| 57 | * @returns {void}
|
---|
| 58 | */
|
---|
| 59 | attach(compilation, chunk, chunkGraph = compilation.chunkGraph) {
|
---|
| 60 | this.compilation = compilation;
|
---|
| 61 | this.chunk = chunk;
|
---|
| 62 | this.chunkGraph = chunkGraph;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * @returns {string} a unique identifier of the module
|
---|
| 67 | */
|
---|
| 68 | identifier() {
|
---|
| 69 | return `webpack/runtime/${this.name}`;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * @param {RequestShortener} requestShortener the request shortener
|
---|
| 74 | * @returns {string} a user readable identifier of the module
|
---|
| 75 | */
|
---|
| 76 | readableIdentifier(requestShortener) {
|
---|
| 77 | return `webpack/runtime/${this.name}`;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * @param {NeedBuildContext} context context info
|
---|
| 82 | * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
|
---|
| 83 | * @returns {void}
|
---|
| 84 | */
|
---|
| 85 | needBuild(context, callback) {
|
---|
| 86 | return callback(null, false);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * @param {WebpackOptions} options webpack options
|
---|
| 91 | * @param {Compilation} compilation the compilation
|
---|
| 92 | * @param {ResolverWithOptions} resolver the resolver
|
---|
| 93 | * @param {InputFileSystem} fs the file system
|
---|
| 94 | * @param {function(WebpackError=): void} callback callback function
|
---|
| 95 | * @returns {void}
|
---|
| 96 | */
|
---|
| 97 | build(options, compilation, resolver, fs, callback) {
|
---|
| 98 | // do nothing
|
---|
| 99 | // should not be called as runtime modules are added later to the compilation
|
---|
| 100 | callback();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * @param {Hash} hash the hash used to track dependencies
|
---|
| 105 | * @param {UpdateHashContext} context context
|
---|
| 106 | * @returns {void}
|
---|
| 107 | */
|
---|
| 108 | updateHash(hash, context) {
|
---|
| 109 | hash.update(this.name);
|
---|
| 110 | hash.update(`${this.stage}`);
|
---|
| 111 | try {
|
---|
| 112 | if (this.fullHash || this.dependentHash) {
|
---|
| 113 | // Do not use getGeneratedCode here, because i. e. compilation hash might be not
|
---|
| 114 | // ready at this point. We will cache it later instead.
|
---|
| 115 | hash.update(/** @type {string} */ (this.generate()));
|
---|
| 116 | } else {
|
---|
| 117 | hash.update(/** @type {string} */ (this.getGeneratedCode()));
|
---|
| 118 | }
|
---|
| 119 | } catch (err) {
|
---|
| 120 | hash.update(/** @type {Error} */ (err).message);
|
---|
| 121 | }
|
---|
| 122 | super.updateHash(hash, context);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * @returns {SourceTypes} types available (do not mutate)
|
---|
| 127 | */
|
---|
| 128 | getSourceTypes() {
|
---|
| 129 | return RUNTIME_TYPES;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * @param {CodeGenerationContext} context context for code generation
|
---|
| 134 | * @returns {CodeGenerationResult} result
|
---|
| 135 | */
|
---|
| 136 | codeGeneration(context) {
|
---|
| 137 | const sources = new Map();
|
---|
| 138 | const generatedCode = this.getGeneratedCode();
|
---|
| 139 | if (generatedCode) {
|
---|
| 140 | sources.set(
|
---|
| 141 | WEBPACK_MODULE_TYPE_RUNTIME,
|
---|
| 142 | this.useSourceMap || this.useSimpleSourceMap
|
---|
| 143 | ? new OriginalSource(generatedCode, this.identifier())
|
---|
| 144 | : new RawSource(generatedCode)
|
---|
| 145 | );
|
---|
| 146 | }
|
---|
| 147 | return {
|
---|
| 148 | sources,
|
---|
| 149 | runtimeRequirements: null
|
---|
| 150 | };
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
| 154 | * @param {string=} type the source type for which the size should be estimated
|
---|
| 155 | * @returns {number} the estimated size of the module (must be non-zero)
|
---|
| 156 | */
|
---|
| 157 | size(type) {
|
---|
| 158 | try {
|
---|
| 159 | const source = this.getGeneratedCode();
|
---|
| 160 | return source ? source.length : 0;
|
---|
| 161 | } catch (_err) {
|
---|
| 162 | return 0;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /* istanbul ignore next */
|
---|
| 167 | /**
|
---|
| 168 | * @abstract
|
---|
| 169 | * @returns {string | null} runtime code
|
---|
| 170 | */
|
---|
| 171 | generate() {
|
---|
| 172 | const AbstractMethodError = require("./AbstractMethodError");
|
---|
| 173 | throw new AbstractMethodError();
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * @returns {string | null} runtime code
|
---|
| 178 | */
|
---|
| 179 | getGeneratedCode() {
|
---|
| 180 | if (this._cachedGeneratedCode) {
|
---|
| 181 | return this._cachedGeneratedCode;
|
---|
| 182 | }
|
---|
| 183 | return (this._cachedGeneratedCode = this.generate());
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
| 187 | * @returns {boolean} true, if the runtime module should get it's own scope
|
---|
| 188 | */
|
---|
| 189 | shouldIsolate() {
|
---|
| 190 | return true;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /**
|
---|
| 195 | * Runtime modules without any dependencies to other runtime modules
|
---|
| 196 | */
|
---|
| 197 | RuntimeModule.STAGE_NORMAL = 0;
|
---|
| 198 |
|
---|
| 199 | /**
|
---|
| 200 | * Runtime modules with simple dependencies on other runtime modules
|
---|
| 201 | */
|
---|
| 202 | RuntimeModule.STAGE_BASIC = 5;
|
---|
| 203 |
|
---|
| 204 | /**
|
---|
| 205 | * Runtime modules which attach to handlers of other runtime modules
|
---|
| 206 | */
|
---|
| 207 | RuntimeModule.STAGE_ATTACH = 10;
|
---|
| 208 |
|
---|
| 209 | /**
|
---|
| 210 | * Runtime modules which trigger actions on bootstrap
|
---|
| 211 | */
|
---|
| 212 | RuntimeModule.STAGE_TRIGGER = 20;
|
---|
| 213 |
|
---|
| 214 | module.exports = RuntimeModule;
|
---|