* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 52 | * @returns {void} |
| 53 | */ |
| 54 | apply(compiler) { |
| 55 | const { options } = this; |
| 56 | |
| 57 | compiler.hooks.compilation.tap( |
| 58 | PLUGIN_NAME, |
| 59 | (compilation, { normalModuleFactory }) => { |
| 60 | compilation.dependencyTemplates.set( |
| 61 | ExternalModuleDependency, |
| 62 | new ExternalModuleDependency.Template() |
| 63 | ); |
| 64 | compilation.dependencyTemplates.set( |
| 65 | ExternalModuleInitFragmentDependency, |
| 66 | new ExternalModuleInitFragmentDependency.Template() |
| 67 | ); |
| 68 | |
| 69 | /** |
| 70 | * Processes the provided parser. |
| 71 | * @param {JavascriptParser} parser the parser |
| 72 | * @param {NodeOptions} nodeOptions options |
| 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 | } |
nothing calls this directly
no test coverage detected