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