|
1 | 1 | use std::sync::LazyLock; |
2 | 2 |
|
3 | 3 | use concat_string::concat_string; |
4 | | -use rspack_core::{Compilation, CompilationId, property_access_with_optional}; |
| 4 | +use rspack_core::{ |
| 5 | + Compilation, CompilationId, CompilationParams, CompilerCompilation, Plugin, |
| 6 | + property_access_with_optional, |
| 7 | +}; |
| 8 | +use rspack_error::Result; |
| 9 | +use rspack_hook::{plugin, plugin_hook}; |
5 | 10 | use rspack_util::fx_hash::FxDashMap; |
6 | 11 | use serde_json::{Map, Value}; |
7 | 12 | use swc_atoms::Atom; |
8 | 13 | use swc_experimental_ecma_ast::{MemberExpr, MemberProp, MetaPropKind}; |
9 | 14 |
|
10 | | -use super::define_plugin::utils::{code_object_properties_to_string, code_to_string}; |
11 | | -use crate::visitors::{ExportedVariableInfo, ExpressionExpressionInfo, JavascriptParser}; |
| 15 | +use crate::{ |
| 16 | + parser_plugin::define_plugin::utils::{code_object_properties_to_string, code_to_string}, |
| 17 | + visitors::{ExportedVariableInfo, ExpressionExpressionInfo, JavascriptParser}, |
| 18 | +}; |
| 19 | + |
| 20 | +#[plugin] |
| 21 | +#[derive(Debug, Default)] |
| 22 | +pub struct EnvPlugin; |
| 23 | + |
| 24 | +impl EnvPlugin { |
| 25 | + pub(crate) fn collect(compilation_id: CompilationId, definitions: &ImportMetaEnvDefinitions) { |
| 26 | + let mut state = IMPORT_META_ENV_DEFINITIONS_MAP |
| 27 | + .entry(compilation_id) |
| 28 | + .or_default(); |
| 29 | + state.definitions.merge(definitions); |
| 30 | + state.serialized = None; |
| 31 | + } |
| 32 | + |
| 33 | + fn finalize(compilation: &mut Compilation) { |
| 34 | + let compilation_id = compilation.id(); |
| 35 | + let mut state = IMPORT_META_ENV_DEFINITIONS_MAP |
| 36 | + .entry(compilation_id) |
| 37 | + .or_default(); |
| 38 | + let serialized = match &state.serialized { |
| 39 | + Some(serialized) => serialized.clone(), |
| 40 | + None => { |
| 41 | + let serialized = serialize_import_meta_env_definitions(&state.definitions); |
| 42 | + state.serialized = Some(serialized.clone()); |
| 43 | + serialized |
| 44 | + } |
| 45 | + }; |
| 46 | + compilation |
| 47 | + .value_cache_versions |
| 48 | + .insert(IMPORT_META_ENV_VALUE_DEP_KEY.to_string(), serialized); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[plugin_hook(CompilerCompilation for EnvPlugin, stage = i32::MAX, tracing=false)] |
| 53 | +async fn compilation( |
| 54 | + &self, |
| 55 | + compilation: &mut Compilation, |
| 56 | + _params: &mut CompilationParams, |
| 57 | +) -> Result<()> { |
| 58 | + if compilation.options.experiments.env { |
| 59 | + EnvPlugin::finalize(compilation); |
| 60 | + } |
| 61 | + Ok(()) |
| 62 | +} |
| 63 | + |
| 64 | +impl Plugin for EnvPlugin { |
| 65 | + fn name(&self) -> &'static str { |
| 66 | + "rspack.EnvPlugin" |
| 67 | + } |
| 68 | + |
| 69 | + fn clear_cache(&self, id: CompilationId) { |
| 70 | + IMPORT_META_ENV_DEFINITIONS_MAP.remove(&id); |
| 71 | + } |
| 72 | + |
| 73 | + fn apply(&self, ctx: &mut rspack_core::ApplyContext<'_>) -> Result<()> { |
| 74 | + ctx.compiler_hooks.compilation.tap(compilation::new(self)); |
| 75 | + Ok(()) |
| 76 | + } |
| 77 | +} |
12 | 78 |
|
13 | 79 | pub(crate) const IMPORT_META_ENV: &str = "import.meta.env"; |
14 | 80 | pub(crate) const IMPORT_META_ENV_PREFIX: &str = "import.meta.env."; |
@@ -135,46 +201,6 @@ fn serialize_import_meta_env_definitions(definitions: &ImportMetaEnvDefinitions) |
135 | 201 | concat_string!("{", properties, "}") |
136 | 202 | } |
137 | 203 |
|
138 | | -pub(crate) fn merge_import_meta_env_definitions( |
139 | | - compilation_id: CompilationId, |
140 | | - definitions: &ImportMetaEnvDefinitions, |
141 | | -) { |
142 | | - let mut state = IMPORT_META_ENV_DEFINITIONS_MAP |
143 | | - .entry(compilation_id) |
144 | | - .or_default(); |
145 | | - state.definitions.merge(definitions); |
146 | | - state.serialized = None; |
147 | | -} |
148 | | - |
149 | | -pub(crate) fn finalize_import_meta_env_definitions(compilation: &mut Compilation) { |
150 | | - let compilation_id = compilation.id(); |
151 | | - let mut state = IMPORT_META_ENV_DEFINITIONS_MAP |
152 | | - .entry(compilation_id) |
153 | | - .or_default(); |
154 | | - let serialized = match &state.serialized { |
155 | | - Some(serialized) => serialized.clone(), |
156 | | - None => { |
157 | | - let serialized = serialize_import_meta_env_definitions(&state.definitions); |
158 | | - state.serialized = Some(serialized.clone()); |
159 | | - serialized |
160 | | - } |
161 | | - }; |
162 | | - compilation |
163 | | - .value_cache_versions |
164 | | - .insert(IMPORT_META_ENV_VALUE_DEP_KEY.to_string(), serialized); |
165 | | -} |
166 | | - |
167 | | -pub(crate) fn seed_import_meta_env_value_cache(compilation: &mut Compilation) { |
168 | | - compilation.value_cache_versions.insert( |
169 | | - IMPORT_META_ENV_VALUE_DEP_KEY.to_string(), |
170 | | - import_meta_env_definitions_string(compilation.id()), |
171 | | - ); |
172 | | -} |
173 | | - |
174 | | -pub(crate) fn remove_import_meta_env_definitions(compilation_id: CompilationId) { |
175 | | - IMPORT_META_ENV_DEFINITIONS_MAP.remove(&compilation_id); |
176 | | -} |
177 | | - |
178 | 204 | pub(crate) fn import_meta_env_definitions_string(compilation_id: CompilationId) -> String { |
179 | 205 | IMPORT_META_ENV_DEFINITIONS_MAP |
180 | 206 | .get(&compilation_id) |
|
0 commit comments