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 NormalModule = require("../NormalModule");
|
---|
9 | const LazySet = require("../util/LazySet");
|
---|
10 | const LoaderDependency = require("./LoaderDependency");
|
---|
11 | const LoaderImportDependency = require("./LoaderImportDependency");
|
---|
12 |
|
---|
13 | /** @typedef {import("../../declarations/LoaderContext").LoaderPluginLoaderContext} LoaderPluginLoaderContext */
|
---|
14 | /** @typedef {import("../Compilation").DepConstructor} DepConstructor */
|
---|
15 | /** @typedef {import("../Compilation").ExecuteModuleResult} ExecuteModuleResult */
|
---|
16 | /** @typedef {import("../Compiler")} Compiler */
|
---|
17 | /** @typedef {import("../Module")} Module */
|
---|
18 | /** @typedef {import("../Module").BuildInfo} BuildInfo */
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * @callback ImportModuleCallback
|
---|
22 | * @param {(Error | null)=} err error object
|
---|
23 | * @param {any=} exports exports of the evaluated module
|
---|
24 | */
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * @typedef {object} ImportModuleOptions
|
---|
28 | * @property {string=} layer the target layer
|
---|
29 | * @property {string=} publicPath the target public path
|
---|
30 | * @property {string=} baseUri target base uri
|
---|
31 | */
|
---|
32 |
|
---|
33 | class LoaderPlugin {
|
---|
34 | /**
|
---|
35 | * @param {object} options options
|
---|
36 | */
|
---|
37 | constructor(options = {}) {}
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Apply the plugin
|
---|
41 | * @param {Compiler} compiler the compiler instance
|
---|
42 | * @returns {void}
|
---|
43 | */
|
---|
44 | apply(compiler) {
|
---|
45 | compiler.hooks.compilation.tap(
|
---|
46 | "LoaderPlugin",
|
---|
47 | (compilation, { normalModuleFactory }) => {
|
---|
48 | compilation.dependencyFactories.set(
|
---|
49 | LoaderDependency,
|
---|
50 | normalModuleFactory
|
---|
51 | );
|
---|
52 | compilation.dependencyFactories.set(
|
---|
53 | LoaderImportDependency,
|
---|
54 | normalModuleFactory
|
---|
55 | );
|
---|
56 | }
|
---|
57 | );
|
---|
58 |
|
---|
59 | compiler.hooks.compilation.tap("LoaderPlugin", compilation => {
|
---|
60 | const moduleGraph = compilation.moduleGraph;
|
---|
61 | NormalModule.getCompilationHooks(compilation).loader.tap(
|
---|
62 | "LoaderPlugin",
|
---|
63 | loaderContext => {
|
---|
64 | loaderContext.loadModule = (request, callback) => {
|
---|
65 | const dep = new LoaderDependency(request);
|
---|
66 | dep.loc = {
|
---|
67 | name: request
|
---|
68 | };
|
---|
69 | const factory = compilation.dependencyFactories.get(
|
---|
70 | /** @type {DepConstructor} */ (dep.constructor)
|
---|
71 | );
|
---|
72 | if (factory === undefined) {
|
---|
73 | return callback(
|
---|
74 | new Error(
|
---|
75 | `No module factory available for dependency type: ${dep.constructor.name}`
|
---|
76 | )
|
---|
77 | );
|
---|
78 | }
|
---|
79 | const oldFactorizeQueueContext =
|
---|
80 | compilation.factorizeQueue.getContext();
|
---|
81 | compilation.factorizeQueue.setContext("load-module");
|
---|
82 | const oldAddModuleQueueContext =
|
---|
83 | compilation.addModuleQueue.getContext();
|
---|
84 | compilation.addModuleQueue.setContext("load-module");
|
---|
85 | compilation.buildQueue.increaseParallelism();
|
---|
86 | compilation.handleModuleCreation(
|
---|
87 | {
|
---|
88 | factory,
|
---|
89 | dependencies: [dep],
|
---|
90 | originModule:
|
---|
91 | /** @type {NormalModule} */
|
---|
92 | (loaderContext._module),
|
---|
93 | context: loaderContext.context,
|
---|
94 | recursive: false
|
---|
95 | },
|
---|
96 | err => {
|
---|
97 | compilation.factorizeQueue.setContext(oldFactorizeQueueContext);
|
---|
98 | compilation.addModuleQueue.setContext(oldAddModuleQueueContext);
|
---|
99 | compilation.buildQueue.decreaseParallelism();
|
---|
100 | if (err) {
|
---|
101 | return callback(err);
|
---|
102 | }
|
---|
103 | const referencedModule = moduleGraph.getModule(dep);
|
---|
104 | if (!referencedModule) {
|
---|
105 | return callback(new Error("Cannot load the module"));
|
---|
106 | }
|
---|
107 | if (referencedModule.getNumberOfErrors() > 0) {
|
---|
108 | return callback(
|
---|
109 | new Error("The loaded module contains errors")
|
---|
110 | );
|
---|
111 | }
|
---|
112 | const moduleSource = referencedModule.originalSource();
|
---|
113 | if (!moduleSource) {
|
---|
114 | return callback(
|
---|
115 | new Error(
|
---|
116 | "The module created for a LoaderDependency must have an original source"
|
---|
117 | )
|
---|
118 | );
|
---|
119 | }
|
---|
120 | let map;
|
---|
121 | let source;
|
---|
122 | if (moduleSource.sourceAndMap) {
|
---|
123 | const sourceAndMap = moduleSource.sourceAndMap();
|
---|
124 | map = sourceAndMap.map;
|
---|
125 | source = sourceAndMap.source;
|
---|
126 | } else {
|
---|
127 | map = moduleSource.map();
|
---|
128 | source = moduleSource.source();
|
---|
129 | }
|
---|
130 | const fileDependencies = new LazySet();
|
---|
131 | const contextDependencies = new LazySet();
|
---|
132 | const missingDependencies = new LazySet();
|
---|
133 | const buildDependencies = new LazySet();
|
---|
134 | referencedModule.addCacheDependencies(
|
---|
135 | fileDependencies,
|
---|
136 | contextDependencies,
|
---|
137 | missingDependencies,
|
---|
138 | buildDependencies
|
---|
139 | );
|
---|
140 |
|
---|
141 | for (const d of fileDependencies) {
|
---|
142 | loaderContext.addDependency(d);
|
---|
143 | }
|
---|
144 | for (const d of contextDependencies) {
|
---|
145 | loaderContext.addContextDependency(d);
|
---|
146 | }
|
---|
147 | for (const d of missingDependencies) {
|
---|
148 | loaderContext.addMissingDependency(d);
|
---|
149 | }
|
---|
150 | for (const d of buildDependencies) {
|
---|
151 | loaderContext.addBuildDependency(d);
|
---|
152 | }
|
---|
153 | return callback(
|
---|
154 | null,
|
---|
155 | source,
|
---|
156 | /** @type {object | null} */ (map),
|
---|
157 | referencedModule
|
---|
158 | );
|
---|
159 | }
|
---|
160 | );
|
---|
161 | };
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * @param {string} request the request string to load the module from
|
---|
165 | * @param {ImportModuleOptions} options options
|
---|
166 | * @param {ImportModuleCallback} callback callback returning the exports
|
---|
167 | * @returns {void}
|
---|
168 | */
|
---|
169 | const importModule = (request, options, callback) => {
|
---|
170 | const dep = new LoaderImportDependency(request);
|
---|
171 | dep.loc = {
|
---|
172 | name: request
|
---|
173 | };
|
---|
174 | const factory = compilation.dependencyFactories.get(
|
---|
175 | /** @type {DepConstructor} */ (dep.constructor)
|
---|
176 | );
|
---|
177 | if (factory === undefined) {
|
---|
178 | return callback(
|
---|
179 | new Error(
|
---|
180 | `No module factory available for dependency type: ${dep.constructor.name}`
|
---|
181 | )
|
---|
182 | );
|
---|
183 | }
|
---|
184 |
|
---|
185 | const oldFactorizeQueueContext =
|
---|
186 | compilation.factorizeQueue.getContext();
|
---|
187 | compilation.factorizeQueue.setContext("import-module");
|
---|
188 | const oldAddModuleQueueContext =
|
---|
189 | compilation.addModuleQueue.getContext();
|
---|
190 | compilation.addModuleQueue.setContext("import-module");
|
---|
191 | compilation.buildQueue.increaseParallelism();
|
---|
192 | compilation.handleModuleCreation(
|
---|
193 | {
|
---|
194 | factory,
|
---|
195 | dependencies: [dep],
|
---|
196 | originModule:
|
---|
197 | /** @type {NormalModule} */
|
---|
198 | (loaderContext._module),
|
---|
199 | contextInfo: {
|
---|
200 | issuerLayer: options.layer
|
---|
201 | },
|
---|
202 | context: loaderContext.context,
|
---|
203 | connectOrigin: false,
|
---|
204 | checkCycle: true
|
---|
205 | },
|
---|
206 | err => {
|
---|
207 | compilation.factorizeQueue.setContext(oldFactorizeQueueContext);
|
---|
208 | compilation.addModuleQueue.setContext(oldAddModuleQueueContext);
|
---|
209 | compilation.buildQueue.decreaseParallelism();
|
---|
210 | if (err) {
|
---|
211 | return callback(err);
|
---|
212 | }
|
---|
213 | const referencedModule = moduleGraph.getModule(dep);
|
---|
214 | if (!referencedModule) {
|
---|
215 | return callback(new Error("Cannot load the module"));
|
---|
216 | }
|
---|
217 | compilation.buildQueue.increaseParallelism();
|
---|
218 | compilation.executeModule(
|
---|
219 | referencedModule,
|
---|
220 | {
|
---|
221 | entryOptions: {
|
---|
222 | baseUri: options.baseUri,
|
---|
223 | publicPath: options.publicPath
|
---|
224 | }
|
---|
225 | },
|
---|
226 | (err, result) => {
|
---|
227 | compilation.buildQueue.decreaseParallelism();
|
---|
228 | if (err) return callback(err);
|
---|
229 | const {
|
---|
230 | fileDependencies,
|
---|
231 | contextDependencies,
|
---|
232 | missingDependencies,
|
---|
233 | buildDependencies,
|
---|
234 | cacheable,
|
---|
235 | assets,
|
---|
236 | exports
|
---|
237 | } = /** @type {ExecuteModuleResult} */ (result);
|
---|
238 | for (const d of fileDependencies) {
|
---|
239 | loaderContext.addDependency(d);
|
---|
240 | }
|
---|
241 | for (const d of contextDependencies) {
|
---|
242 | loaderContext.addContextDependency(d);
|
---|
243 | }
|
---|
244 | for (const d of missingDependencies) {
|
---|
245 | loaderContext.addMissingDependency(d);
|
---|
246 | }
|
---|
247 | for (const d of buildDependencies) {
|
---|
248 | loaderContext.addBuildDependency(d);
|
---|
249 | }
|
---|
250 | if (cacheable === false) loaderContext.cacheable(false);
|
---|
251 | for (const [name, { source, info }] of assets) {
|
---|
252 | const buildInfo =
|
---|
253 | /** @type {BuildInfo} */
|
---|
254 | (
|
---|
255 | /** @type {NormalModule} */ (loaderContext._module)
|
---|
256 | .buildInfo
|
---|
257 | );
|
---|
258 | if (!buildInfo.assets) {
|
---|
259 | buildInfo.assets = Object.create(null);
|
---|
260 | buildInfo.assetsInfo = new Map();
|
---|
261 | }
|
---|
262 | /** @type {NonNullable<BuildInfo["assets"]>} */
|
---|
263 | (buildInfo.assets)[name] = source;
|
---|
264 | /** @type {NonNullable<BuildInfo["assetsInfo"]>} */
|
---|
265 | (buildInfo.assetsInfo).set(name, info);
|
---|
266 | }
|
---|
267 | callback(null, exports);
|
---|
268 | }
|
---|
269 | );
|
---|
270 | }
|
---|
271 | );
|
---|
272 | };
|
---|
273 |
|
---|
274 | // eslint-disable-next-line no-warning-comments
|
---|
275 | // @ts-ignore Overloading doesn't work
|
---|
276 | loaderContext.importModule = (request, options, callback) => {
|
---|
277 | if (!callback) {
|
---|
278 | return new Promise((resolve, reject) => {
|
---|
279 | importModule(request, options || {}, (err, result) => {
|
---|
280 | if (err) reject(err);
|
---|
281 | else resolve(result);
|
---|
282 | });
|
---|
283 | });
|
---|
284 | }
|
---|
285 | return importModule(request, options || {}, callback);
|
---|
286 | };
|
---|
287 | }
|
---|
288 | );
|
---|
289 | });
|
---|
290 | }
|
---|
291 | }
|
---|
292 | module.exports = LoaderPlugin;
|
---|