|
8 | 8 | const { pathToFileURL } = require("url"); |
9 | 9 | const { SyncBailHook } = require("tapable"); |
10 | 10 | const Compilation = require("../Compilation"); |
| 11 | +const DefinePlugin = require("../DefinePlugin"); |
11 | 12 | const ModuleDependencyWarning = require("../ModuleDependencyWarning"); |
12 | 13 | const { |
13 | 14 | JAVASCRIPT_MODULE_TYPE_AUTO, |
@@ -43,6 +44,31 @@ const getCriticalDependencyWarning = memoize(() => |
43 | 44 |
|
44 | 45 | const PLUGIN_NAME = "ImportMetaPlugin"; |
45 | 46 |
|
| 47 | +/** |
| 48 | + * Collect import.meta.env definitions from DefinePlugin and build JSON string |
| 49 | + * @param {Compilation} compilation the compilation |
| 50 | + * @returns {string} env object as JSON string |
| 51 | + */ |
| 52 | +const collectImportMetaEnvDefinitions = (compilation) => { |
| 53 | + const definePluginHooks = DefinePlugin.getCompilationHooks(compilation); |
| 54 | + const definitions = definePluginHooks.definitions.call({}); |
| 55 | + if (!definitions) { |
| 56 | + return "{}"; |
| 57 | + } |
| 58 | + |
| 59 | + /** @type {string[]} */ |
| 60 | + const pairs = []; |
| 61 | + for (const key of Object.keys(definitions)) { |
| 62 | + if (key.startsWith("import.meta.env.")) { |
| 63 | + const envKey = key.slice("import.meta.env.".length); |
| 64 | + const value = definitions[key]; |
| 65 | + pairs.push(`${JSON.stringify(envKey)}:${value}`); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return `{${pairs.join(",")}}`; |
| 70 | +}; |
| 71 | + |
46 | 72 | /** |
47 | 73 | * @typedef {object} ImportMetaPluginHooks |
48 | 74 | * @property {SyncBailHook<[DestructuringAssignmentProperty], string | void>} propertyInDestructuring |
@@ -294,6 +320,37 @@ class ImportMetaPlugin { |
294 | 320 | .for("import.meta.main") |
295 | 321 | .tap(PLUGIN_NAME, evaluateToString("boolean")); |
296 | 322 |
|
| 323 | + // import.meta.env |
| 324 | + parser.hooks.typeof |
| 325 | + .for("import.meta.env") |
| 326 | + .tap( |
| 327 | + PLUGIN_NAME, |
| 328 | + toConstantDependency(parser, JSON.stringify("object")) |
| 329 | + ); |
| 330 | + parser.hooks.expression |
| 331 | + .for("import.meta.env") |
| 332 | + .tap(PLUGIN_NAME, (expr) => { |
| 333 | + const envCode = collectImportMetaEnvDefinitions(compilation); |
| 334 | + const dep = new ConstDependency( |
| 335 | + envCode, |
| 336 | + /** @type {Range} */ (expr.range) |
| 337 | + ); |
| 338 | + dep.loc = /** @type {DependencyLocation} */ (expr.loc); |
| 339 | + parser.state.module.addPresentationalDependency(dep); |
| 340 | + return true; |
| 341 | + }); |
| 342 | + parser.hooks.evaluateTypeof |
| 343 | + .for("import.meta.env") |
| 344 | + .tap(PLUGIN_NAME, evaluateToString("object")); |
| 345 | + parser.hooks.evaluateIdentifier |
| 346 | + .for("import.meta.env") |
| 347 | + .tap(PLUGIN_NAME, (expr) => |
| 348 | + new BasicEvaluatedExpression() |
| 349 | + .setTruthy() |
| 350 | + .setSideEffects(false) |
| 351 | + .setRange(/** @type {Range} */ (expr.range)) |
| 352 | + ); |
| 353 | + |
297 | 354 | // Unknown properties |
298 | 355 | parser.hooks.unhandledExpressionMemberChain |
299 | 356 | .for("import.meta") |
|
0 commit comments