| [9af201e] | 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 { dirname, join } = require("./util/fs");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 11 | /** @typedef {import("./NormalModuleFactory").ResolveData} ResolveData */
|
|---|
| 12 | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {(resolveData: ResolveData) => void} ModuleReplacer */
|
|---|
| 15 |
|
|---|
| 16 | const PLUGIN_NAME = "NormalModuleReplacementPlugin";
|
|---|
| 17 |
|
|---|
| 18 | class NormalModuleReplacementPlugin {
|
|---|
| 19 | /**
|
|---|
| 20 | * Create an instance of the plugin
|
|---|
| 21 | * @param {RegExp} resourceRegExp the resource matcher
|
|---|
| 22 | * @param {string | ModuleReplacer} newResource the resource replacement
|
|---|
| 23 | */
|
|---|
| 24 | constructor(resourceRegExp, newResource) {
|
|---|
| 25 | this.resourceRegExp = resourceRegExp;
|
|---|
| 26 | this.newResource = newResource;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 31 | * @param {Compiler} compiler the compiler instance
|
|---|
| 32 | * @returns {void}
|
|---|
| 33 | */
|
|---|
| 34 | apply(compiler) {
|
|---|
| 35 | const resourceRegExp = this.resourceRegExp;
|
|---|
| 36 | const newResource = this.newResource;
|
|---|
| 37 | compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (nmf) => {
|
|---|
| 38 | nmf.hooks.beforeResolve.tap(PLUGIN_NAME, (result) => {
|
|---|
| 39 | if (resourceRegExp.test(result.request)) {
|
|---|
| 40 | if (typeof newResource === "function") {
|
|---|
| 41 | newResource(result);
|
|---|
| 42 | } else {
|
|---|
| 43 | result.request = newResource;
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 | });
|
|---|
| 47 | nmf.hooks.afterResolve.tap(PLUGIN_NAME, (result) => {
|
|---|
| 48 | const createData = result.createData;
|
|---|
| 49 | if (resourceRegExp.test(/** @type {string} */ (createData.resource))) {
|
|---|
| 50 | if (typeof newResource === "function") {
|
|---|
| 51 | newResource(result);
|
|---|
| 52 | } else {
|
|---|
| 53 | const fs =
|
|---|
| 54 | /** @type {InputFileSystem} */
|
|---|
| 55 | (compiler.inputFileSystem);
|
|---|
| 56 | if (
|
|---|
| 57 | newResource.startsWith("/") ||
|
|---|
| 58 | (newResource.length > 1 && newResource[1] === ":")
|
|---|
| 59 | ) {
|
|---|
| 60 | createData.resource = newResource;
|
|---|
| 61 | } else {
|
|---|
| 62 | createData.resource = join(
|
|---|
| 63 | fs,
|
|---|
| 64 | dirname(fs, /** @type {string} */ (createData.resource)),
|
|---|
| 65 | newResource
|
|---|
| 66 | );
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | });
|
|---|
| 71 | });
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | module.exports = NormalModuleReplacementPlugin;
|
|---|