source: imaps-frontend/node_modules/webpack/lib/LoaderOptionsPlugin.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.2 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 ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
9const NormalModule = require("./NormalModule");
10const createSchemaValidation = require("./util/create-schema-validation");
11
12/** @typedef {import("../declarations/plugins/LoaderOptionsPlugin").LoaderOptionsPluginOptions} LoaderOptionsPluginOptions */
13/** @typedef {import("./Compiler")} Compiler */
14/** @typedef {import("./ModuleFilenameHelpers").MatchObject} MatchObject */
15
16const validate = createSchemaValidation(
17 require("../schemas/plugins/LoaderOptionsPlugin.check.js"),
18 () => require("../schemas/plugins/LoaderOptionsPlugin.json"),
19 {
20 name: "Loader Options Plugin",
21 baseDataPath: "options"
22 }
23);
24
25class LoaderOptionsPlugin {
26 /**
27 * @param {LoaderOptionsPluginOptions & MatchObject} options options object
28 */
29 constructor(options = {}) {
30 validate(options);
31 // If no options are set then generate empty options object
32 if (typeof options !== "object") options = {};
33 if (!options.test) {
34 // This is mocking a RegExp object which always returns true
35 // TODO: Figure out how to do `as unknown as RegExp` for this line
36 // in JSDoc equivalent
37 /** @type {any} */
38 const defaultTrueMockRegExp = {
39 test: () => true
40 };
41
42 /** @type {RegExp} */
43 options.test = defaultTrueMockRegExp;
44 }
45 this.options = options;
46 }
47
48 /**
49 * Apply the plugin
50 * @param {Compiler} compiler the compiler instance
51 * @returns {void}
52 */
53 apply(compiler) {
54 const options = this.options;
55 compiler.hooks.compilation.tap("LoaderOptionsPlugin", compilation => {
56 NormalModule.getCompilationHooks(compilation).loader.tap(
57 "LoaderOptionsPlugin",
58 (context, module) => {
59 const resource = module.resource;
60 if (!resource) return;
61 const i = resource.indexOf("?");
62 if (
63 ModuleFilenameHelpers.matchObject(
64 options,
65 i < 0 ? resource : resource.slice(0, i)
66 )
67 ) {
68 for (const key of Object.keys(options)) {
69 if (key === "include" || key === "exclude" || key === "test") {
70 continue;
71 }
72
73 /** @type {any} */
74 (context)[key] = options[key];
75 }
76 }
77 }
78 );
79 });
80 }
81}
82
83module.exports = LoaderOptionsPlugin;
Note: See TracBrowser for help on using the repository browser.