source: trip-planner-front/node_modules/webpack/lib/dependencies/LoaderPlugin.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 8.0 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const NormalModule = require("../NormalModule");
9const LazySet = require("../util/LazySet");
10const LoaderDependency = require("./LoaderDependency");
11const LoaderImportDependency = require("./LoaderImportDependency");
12
13/** @typedef {import("../Compilation").DepConstructor} DepConstructor */
14/** @typedef {import("../Compiler")} Compiler */
15/** @typedef {import("../Module")} Module */
16
17/**
18 * @callback LoadModuleCallback
19 * @param {Error=} err error object
20 * @param {string | Buffer=} source source code
21 * @param {object=} map source map
22 * @param {Module=} module loaded module if successful
23 */
24
25/**
26 * @callback ImportModuleCallback
27 * @param {Error=} err error object
28 * @param {any=} exports exports of the evaluated module
29 */
30
31/**
32 * @typedef {Object} ImportModuleOptions
33 * @property {string=} layer the target layer
34 * @property {string=} publicPath the target public path
35 */
36
37class LoaderPlugin {
38 /**
39 * @param {Object} options options
40 * @param {boolean=} options.enableExecuteModule execute module enabled
41 */
42 constructor(options = {}) {
43 this._enableExecuteModule = !!options.enableExecuteModule;
44 }
45 /**
46 * Apply the plugin
47 * @param {Compiler} compiler the compiler instance
48 * @returns {void}
49 */
50 apply(compiler) {
51 compiler.hooks.compilation.tap(
52 "LoaderPlugin",
53 (compilation, { normalModuleFactory }) => {
54 compilation.dependencyFactories.set(
55 LoaderDependency,
56 normalModuleFactory
57 );
58 compilation.dependencyFactories.set(
59 LoaderImportDependency,
60 normalModuleFactory
61 );
62 }
63 );
64
65 compiler.hooks.compilation.tap("LoaderPlugin", compilation => {
66 const moduleGraph = compilation.moduleGraph;
67 NormalModule.getCompilationHooks(compilation).loader.tap(
68 "LoaderPlugin",
69 loaderContext => {
70 /**
71 * @param {string} request the request string to load the module from
72 * @param {LoadModuleCallback} callback callback returning the loaded module or error
73 * @returns {void}
74 */
75 loaderContext.loadModule = (request, callback) => {
76 const dep = new LoaderDependency(request);
77 dep.loc = {
78 name: request
79 };
80 const factory = compilation.dependencyFactories.get(
81 /** @type {DepConstructor} */ (dep.constructor)
82 );
83 if (factory === undefined) {
84 return callback(
85 new Error(
86 `No module factory available for dependency type: ${dep.constructor.name}`
87 )
88 );
89 }
90 compilation.buildQueue.increaseParallelism();
91 compilation.handleModuleCreation(
92 {
93 factory,
94 dependencies: [dep],
95 originModule: loaderContext._module,
96 context: loaderContext.context,
97 recursive: false
98 },
99 err => {
100 compilation.buildQueue.decreaseParallelism();
101 if (err) {
102 return callback(err);
103 }
104 const referencedModule = moduleGraph.getModule(dep);
105 if (!referencedModule) {
106 return callback(new Error("Cannot load the module"));
107 }
108 if (referencedModule.getNumberOfErrors() > 0) {
109 return callback(
110 new Error("The loaded module contains errors")
111 );
112 }
113 const moduleSource = referencedModule.originalSource();
114 if (!moduleSource) {
115 return callback(
116 new Error(
117 "The module created for a LoaderDependency must have an original source"
118 )
119 );
120 }
121 let source, map;
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(null, source, map, referencedModule);
154 }
155 );
156 };
157
158 if (this._enableExecuteModule) {
159 /**
160 * @param {string} request the request string to load the module from
161 * @param {ImportModuleOptions=} options options
162 * @param {ImportModuleCallback=} callback callback returning the exports
163 * @returns {void}
164 */
165 const importModule = (request, options, callback) => {
166 const dep = new LoaderImportDependency(request);
167 dep.loc = {
168 name: request
169 };
170 const factory = compilation.dependencyFactories.get(
171 /** @type {DepConstructor} */ (dep.constructor)
172 );
173 if (factory === undefined) {
174 return callback(
175 new Error(
176 `No module factory available for dependency type: ${dep.constructor.name}`
177 )
178 );
179 }
180 compilation.buildQueue.increaseParallelism();
181 compilation.handleModuleCreation(
182 {
183 factory,
184 dependencies: [dep],
185 originModule: loaderContext._module,
186 contextInfo: {
187 issuerLayer: options.layer
188 },
189 context: loaderContext.context,
190 connectOrigin: false
191 },
192 err => {
193 compilation.buildQueue.decreaseParallelism();
194 if (err) {
195 return callback(err);
196 }
197 const referencedModule = moduleGraph.getModule(dep);
198 if (!referencedModule) {
199 return callback(new Error("Cannot load the module"));
200 }
201 compilation.executeModule(
202 referencedModule,
203 {
204 entryOptions: {
205 publicPath: options.publicPath
206 }
207 },
208 (err, result) => {
209 if (err) return callback(err);
210 for (const d of result.fileDependencies) {
211 loaderContext.addDependency(d);
212 }
213 for (const d of result.contextDependencies) {
214 loaderContext.addContextDependency(d);
215 }
216 for (const d of result.missingDependencies) {
217 loaderContext.addMissingDependency(d);
218 }
219 for (const d of result.buildDependencies) {
220 loaderContext.addBuildDependency(d);
221 }
222 if (result.cacheable === false)
223 loaderContext.cacheable(false);
224 for (const [name, { source, info }] of result.assets) {
225 const { buildInfo } = loaderContext._module;
226 if (!buildInfo.assets) {
227 buildInfo.assets = Object.create(null);
228 buildInfo.assetsInfo = new Map();
229 }
230 buildInfo.assets[name] = source;
231 buildInfo.assetsInfo.set(name, info);
232 }
233 callback(null, result.exports);
234 }
235 );
236 }
237 );
238 };
239
240 /**
241 * @param {string} request the request string to load the module from
242 * @param {ImportModuleOptions} options options
243 * @param {ImportModuleCallback=} callback callback returning the exports
244 * @returns {Promise<any> | void} exports
245 */
246 loaderContext.importModule = (request, options, callback) => {
247 if (!callback) {
248 return new Promise((resolve, reject) => {
249 importModule(request, options || {}, (err, result) => {
250 if (err) reject(err);
251 else resolve(result);
252 });
253 });
254 }
255 return importModule(request, options || {}, callback);
256 };
257 }
258 }
259 );
260 });
261 }
262}
263module.exports = LoaderPlugin;
Note: See TracBrowser for help on using the repository browser.