source: imaps-frontend/node_modules/webpack/lib/CompatibilityPlugin.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.7 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 {
9 JAVASCRIPT_MODULE_TYPE_AUTO,
10 JAVASCRIPT_MODULE_TYPE_DYNAMIC,
11 JAVASCRIPT_MODULE_TYPE_ESM
12} = require("./ModuleTypeConstants");
13const RuntimeGlobals = require("./RuntimeGlobals");
14const ConstDependency = require("./dependencies/ConstDependency");
15
16/** @typedef {import("estree").CallExpression} CallExpression */
17/** @typedef {import("./Compiler")} Compiler */
18/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
19/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
20/** @typedef {import("./javascript/JavascriptParser").Range} Range */
21
22const nestedWebpackIdentifierTag = Symbol("nested webpack identifier");
23const PLUGIN_NAME = "CompatibilityPlugin";
24
25class CompatibilityPlugin {
26 /**
27 * Apply the plugin
28 * @param {Compiler} compiler the compiler instance
29 * @returns {void}
30 */
31 apply(compiler) {
32 compiler.hooks.compilation.tap(
33 PLUGIN_NAME,
34 (compilation, { normalModuleFactory }) => {
35 compilation.dependencyTemplates.set(
36 ConstDependency,
37 new ConstDependency.Template()
38 );
39
40 normalModuleFactory.hooks.parser
41 .for(JAVASCRIPT_MODULE_TYPE_AUTO)
42 .tap(PLUGIN_NAME, (parser, parserOptions) => {
43 if (
44 parserOptions.browserify !== undefined &&
45 !parserOptions.browserify
46 )
47 return;
48
49 parser.hooks.call.for("require").tap(
50 PLUGIN_NAME,
51 /**
52 * @param {CallExpression} expr call expression
53 * @returns {boolean | void} true when need to handle
54 */
55 expr => {
56 // support for browserify style require delegator: "require(o, !0)"
57 if (expr.arguments.length !== 2) return;
58 const second = parser.evaluateExpression(expr.arguments[1]);
59 if (!second.isBoolean()) return;
60 if (second.asBool() !== true) return;
61 const dep = new ConstDependency(
62 "require",
63 /** @type {Range} */ (expr.callee.range)
64 );
65 dep.loc = /** @type {DependencyLocation} */ (expr.loc);
66 if (parser.state.current.dependencies.length > 0) {
67 const last =
68 parser.state.current.dependencies[
69 parser.state.current.dependencies.length - 1
70 ];
71 if (
72 last.critical &&
73 last.options &&
74 last.options.request === "." &&
75 last.userRequest === "." &&
76 last.options.recursive
77 )
78 parser.state.current.dependencies.pop();
79 }
80 parser.state.module.addPresentationalDependency(dep);
81 return true;
82 }
83 );
84 });
85
86 /**
87 * @param {JavascriptParser} parser the parser
88 * @returns {void}
89 */
90 const handler = parser => {
91 // Handle nested requires
92 parser.hooks.preStatement.tap(PLUGIN_NAME, statement => {
93 if (
94 statement.type === "FunctionDeclaration" &&
95 statement.id &&
96 statement.id.name === RuntimeGlobals.require
97 ) {
98 const newName = `__nested_webpack_require_${
99 /** @type {Range} */ (statement.range)[0]
100 }__`;
101 parser.tagVariable(
102 statement.id.name,
103 nestedWebpackIdentifierTag,
104 {
105 name: newName,
106 declaration: {
107 updated: false,
108 loc: statement.id.loc,
109 range: statement.id.range
110 }
111 }
112 );
113 return true;
114 }
115 });
116 parser.hooks.pattern
117 .for(RuntimeGlobals.require)
118 .tap(PLUGIN_NAME, pattern => {
119 const newName = `__nested_webpack_require_${
120 /** @type {Range} */ (pattern.range)[0]
121 }__`;
122 parser.tagVariable(pattern.name, nestedWebpackIdentifierTag, {
123 name: newName,
124 declaration: {
125 updated: false,
126 loc: pattern.loc,
127 range: pattern.range
128 }
129 });
130 return true;
131 });
132 parser.hooks.pattern
133 .for(RuntimeGlobals.exports)
134 .tap(PLUGIN_NAME, pattern => {
135 parser.tagVariable(pattern.name, nestedWebpackIdentifierTag, {
136 name: "__nested_webpack_exports__",
137 declaration: {
138 updated: false,
139 loc: pattern.loc,
140 range: pattern.range
141 }
142 });
143 return true;
144 });
145 parser.hooks.expression
146 .for(nestedWebpackIdentifierTag)
147 .tap(PLUGIN_NAME, expr => {
148 const { name, declaration } = parser.currentTagData;
149 if (!declaration.updated) {
150 const dep = new ConstDependency(name, declaration.range);
151 dep.loc = declaration.loc;
152 parser.state.module.addPresentationalDependency(dep);
153 declaration.updated = true;
154 }
155 const dep = new ConstDependency(
156 name,
157 /** @type {Range} */ (expr.range)
158 );
159 dep.loc = /** @type {DependencyLocation} */ (expr.loc);
160 parser.state.module.addPresentationalDependency(dep);
161 return true;
162 });
163
164 // Handle hashbang
165 parser.hooks.program.tap(PLUGIN_NAME, (program, comments) => {
166 if (comments.length === 0) return;
167 const c = comments[0];
168 if (c.type === "Line" && /** @type {Range} */ (c.range)[0] === 0) {
169 if (parser.state.source.slice(0, 2).toString() !== "#!") return;
170 // this is a hashbang comment
171 const dep = new ConstDependency("//", 0);
172 dep.loc = /** @type {DependencyLocation} */ (c.loc);
173 parser.state.module.addPresentationalDependency(dep);
174 }
175 });
176 };
177
178 normalModuleFactory.hooks.parser
179 .for(JAVASCRIPT_MODULE_TYPE_AUTO)
180 .tap(PLUGIN_NAME, handler);
181 normalModuleFactory.hooks.parser
182 .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
183 .tap(PLUGIN_NAME, handler);
184 normalModuleFactory.hooks.parser
185 .for(JAVASCRIPT_MODULE_TYPE_ESM)
186 .tap(PLUGIN_NAME, handler);
187 }
188 );
189 }
190}
191module.exports = CompatibilityPlugin;
Note: See TracBrowser for help on using the repository browser.