UNPKG

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