-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathinject_global_variables.rs
254 lines (223 loc) · 9.15 KB
/
inject_global_variables.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use std::sync::Arc;
use cow_utils::CowUtils;
use oxc_allocator::Allocator;
use oxc_ast::{ast::*, AstBuilder, NONE};
use oxc_semantic::{ScopeTree, SymbolTable};
use oxc_span::{CompactStr, SPAN};
use oxc_traverse::{traverse_mut, Traverse, TraverseCtx};
use super::replace_global_defines::{DotDefine, ReplaceGlobalDefines};
#[derive(Debug, Clone)]
pub struct InjectGlobalVariablesConfig {
injects: Arc<[InjectImport]>,
}
impl InjectGlobalVariablesConfig {
pub fn new(injects: Vec<InjectImport>) -> Self {
Self { injects: Arc::from(injects) }
}
}
#[derive(Debug, Clone)]
pub struct InjectImport {
/// `import _ from `source`
source: CompactStr,
specifier: InjectImportSpecifier,
/// value to be replaced for `specifier.local` if it's a `StaticMemberExpression` in the form of `foo.bar.baz`.
replace_value: Option<CompactStr>,
}
impl InjectImport {
pub fn named_specifier(source: &str, imported: Option<&str>, local: &str) -> InjectImport {
InjectImport {
source: CompactStr::from(source),
specifier: InjectImportSpecifier::Specifier {
imported: imported.map(CompactStr::from),
local: CompactStr::from(local),
},
replace_value: Self::replace_name(local),
}
}
pub fn namespace_specifier(source: &str, local: &str) -> InjectImport {
InjectImport {
source: CompactStr::from(source),
specifier: InjectImportSpecifier::NamespaceSpecifier { local: CompactStr::from(local) },
replace_value: Self::replace_name(local),
}
}
pub fn default_specifier(source: &str, local: &str) -> InjectImport {
InjectImport {
source: CompactStr::from(source),
specifier: InjectImportSpecifier::DefaultSpecifier { local: CompactStr::from(local) },
replace_value: Self::replace_name(local),
}
}
fn replace_name(local: &str) -> Option<CompactStr> {
local
.contains('.')
.then(|| CompactStr::from(format!("$inject_{}", local.cow_replace('.', "_"))))
}
}
#[derive(Debug, Clone)]
pub enum InjectImportSpecifier {
/// `import { local } from "source"`
/// `import { default as local } from "source"` when `imported` is `None`
Specifier { imported: Option<CompactStr>, local: CompactStr },
/// import * as local from "source"
NamespaceSpecifier { local: CompactStr },
/// import local from "source"
DefaultSpecifier { local: CompactStr },
}
impl InjectImportSpecifier {
fn local(&self) -> &CompactStr {
match self {
Self::Specifier { local, .. }
| Self::NamespaceSpecifier { local, .. }
| Self::DefaultSpecifier { local, .. } => local,
}
}
}
/// Wrapper around `DotDefine` which caches the `Atom` to replace with.
/// `value_atom` is populated lazily when first replacement happens.
/// If no replacement is made, `value_atom` remains `None`.
struct DotDefineState<'a> {
dot_define: DotDefine,
value_atom: Option<Atom<'a>>,
}
impl<'a> From<&InjectImport> for DotDefineState<'a> {
fn from(inject: &InjectImport) -> Self {
let parts = inject.specifier.local().split('.').map(CompactStr::from).collect::<Vec<_>>();
let value = inject.replace_value.clone().unwrap();
let dot_define = DotDefine { parts, value };
Self { dot_define, value_atom: None }
}
}
#[must_use]
pub struct InjectGlobalVariablesReturn {
pub symbols: SymbolTable,
pub scopes: ScopeTree,
}
/// Injects import statements for global variables.
///
/// References:
///
/// * <https://www.npmjs.com/package/@rollup/plugin-inject>
pub struct InjectGlobalVariables<'a> {
ast: AstBuilder<'a>,
config: InjectGlobalVariablesConfig,
// states
/// Dot defines derived from the config.
dot_defines: Vec<DotDefineState<'a>>,
/// Identifiers for which dot define replaced a member expression.
replaced_dot_defines:
Vec<(/* identifier of member expression */ CompactStr, /* local */ CompactStr)>,
}
impl<'a> Traverse<'a> for InjectGlobalVariables<'a> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.replace_dot_defines(expr, ctx);
}
}
impl<'a> InjectGlobalVariables<'a> {
pub fn new(allocator: &'a Allocator, config: InjectGlobalVariablesConfig) -> Self {
Self {
ast: AstBuilder::new(allocator),
config,
dot_defines: vec![],
replaced_dot_defines: vec![],
}
}
pub fn build(
&mut self,
symbols: SymbolTable,
scopes: ScopeTree,
program: &mut Program<'a>,
) -> InjectGlobalVariablesReturn {
let mut symbols = symbols;
let mut scopes = scopes;
// Step 1: slow path where visiting the AST is required to replace dot defines.
let dot_defines = self
.config
.injects
.iter()
.filter(|i| i.replace_value.is_some())
.map(DotDefineState::from)
.collect::<Vec<_>>();
if !dot_defines.is_empty() {
self.dot_defines = dot_defines;
(symbols, scopes) = traverse_mut(self, self.ast.allocator, program, symbols, scopes);
}
// Step 2: find all the injects that are referenced.
let injects = self
.config
.injects
.iter()
.filter(|i| {
// remove replaced `Buffer` for `Buffer` + Buffer.isBuffer` combo.
if let Some(replace_value) = &i.replace_value {
self.replaced_dot_defines.iter().any(|d| d.1 == replace_value)
} else if self.replaced_dot_defines.iter().any(|d| d.0 == i.specifier.local()) {
false
} else {
scopes.root_unresolved_references().contains_key(i.specifier.local())
}
})
.cloned()
.collect::<Vec<_>>();
if injects.is_empty() {
return InjectGlobalVariablesReturn { symbols, scopes };
}
self.inject_imports(&injects, program);
InjectGlobalVariablesReturn { symbols, scopes }
}
fn inject_imports(&self, injects: &[InjectImport], program: &mut Program<'a>) {
let imports = injects.iter().map(|inject| {
let specifiers = Some(self.ast.vec1(self.inject_import_to_specifier(inject)));
let source = self.ast.string_literal(SPAN, inject.source.as_str());
let kind = ImportOrExportKind::Value;
let import_decl = self
.ast
.module_declaration_import_declaration(SPAN, specifiers, source, NONE, kind);
self.ast.statement_module_declaration(import_decl)
});
program.body.splice(0..0, imports);
}
fn inject_import_to_specifier(&self, inject: &InjectImport) -> ImportDeclarationSpecifier<'a> {
match &inject.specifier {
InjectImportSpecifier::Specifier { imported, local } => {
let imported = imported.as_deref().unwrap_or("default");
let local = inject.replace_value.as_ref().unwrap_or(local).as_str();
self.ast.import_declaration_specifier_import_specifier(
SPAN,
self.ast.module_export_name_identifier_name(SPAN, imported),
self.ast.binding_identifier(SPAN, local),
ImportOrExportKind::Value,
)
}
InjectImportSpecifier::DefaultSpecifier { local } => {
let local = inject.replace_value.as_ref().unwrap_or(local).as_str();
let local = self.ast.binding_identifier(SPAN, local);
self.ast.import_declaration_specifier_import_default_specifier(SPAN, local)
}
InjectImportSpecifier::NamespaceSpecifier { local } => {
let local = inject.replace_value.as_ref().unwrap_or(local).as_str();
let local = self.ast.binding_identifier(SPAN, local);
self.ast.import_declaration_specifier_import_namespace_specifier(SPAN, local)
}
}
}
fn replace_dot_defines(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Expression::StaticMemberExpression(member) = expr {
for DotDefineState { dot_define, value_atom } in &mut self.dot_defines {
if ReplaceGlobalDefines::is_dot_define(ctx.symbols(), dot_define, member) {
// If this is first replacement made for this dot define,
// create `Atom` for replacement, and record in `replaced_dot_defines`
if value_atom.is_none() {
*value_atom = Some(self.ast.atom(dot_define.value.as_str()));
self.replaced_dot_defines
.push((dot_define.parts[0].clone(), dot_define.value.clone()));
}
let value_atom = value_atom.as_ref().unwrap().clone();
let value = self.ast.expression_identifier_reference(SPAN, value_atom);
*expr = value;
break;
}
}
}
}
}