| 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 {
|
|---|
| 12 | JAVASCRIPT_TYPES,
|
|---|
| 13 | RUNTIME_TYPES
|
|---|
| 14 | } = require("./ModuleSourceTypeConstants");
|
|---|
| 15 | const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./ModuleTypeConstants");
|
|---|
| 16 |
|
|---|
| 17 | /** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
|
|---|
| 18 | /** @typedef {import("./Chunk")} Chunk */
|
|---|
| 19 | /** @typedef {import("./ChunkGraph")} ChunkGraph */
|
|---|
| 20 | /** @typedef {import("./Compilation")} Compilation */
|
|---|
| 21 | /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 22 | /** @typedef {import("./Generator").SourceTypes} SourceTypes */
|
|---|
| 23 | /** @typedef {import("./Module").BuildMeta} BuildMeta */
|
|---|
| 24 | /** @typedef {import("./Module").BuildInfo} BuildInfo */
|
|---|
| 25 | /** @typedef {import("./Module").BuildCallback} BuildCallback */
|
|---|
| 26 | /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
|
|---|
| 27 | /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
|
|---|
| 28 | /** @typedef {import("./Module").NeedBuildCallback} NeedBuildCallback */
|
|---|
| 29 | /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
|
|---|
| 30 | /** @typedef {import("./Module").Sources} Sources */
|
|---|
| 31 | /** @typedef {import("./RequestShortener")} RequestShortener */
|
|---|
| 32 | /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
|---|
| 33 | /** @typedef {import("./util/Hash")} Hash */
|
|---|
| 34 | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 35 | /** @typedef {import("./Module").BasicSourceTypes} BasicSourceTypes */
|
|---|
| 36 |
|
|---|
| 37 | class RuntimeModule extends Module {
|
|---|
| 38 | /**
|
|---|
| 39 | * Creates an instance of RuntimeModule.
|
|---|
| 40 | * @param {string} name a readable name
|
|---|
| 41 | * @param {number=} stage an optional stage
|
|---|
| 42 | */
|
|---|
| 43 | constructor(name, stage = 0) {
|
|---|
| 44 | super(WEBPACK_MODULE_TYPE_RUNTIME);
|
|---|
| 45 | /** @type {string} */
|
|---|
| 46 | this.name = name;
|
|---|
| 47 | /** @type {number} */
|
|---|
| 48 | this.stage = stage;
|
|---|
| 49 | /** @type {BuildMeta} */
|
|---|
| 50 | this.buildMeta = {};
|
|---|
| 51 | /** @type {BuildInfo} */
|
|---|
| 52 | this.buildInfo = {};
|
|---|
| 53 | /** @type {Compilation | undefined} */
|
|---|
| 54 | this.compilation = undefined;
|
|---|
| 55 | /** @type {Chunk | undefined} */
|
|---|
| 56 | this.chunk = undefined;
|
|---|
| 57 | /** @type {ChunkGraph | undefined} */
|
|---|
| 58 | this.chunkGraph = undefined;
|
|---|
| 59 | /** @type {boolean} */
|
|---|
| 60 | this.fullHash = false;
|
|---|
| 61 | /** @type {boolean} */
|
|---|
| 62 | this.dependentHash = false;
|
|---|
| 63 | /** @type {string | undefined | null} */
|
|---|
| 64 | this._cachedGeneratedCode = undefined;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Processes the provided compilation.
|
|---|
| 69 | * @param {Compilation} compilation the compilation
|
|---|
| 70 | * @param {Chunk} chunk the chunk
|
|---|
| 71 | * @param {ChunkGraph} chunkGraph the chunk graph
|
|---|
| 72 | * @returns {void}
|
|---|
| 73 | */
|
|---|
| 74 | attach(compilation, chunk, chunkGraph = compilation.chunkGraph) {
|
|---|
| 75 | this.compilation = compilation;
|
|---|
| 76 | this.chunk = chunk;
|
|---|
| 77 | this.chunkGraph = chunkGraph;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Returns the unique identifier used to reference this module.
|
|---|
| 82 | * @returns {string} a unique identifier of the module
|
|---|
| 83 | */
|
|---|
| 84 | identifier() {
|
|---|
| 85 | return `webpack/runtime/${this.name}`;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Returns a human-readable identifier for this module.
|
|---|
| 90 | * @param {RequestShortener} requestShortener the request shortener
|
|---|
| 91 | * @returns {string} a user readable identifier of the module
|
|---|
| 92 | */
|
|---|
| 93 | readableIdentifier(requestShortener) {
|
|---|
| 94 | return `webpack/runtime/${this.name}`;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Checks whether the module needs to be rebuilt for the current build state.
|
|---|
| 99 | * @param {NeedBuildContext} context context info
|
|---|
| 100 | * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
|
|---|
| 101 | * @returns {void}
|
|---|
| 102 | */
|
|---|
| 103 | needBuild(context, callback) {
|
|---|
| 104 | return callback(null, false);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * Builds the module using the provided compilation context.
|
|---|
| 109 | * @param {WebpackOptions} options webpack options
|
|---|
| 110 | * @param {Compilation} compilation the compilation
|
|---|
| 111 | * @param {ResolverWithOptions} resolver the resolver
|
|---|
| 112 | * @param {InputFileSystem} fs the file system
|
|---|
| 113 | * @param {BuildCallback} callback callback function
|
|---|
| 114 | * @returns {void}
|
|---|
| 115 | */
|
|---|
| 116 | build(options, compilation, resolver, fs, callback) {
|
|---|
| 117 | // do nothing
|
|---|
| 118 | // should not be called as runtime modules are added later to the compilation
|
|---|
| 119 | callback();
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Updates the hash with the data contributed by this instance.
|
|---|
| 124 | * @param {Hash} hash the hash used to track dependencies
|
|---|
| 125 | * @param {UpdateHashContext} context context
|
|---|
| 126 | * @returns {void}
|
|---|
| 127 | */
|
|---|
| 128 | updateHash(hash, context) {
|
|---|
| 129 | hash.update(this.name);
|
|---|
| 130 | hash.update(`${this.stage}`);
|
|---|
| 131 | try {
|
|---|
| 132 | const code =
|
|---|
| 133 | this.fullHash || this.dependentHash
|
|---|
| 134 | ? // Do not use getGeneratedCode here, because i. e. compilation hash might be not
|
|---|
| 135 | // ready at this point. We will cache it later instead.
|
|---|
| 136 | this.generate()
|
|---|
| 137 | : this.getGeneratedCode();
|
|---|
| 138 | if (code !== null && code !== undefined) {
|
|---|
| 139 | hash.update(code);
|
|---|
| 140 | }
|
|---|
| 141 | } catch (err) {
|
|---|
| 142 | hash.update(/** @type {Error} */ (err).message);
|
|---|
| 143 | }
|
|---|
| 144 | super.updateHash(hash, context);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * Returns the source types this module can generate.
|
|---|
| 149 | * @returns {SourceTypes} types available (do not mutate)
|
|---|
| 150 | */
|
|---|
| 151 | getSourceTypes() {
|
|---|
| 152 | return RUNTIME_TYPES;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * Basic source types are high-level categories like javascript, css, webassembly, etc.
|
|---|
| 157 | * We only have built-in knowledge about the javascript basic type here; other basic types may be
|
|---|
| 158 | * added or changed over time by generators and do not need to be handled or detected here.
|
|---|
| 159 | *
|
|---|
| 160 | * Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
|
|---|
| 161 | * from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
|
|---|
| 162 | * @returns {BasicSourceTypes} types available (do not mutate)
|
|---|
| 163 | */
|
|---|
| 164 | getSourceBasicTypes() {
|
|---|
| 165 | return JAVASCRIPT_TYPES;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Generates code and runtime requirements for this module.
|
|---|
| 170 | * @param {CodeGenerationContext} context context for code generation
|
|---|
| 171 | * @returns {CodeGenerationResult} result
|
|---|
| 172 | */
|
|---|
| 173 | codeGeneration(context) {
|
|---|
| 174 | /** @type {Sources} */
|
|---|
| 175 | const sources = new Map();
|
|---|
| 176 | const generatedCode = this.getGeneratedCode();
|
|---|
| 177 | if (generatedCode) {
|
|---|
| 178 | sources.set(
|
|---|
| 179 | WEBPACK_MODULE_TYPE_RUNTIME,
|
|---|
| 180 | this.useSourceMap || this.useSimpleSourceMap
|
|---|
| 181 | ? new OriginalSource(generatedCode, this.identifier())
|
|---|
| 182 | : new RawSource(generatedCode)
|
|---|
| 183 | );
|
|---|
| 184 | }
|
|---|
| 185 | return {
|
|---|
| 186 | sources,
|
|---|
| 187 | runtimeRequirements: null
|
|---|
| 188 | };
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /**
|
|---|
| 192 | * Returns the estimated size for the requested source type.
|
|---|
| 193 | * @param {string=} type the source type for which the size should be estimated
|
|---|
| 194 | * @returns {number} the estimated size of the module (must be non-zero)
|
|---|
| 195 | */
|
|---|
| 196 | size(type) {
|
|---|
| 197 | try {
|
|---|
| 198 | const source = this.getGeneratedCode();
|
|---|
| 199 | return source ? source.length : 0;
|
|---|
| 200 | } catch (_err) {
|
|---|
| 201 | return 0;
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /* istanbul ignore next */
|
|---|
| 206 | /**
|
|---|
| 207 | * Generates runtime code for this runtime module.
|
|---|
| 208 | * @abstract
|
|---|
| 209 | * @returns {string | null} runtime code
|
|---|
| 210 | */
|
|---|
| 211 | generate() {
|
|---|
| 212 | const AbstractMethodError = require("./errors/AbstractMethodError");
|
|---|
| 213 |
|
|---|
| 214 | throw new AbstractMethodError();
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Gets generated code.
|
|---|
| 219 | * @returns {string | null} runtime code
|
|---|
| 220 | */
|
|---|
| 221 | getGeneratedCode() {
|
|---|
| 222 | if (this._cachedGeneratedCode) {
|
|---|
| 223 | return this._cachedGeneratedCode;
|
|---|
| 224 | }
|
|---|
| 225 | return (this._cachedGeneratedCode = this.generate());
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /**
|
|---|
| 229 | * Returns true, if the runtime module should get it's own scope.
|
|---|
| 230 | * @returns {boolean} true, if the runtime module should get it's own scope
|
|---|
| 231 | */
|
|---|
| 232 | shouldIsolate() {
|
|---|
| 233 | return true;
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /**
|
|---|
| 238 | * Runtime modules without any dependencies to other runtime modules
|
|---|
| 239 | */
|
|---|
| 240 | RuntimeModule.STAGE_NORMAL = 0;
|
|---|
| 241 |
|
|---|
| 242 | /**
|
|---|
| 243 | * Runtime modules with simple dependencies on other runtime modules
|
|---|
| 244 | */
|
|---|
| 245 | RuntimeModule.STAGE_BASIC = 5;
|
|---|
| 246 |
|
|---|
| 247 | /**
|
|---|
| 248 | * Runtime modules which attach to handlers of other runtime modules
|
|---|
| 249 | */
|
|---|
| 250 | RuntimeModule.STAGE_ATTACH = 10;
|
|---|
| 251 |
|
|---|
| 252 | /**
|
|---|
| 253 | * Runtime modules which trigger actions on bootstrap
|
|---|
| 254 | */
|
|---|
| 255 | RuntimeModule.STAGE_TRIGGER = 20;
|
|---|
| 256 |
|
|---|
| 257 | module.exports = RuntimeModule;
|
|---|