MCPcopy
hub / github.com/webpack/webpack / collectImportMetaEnvDefinitions

Function collectImportMetaEnvDefinitions

lib/dependencies/ImportMetaPlugin.js:82–105  ·  view source on GitHub ↗
(compilation)

Source from the content-addressed store, hash-verified

80 * @returns {{ stringify: string, env: Record<string, string> }} env object as JSON string
81 */
82const collectImportMetaEnvDefinitions = (compilation) => {
83 const cached = compilationMetaEnvMap.get(compilation);
84 if (cached) {
85 return cached;
86 }
87
88 const definePluginHooks = DefinePlugin.getCompilationHooks(compilation);
89 const definitions = definePluginHooks.definitions.call({});
90 /** @type {Record<string, string>} */
91 const env = {};
92 /** @type {string[]} */
93 const pairs = [];
94 for (const key of Object.keys(definitions)) {
95 if (key.startsWith("import.meta.env.")) {
96 const envKey = key.slice("import.meta.env.".length);
97 const value = definitions[key];
98 pairs.push(`${JSON.stringify(envKey)}:${value}`);
99 env[envKey] = /** @type {string} */ (value);
100 }
101 }
102 const result = { stringify: `{${pairs.join(",")}}`, env };
103 compilationMetaEnvMap.set(compilation, result);
104 return result;
105};
106
107/**
108 * Defines the import meta plugin hooks type used by this module.

Callers 1

parserHandlerMethod · 0.85

Calls 7

sliceMethod · 0.80
keysMethod · 0.65
getMethod · 0.45
getCompilationHooksMethod · 0.45
callMethod · 0.45
pushMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected