MCPcopy
hub / github.com/webpack/webpack / apply

Method apply

lib/CompatibilityPlugin.js:54–251  ·  view source on GitHub ↗

* Installs parser hooks that preserve compatibility with legacy patterns * such as nested `__webpack_require__` bindings and hashbang handling. * @param {Compiler} compiler the compiler instance * @returns {void}

(compiler)

Source from the content-addressed store, hash-verified

52 * @returns {void}
53 */
54 apply(compiler) {
55 compiler.hooks.compilation.tap(
56 PLUGIN_NAME,
57 (compilation, { normalModuleFactory }) => {
58 compilation.dependencyTemplates.set(
59 ConstDependency,
60 new ConstDependency.Template()
61 );
62
63 normalModuleFactory.hooks.parser
64 .for(JAVASCRIPT_MODULE_TYPE_AUTO)
65 .tap(PLUGIN_NAME, (parser, parserOptions) => {
66 if (
67 parserOptions.browserify !== undefined &&
68 !parserOptions.browserify
69 ) {
70 return;
71 }
72
73 parser.hooks.call.for("require").tap(
74 PLUGIN_NAME,
75 /**
76 * Rewrites browserify-style delegated `require` calls into a
77 * plain webpack require reference and removes the synthetic
78 * context dependency created for the delegator pattern.
79 * @param {CallExpression} expr call expression
80 * @returns {boolean | void} true when need to handle
81 */
82 (expr) => {
83 // support for browserify style require delegator: "require(o, !0)"
84 if (expr.arguments.length !== 2) return;
85 const second = parser.evaluateExpression(expr.arguments[1]);
86 if (!second.isBoolean()) return;
87 if (second.asBool() !== true) return;
88 const dep = new ConstDependency(
89 "require",
90 /** @type {Range} */ (expr.callee.range)
91 );
92 dep.loc = /** @type {DependencyLocation} */ (expr.loc);
93 if (parser.state.current.dependencies.length > 0) {
94 const last =
95 /** @type {ContextDependency} */
96 (
97 parser.state.current.dependencies[
98 parser.state.current.dependencies.length - 1
99 ]
100 );
101 if (
102 last.critical &&
103 last.options &&
104 last.options.request === "." &&
105 last.userRequest === "." &&
106 last.options.recursive
107 ) {
108 parser.state.current.dependencies.pop();
109 }
110 }
111 parser.state.module.addPresentationalDependency(dep);

Callers

nothing calls this directly

Calls 7

tapMethod · 0.80
evaluateExpressionMethod · 0.80
isBooleanMethod · 0.80
asBoolMethod · 0.80
popMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected