| 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 ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
|
|---|
| 9 | const NormalModule = require("./NormalModule");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("../declarations/plugins/LoaderOptionsPlugin").LoaderOptionsPluginOptions} LoaderOptionsPluginOptions */
|
|---|
| 12 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 13 | /** @typedef {import("./ModuleFilenameHelpers").MatchObject} MatchObject */
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Defines the loader context type used by this module.
|
|---|
| 17 | * @template T
|
|---|
| 18 | * @typedef {import("../declarations/LoaderContext").LoaderContext<T>} LoaderContext
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | const PLUGIN_NAME = "LoaderOptionsPlugin";
|
|---|
| 22 |
|
|---|
| 23 | class LoaderOptionsPlugin {
|
|---|
| 24 | /**
|
|---|
| 25 | * Creates an instance of LoaderOptionsPlugin.
|
|---|
| 26 | * @param {LoaderOptionsPluginOptions & MatchObject} options options object
|
|---|
| 27 | */
|
|---|
| 28 | constructor(options = {}) {
|
|---|
| 29 | // If no options are set then generate empty options object
|
|---|
| 30 | if (typeof options !== "object") options = {};
|
|---|
| 31 | if (!options.test) {
|
|---|
| 32 | options.test = () => true;
|
|---|
| 33 | }
|
|---|
| 34 | /** @type {LoaderOptionsPluginOptions & MatchObject} */
|
|---|
| 35 | this.options = options;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 40 | * @param {Compiler} compiler the compiler instance
|
|---|
| 41 | * @returns {void}
|
|---|
| 42 | */
|
|---|
| 43 | apply(compiler) {
|
|---|
| 44 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 45 | compiler.validate(
|
|---|
| 46 | () => require("../schemas/plugins/LoaderOptionsPlugin.json"),
|
|---|
| 47 | this.options,
|
|---|
| 48 | {
|
|---|
| 49 | name: "Loader Options Plugin",
|
|---|
| 50 | baseDataPath: "options"
|
|---|
| 51 | },
|
|---|
| 52 | (options) =>
|
|---|
| 53 | require("../schemas/plugins/LoaderOptionsPlugin.check")(options)
|
|---|
| 54 | );
|
|---|
| 55 | });
|
|---|
| 56 |
|
|---|
| 57 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 58 | NormalModule.getCompilationHooks(compilation).loader.tap(
|
|---|
| 59 | PLUGIN_NAME,
|
|---|
| 60 | (context, module) => {
|
|---|
| 61 | const resource = module.resource;
|
|---|
| 62 | if (!resource) return;
|
|---|
| 63 | const i = resource.indexOf("?");
|
|---|
| 64 | if (
|
|---|
| 65 | ModuleFilenameHelpers.matchObject(
|
|---|
| 66 | this.options,
|
|---|
| 67 | i < 0 ? resource : resource.slice(0, i)
|
|---|
| 68 | )
|
|---|
| 69 | ) {
|
|---|
| 70 | for (const key of Object.keys(this.options)) {
|
|---|
| 71 | if (key === "include" || key === "exclude" || key === "test") {
|
|---|
| 72 | continue;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /** @type {LoaderContext<EXPECTED_ANY> & Record<string, EXPECTED_ANY>} */
|
|---|
| 76 | (context)[key] = this.options[key];
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | );
|
|---|
| 81 | });
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | module.exports = LoaderOptionsPlugin;
|
|---|