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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! Rewrite default export to a variable declaration.
//!
//! This module transforms `export default` declarations to variable declarations,
//! allowing the compiler to inject properties like render functions.
use oxc_allocator::Allocator;
use oxc_ast::ast::{ExportDefaultDeclarationKind, Statement};
use oxc_parser::Parser;
use oxc_span::{GetSpan, SourceType};
use vize_carton::{profile, String, ToCompactString};
/// Rewrite `export default` to a const declaration with the given name.
/// Returns (rewritten_code, has_default_export)
pub fn rewrite_default(input: &str, as_name: &str, is_ts: bool) -> (String, bool) {
let source_type = if is_ts {
SourceType::ts()
} else {
SourceType::mjs()
};
let allocator = Allocator::default();
let ret = profile!(
"atelier.normal_script.rewrite_default.parse",
Parser::new(&allocator, input, source_type).parse()
);
if !ret.errors.is_empty() {
// If parsing fails, return original code
return (input.to_compact_string(), false);
}
let program = ret.program;
// Check if there's a default export
let has_default = program.body.iter().any(|stmt| {
matches!(stmt, Statement::ExportDefaultDeclaration(_))
|| matches!(stmt, Statement::ExportNamedDeclaration(decl)
if decl.specifiers.iter().any(|s| {
matches!(&s.exported, oxc_ast::ast::ModuleExportName::IdentifierName(name) if name.name == "default")
|| matches!(&s.exported, oxc_ast::ast::ModuleExportName::IdentifierReference(name) if name.name == "default")
}))
});
if !has_default {
// No default export - append empty object
let mut output = input.to_compact_string();
output.push_str("\nconst ");
output.push_str(as_name);
output.push_str(" = {}");
return (output, false);
}
// Find and rewrite the default export
let mut output = String::default();
let mut last_end = 0;
for stmt in program.body.iter() {
match stmt {
Statement::ExportDefaultDeclaration(decl) => {
// Copy everything before this statement
output.push_str(&input[last_end..decl.span.start as usize]);
match &decl.declaration {
ExportDefaultDeclarationKind::ClassDeclaration(class_decl) => {
// export default class Foo {} -> class Foo {} \n const as_name = Foo
if let Some(id) = &class_decl.id {
output.push_str("class ");
output.push_str(id.name.as_str());
// Copy the rest of the class declaration
let class_body_start = id.span.end as usize;
let class_body = &input[class_body_start..decl.span.end as usize];
output.push_str(class_body);
output.push_str("\nconst ");
output.push_str(as_name);
output.push_str(" = ");
output.push_str(id.name.as_str());
} else {
// Anonymous class - wrap in const
output.push_str("const ");
output.push_str(as_name);
output.push_str(" = ");
let class_start = class_decl.span.start as usize;
output.push_str(&input[class_start..decl.span.end as usize]);
}
}
ExportDefaultDeclarationKind::FunctionDeclaration(func_decl) => {
// export default function foo() {} -> function foo() {} \n const as_name = foo
if let Some(id) = &func_decl.id {
output.push_str("function ");
output.push_str(id.name.as_str());
// Copy the rest of the function
let func_body_start = id.span.end as usize;
let func_body = &input[func_body_start..decl.span.end as usize];
output.push_str(func_body);
output.push_str("\nconst ");
output.push_str(as_name);
output.push_str(" = ");
output.push_str(id.name.as_str());
} else {
// Anonymous function - wrap in const
output.push_str("const ");
output.push_str(as_name);
output.push_str(" = ");
let func_start = func_decl.span.start as usize;
output.push_str(&input[func_start..decl.span.end as usize]);
}
}
_ => {
// export default {...} -> const as_name = {...}
output.push_str("const ");
output.push_str(as_name);
output.push_str(" = ");
let expr_start = decl.declaration.span().start as usize;
let expr_end = decl.declaration.span().end as usize;
output.push_str(&input[expr_start..expr_end]);
}
}
last_end = decl.span.end as usize;
}
Statement::ExportNamedDeclaration(named_decl) => {
// Handle: export { foo as default }
let has_default_specifier = named_decl.specifiers.iter().any(|s| {
matches!(&s.exported, oxc_ast::ast::ModuleExportName::IdentifierName(name) if name.name == "default")
|| matches!(&s.exported, oxc_ast::ast::ModuleExportName::IdentifierReference(name) if name.name == "default")
});
if has_default_specifier {
// Copy everything before this statement
output.push_str(&input[last_end..named_decl.span.start as usize]);
if let Some(source) = &named_decl.source {
// export { default } from '...' or export { foo as default } from '...'
for specifier in &named_decl.specifiers {
let is_default = matches!(&specifier.exported,
oxc_ast::ast::ModuleExportName::IdentifierName(name) if name.name == "default")
|| matches!(&specifier.exported,
oxc_ast::ast::ModuleExportName::IdentifierReference(name) if name.name == "default");
if is_default {
let local_name = match &specifier.local {
oxc_ast::ast::ModuleExportName::IdentifierName(name) => {
name.name.as_str()
}
oxc_ast::ast::ModuleExportName::IdentifierReference(name) => {
name.name.as_str()
}
_ => "default",
};
// Add import for the default
output.push_str("import { ");
output.push_str(local_name);
output.push_str(" as __VUE_DEFAULT__ } from '");
output.push_str(source.value.as_str());
output.push_str("'\n");
}
}
// Rebuild export without the default specifier
let other_specifiers: Vec<_> = named_decl
.specifiers
.iter()
.filter(|s| {
!matches!(&s.exported,
oxc_ast::ast::ModuleExportName::IdentifierName(name) if name.name == "default")
&& !matches!(&s.exported,
oxc_ast::ast::ModuleExportName::IdentifierReference(name) if name.name == "default")
})
.collect();
if !other_specifiers.is_empty() {
output.push_str("export { ");
for (i, spec) in other_specifiers.iter().enumerate() {
if i > 0 {
output.push_str(", ");
}
let local = match &spec.local {
oxc_ast::ast::ModuleExportName::IdentifierName(name) => {
name.name.as_str()
}
oxc_ast::ast::ModuleExportName::IdentifierReference(name) => {
name.name.as_str()
}
_ => continue,
};
let exported = match &spec.exported {
oxc_ast::ast::ModuleExportName::IdentifierName(name) => {
name.name.as_str()
}
oxc_ast::ast::ModuleExportName::IdentifierReference(name) => {
name.name.as_str()
}
_ => continue,
};
if local == exported {
output.push_str(local);
} else {
output.push_str(local);
output.push_str(" as ");
output.push_str(exported);
}
}
output.push_str(" } from '");
output.push_str(source.value.as_str());
output.push_str("'\n");
}
output.push_str("const ");
output.push_str(as_name);
output.push_str(" = __VUE_DEFAULT__");
} else {
// export { foo as default } (no source)
for specifier in &named_decl.specifiers {
let is_default = matches!(&specifier.exported,
oxc_ast::ast::ModuleExportName::IdentifierName(name) if name.name == "default")
|| matches!(&specifier.exported,
oxc_ast::ast::ModuleExportName::IdentifierReference(name) if name.name == "default");
if is_default {
let local_name = match &specifier.local {
oxc_ast::ast::ModuleExportName::IdentifierName(name) => {
name.name.as_str()
}
oxc_ast::ast::ModuleExportName::IdentifierReference(name) => {
name.name.as_str()
}
_ => "default",
};
// Rebuild export without the default specifier
let other_specifiers: Vec<_> = named_decl
.specifiers
.iter()
.filter(|s| {
!matches!(&s.exported,
oxc_ast::ast::ModuleExportName::IdentifierName(name) if name.name == "default")
&& !matches!(&s.exported,
oxc_ast::ast::ModuleExportName::IdentifierReference(name) if name.name == "default")
})
.collect();
if !other_specifiers.is_empty() {
output.push_str("export { ");
for (i, spec) in other_specifiers.iter().enumerate() {
if i > 0 {
output.push_str(", ");
}
let local = match &spec.local {
oxc_ast::ast::ModuleExportName::IdentifierName(
name,
) => name.name.as_str(),
oxc_ast::ast::ModuleExportName::IdentifierReference(
name,
) => name.name.as_str(),
_ => continue,
};
let exported = match &spec.exported {
oxc_ast::ast::ModuleExportName::IdentifierName(
name,
) => name.name.as_str(),
oxc_ast::ast::ModuleExportName::IdentifierReference(
name,
) => name.name.as_str(),
_ => continue,
};
if local == exported {
output.push_str(local);
} else {
output.push_str(local);
output.push_str(" as ");
output.push_str(exported);
}
}
output.push_str(" }\n");
}
output.push_str("const ");
output.push_str(as_name);
output.push_str(" = ");
output.push_str(local_name);
break;
}
}
}
last_end = named_decl.span.end as usize;
}
}
_ => {}
}
}
// Copy remaining content
if last_end < input.len() {
output.push_str(&input[last_end..]);
}
(output, has_default)
}
#[cfg(test)]
mod tests {
use super::rewrite_default;
#[test]
fn test_rewrite_default_object() {
let (result, has_default) = rewrite_default("export default {}", "_sfc_main", false);
assert!(has_default);
insta::assert_snapshot!(result.as_str());
}
#[test]
fn test_rewrite_default_with_other_code() {
let input = r#"
import { ref } from 'vue'
const count = ref(0)
export default {
name: 'MyComponent'
}
"#;
let (result, has_default) = rewrite_default(input, "_sfc_main", false);
assert!(has_default);
insta::assert_snapshot!(result.as_str());
}
#[test]
fn test_rewrite_default_class() {
let (result, has_default) =
rewrite_default("export default class Foo {}", "_sfc_main", false);
assert!(has_default);
insta::assert_snapshot!(result.as_str());
}
#[test]
fn test_no_default_export() {
let (result, has_default) = rewrite_default("export const a = {}", "_sfc_main", false);
assert!(!has_default);
insta::assert_snapshot!(result.as_str());
}
#[test]
fn test_named_default_export() {
let input = "const a = 1\nexport { a as default }";
let (result, has_default) = rewrite_default(input, "_sfc_main", false);
assert!(has_default);
insta::assert_snapshot!(result.as_str());
}
}