source: imaps-frontend/node_modules/webpack/lib/ContextReplacementPlugin.js@ 79a0317

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[79a0317]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const ContextElementDependency = require("./dependencies/ContextElementDependency");
9const { join } = require("./util/fs");
10
11/** @typedef {import("./Compiler")} Compiler */
12/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
13
14class ContextReplacementPlugin {
15 /**
16 * @param {RegExp} resourceRegExp A regular expression that determines which files will be selected
17 * @param {TODO=} newContentResource A new resource to replace the match
18 * @param {TODO=} newContentRecursive If true, all subdirectories are searched for matches
19 * @param {RegExp=} newContentRegExp A regular expression that determines which files will be selected
20 */
21 constructor(
22 resourceRegExp,
23 newContentResource,
24 newContentRecursive,
25 newContentRegExp
26 ) {
27 this.resourceRegExp = resourceRegExp;
28
29 if (typeof newContentResource === "function") {
30 this.newContentCallback = newContentResource;
31 } else if (
32 typeof newContentResource === "string" &&
33 typeof newContentRecursive === "object"
34 ) {
35 this.newContentResource = newContentResource;
36 this.newContentCreateContextMap = (fs, callback) => {
37 callback(null, newContentRecursive);
38 };
39 } else if (
40 typeof newContentResource === "string" &&
41 typeof newContentRecursive === "function"
42 ) {
43 this.newContentResource = newContentResource;
44 this.newContentCreateContextMap = newContentRecursive;
45 } else {
46 if (typeof newContentResource !== "string") {
47 newContentRegExp = newContentRecursive;
48 newContentRecursive = newContentResource;
49 newContentResource = undefined;
50 }
51 if (typeof newContentRecursive !== "boolean") {
52 newContentRegExp = newContentRecursive;
53 newContentRecursive = undefined;
54 }
55 this.newContentResource = newContentResource;
56 this.newContentRecursive = newContentRecursive;
57 this.newContentRegExp = newContentRegExp;
58 }
59 }
60
61 /**
62 * Apply the plugin
63 * @param {Compiler} compiler the compiler instance
64 * @returns {void}
65 */
66 apply(compiler) {
67 const resourceRegExp = this.resourceRegExp;
68 const newContentCallback = this.newContentCallback;
69 const newContentResource = this.newContentResource;
70 const newContentRecursive = this.newContentRecursive;
71 const newContentRegExp = this.newContentRegExp;
72 const newContentCreateContextMap = this.newContentCreateContextMap;
73
74 compiler.hooks.contextModuleFactory.tap("ContextReplacementPlugin", cmf => {
75 cmf.hooks.beforeResolve.tap("ContextReplacementPlugin", result => {
76 if (!result) return;
77 if (resourceRegExp.test(result.request)) {
78 if (newContentResource !== undefined) {
79 result.request = newContentResource;
80 }
81 if (newContentRecursive !== undefined) {
82 result.recursive = newContentRecursive;
83 }
84 if (newContentRegExp !== undefined) {
85 result.regExp = newContentRegExp;
86 }
87 if (typeof newContentCallback === "function") {
88 newContentCallback(result);
89 } else {
90 for (const d of result.dependencies) {
91 if (d.critical) d.critical = false;
92 }
93 }
94 }
95 return result;
96 });
97 cmf.hooks.afterResolve.tap("ContextReplacementPlugin", result => {
98 if (!result) return;
99 if (resourceRegExp.test(result.resource)) {
100 if (newContentResource !== undefined) {
101 if (
102 newContentResource.startsWith("/") ||
103 (newContentResource.length > 1 && newContentResource[1] === ":")
104 ) {
105 result.resource = newContentResource;
106 } else {
107 result.resource = join(
108 /** @type {InputFileSystem} */ (compiler.inputFileSystem),
109 result.resource,
110 newContentResource
111 );
112 }
113 }
114 if (newContentRecursive !== undefined) {
115 result.recursive = newContentRecursive;
116 }
117 if (newContentRegExp !== undefined) {
118 result.regExp = newContentRegExp;
119 }
120 if (typeof newContentCreateContextMap === "function") {
121 result.resolveDependencies =
122 createResolveDependenciesFromContextMap(
123 newContentCreateContextMap
124 );
125 }
126 if (typeof newContentCallback === "function") {
127 const origResource = result.resource;
128 newContentCallback(result);
129 if (
130 result.resource !== origResource &&
131 !result.resource.startsWith("/") &&
132 (result.resource.length <= 1 || result.resource[1] !== ":")
133 ) {
134 // When the function changed it to an relative path
135 result.resource = join(
136 /** @type {InputFileSystem} */ (compiler.inputFileSystem),
137 origResource,
138 result.resource
139 );
140 }
141 } else {
142 for (const d of result.dependencies) {
143 if (d.critical) d.critical = false;
144 }
145 }
146 }
147 return result;
148 });
149 });
150 }
151}
152
153const createResolveDependenciesFromContextMap = createContextMap => {
154 const resolveDependenciesFromContextMap = (fs, options, callback) => {
155 createContextMap(fs, (err, map) => {
156 if (err) return callback(err);
157 const dependencies = Object.keys(map).map(
158 key =>
159 new ContextElementDependency(
160 map[key] + options.resourceQuery + options.resourceFragment,
161 key,
162 options.category,
163 options.referencedExports
164 )
165 );
166 callback(null, dependencies);
167 });
168 };
169 return resolveDependenciesFromContextMap;
170};
171
172module.exports = ContextReplacementPlugin;
Note: See TracBrowser for help on using the repository browser.