(parser, nodeOptions)
| 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 |
nothing calls this directly
no test coverage detected