MCPcopy
hub / github.com/webpack/webpack / globalHandler

Method globalHandler

lib/NodeStuffPlugin.js:75–120  ·  view source on GitHub ↗
(parser, nodeOptions)

Source from the content-addressed store, hash-verified

73 * @returns {void}
74 */
75 const globalHandler = (parser, nodeOptions) => {
76 /**
77 * Returns const dependency.
78 * @param {Expression} expr expression
79 * @returns {ConstDependency} const dependency
80 */
81 const getGlobalDep = (expr) => {
82 if (compilation.outputOptions.environment.globalThis) {
83 return new ConstDependency(
84 "globalThis",
85 /** @type {Range} */ (expr.range)
86 );
87 }
88
89 return new ConstDependency(
90 RuntimeGlobals.global,
91 /** @type {Range} */ (expr.range),
92 [RuntimeGlobals.global]
93 );
94 };
95
96 const withWarning = nodeOptions.global === "warn";
97
98 parser.hooks.expression.for("global").tap(PLUGIN_NAME, (expr) => {
99 const dep = getGlobalDep(expr);
100 dep.loc = /** @type {DependencyLocation} */ (expr.loc);
101 parser.state.module.addPresentationalDependency(dep);
102
103 if (withWarning) {
104 parser.state.module.addWarning(
105 new NodeStuffInWebError(
106 dep.loc,
107 "global",
108 "The global namespace object is a Node.js feature and isn't available in browsers."
109 )
110 );
111 }
112 });
113
114 parser.hooks.rename.for("global").tap(PLUGIN_NAME, (expr) => {
115 const dep = getGlobalDep(expr);
116 dep.loc = /** @type {DependencyLocation} */ (expr.loc);
117 parser.state.module.addPresentationalDependency(dep);
118 return false;
119 });
120 };
121
122 const hooks = ImportMetaPlugin.getCompilationHooks(compilation);
123

Callers

nothing calls this directly

Calls 3

tapMethod · 0.80
addWarningMethod · 0.45

Tested by

no test coverage detected