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