File size: 16,112 Bytes
1e92f2d |
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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
use std::collections::HashMap;
use serde::Deserialize;
use swc_core::{
atoms::{atom, Atom},
common::DUMMY_SP,
ecma::{
ast::*,
utils::private_ident,
visit::{fold_pass, Fold},
},
};
#[derive(Clone, Debug, Deserialize)]
pub struct Config {
pub wildcard: bool,
}
pub fn optimize_barrel(config: Config) -> impl Pass {
fold_pass(OptimizeBarrel {
wildcard: config.wildcard,
})
}
#[derive(Debug, Default)]
struct OptimizeBarrel {
wildcard: bool,
}
impl Fold for OptimizeBarrel {
fn fold_module_items(&mut self, items: Vec<ModuleItem>) -> Vec<ModuleItem> {
// One pre-pass to find all the local idents that we are referencing, so we can
// handle the case of `import foo from 'a'; export { foo };` correctly.
// Map of "local ident" -> ("source module", "orig ident")
let mut local_idents = HashMap::new();
for item in &items {
if let ModuleItem::ModuleDecl(ModuleDecl::Import(import_decl)) = item {
for spec in &import_decl.specifiers {
let src = import_decl.src.value.clone();
match spec {
ImportSpecifier::Named(s) => {
local_idents.insert(
s.local.sym.clone(),
(
src.clone(),
match &s.imported {
Some(n) => match &n {
ModuleExportName::Ident(n) => n.sym.clone(),
ModuleExportName::Str(n) => n.value.clone(),
},
None => s.local.sym.clone(),
},
),
);
}
ImportSpecifier::Namespace(s) => {
local_idents.insert(s.local.sym.clone(), (src.clone(), atom!("*")));
}
ImportSpecifier::Default(s) => {
local_idents
.insert(s.local.sym.clone(), (src.clone(), atom!("default")));
}
}
}
}
}
// The second pass to rebuild the module items.
let mut new_items = vec![];
// Exported meta information.
let mut export_map = vec![];
let mut export_wildcards = vec![];
// We only apply this optimization to barrel files. Here we consider
// a barrel file to be a file that only exports from other modules.
// Besides that, lit expressions are allowed as well ("use client", etc.).
let mut allowed_directives = true;
let mut directives = vec![];
let mut is_barrel = true;
for item in &items {
match item {
ModuleItem::ModuleDecl(decl) => {
allowed_directives = false;
match decl {
ModuleDecl::Import(_) => {}
// export { foo } from './foo';
ModuleDecl::ExportNamed(export_named) => {
for spec in &export_named.specifiers {
match spec {
ExportSpecifier::Namespace(s) => {
let name_str = match &s.name {
ModuleExportName::Ident(n) => n.sym.clone(),
ModuleExportName::Str(n) => n.value.clone(),
};
if let Some(src) = &export_named.src {
export_map.push((
name_str.clone(),
src.value.clone(),
atom!("*"),
));
} else if self.wildcard {
export_map.push((
name_str.clone(),
Atom::default(),
atom!("*"),
));
} else {
is_barrel = false;
break;
}
}
ExportSpecifier::Named(s) => {
let orig_str = match &s.orig {
ModuleExportName::Ident(n) => n.sym.clone(),
ModuleExportName::Str(n) => n.value.clone(),
};
let name_str = match &s.exported {
Some(n) => match &n {
ModuleExportName::Ident(n) => n.sym.clone(),
ModuleExportName::Str(n) => n.value.clone(),
},
None => orig_str.clone(),
};
if let Some(src) = &export_named.src {
export_map.push((
name_str.clone(),
src.value.clone(),
orig_str.clone(),
));
} else if let Some((src, orig)) =
local_idents.get(&orig_str)
{
export_map.push((
name_str.clone(),
src.clone(),
orig.clone(),
));
} else if self.wildcard {
export_map.push((
name_str.clone(),
Atom::default(),
orig_str.clone(),
));
} else {
is_barrel = false;
break;
}
}
_ => {
if !self.wildcard {
is_barrel = false;
break;
}
}
}
}
}
ModuleDecl::ExportAll(export_all) => {
export_wildcards.push(export_all.src.value.to_string());
}
ModuleDecl::ExportDecl(export_decl) => {
// Export declarations are not allowed in barrel files.
if !self.wildcard {
is_barrel = false;
break;
}
match &export_decl.decl {
Decl::Class(class) => {
export_map.push((
class.ident.sym.clone(),
Atom::default(),
Atom::default(),
));
}
Decl::Fn(func) => {
export_map.push((
func.ident.sym.clone(),
Atom::default(),
Atom::default(),
));
}
Decl::Var(var) => {
let ids = collect_idents_in_var_decls(&var.decls);
for id in ids {
export_map.push((id, Atom::default(), Atom::default()));
}
}
_ => {}
}
}
_ => {
if !self.wildcard {
// Other expressions are not allowed in barrel files.
is_barrel = false;
break;
}
}
}
}
ModuleItem::Stmt(stmt) => match stmt {
Stmt::Expr(expr) => match &*expr.expr {
Expr::Lit(l) => {
if let Lit::Str(s) = l {
if allowed_directives && s.value.starts_with("use ") {
directives.push(s.value.to_string());
}
} else {
allowed_directives = false;
}
}
_ => {
allowed_directives = false;
if !self.wildcard {
is_barrel = false;
break;
}
}
},
_ => {
allowed_directives = false;
if !self.wildcard {
is_barrel = false;
break;
}
}
},
}
}
// If the file is not a barrel file, we export nothing.
if !is_barrel {
new_items = vec![];
} else {
// Otherwise we export the meta information.
new_items.push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
span: DUMMY_SP,
decl: Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Const,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(BindingIdent {
id: private_ident!("__next_private_export_map__"),
type_ann: None,
}),
init: Some(Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: serde_json::to_string(&export_map).unwrap().into(),
raw: None,
})))),
definite: false,
}],
..Default::default()
})),
})));
// Push "export *" statements for each wildcard export.
for src in export_wildcards {
new_items.push(ModuleItem::ModuleDecl(ModuleDecl::ExportAll(ExportAll {
span: DUMMY_SP,
src: Box::new(Str {
span: DUMMY_SP,
value: format!("__barrel_optimize__?names=__PLACEHOLDER__!=!{src}").into(),
raw: None,
}),
with: None,
type_only: false,
})));
}
// Push directives.
if !directives.is_empty() {
new_items.push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
span: DUMMY_SP,
decl: Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Const,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(BindingIdent {
id: private_ident!("__next_private_directive_list__"),
type_ann: None,
}),
init: Some(Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: serde_json::to_string(&directives).unwrap().into(),
raw: None,
})))),
definite: false,
}],
..Default::default()
})),
})));
}
}
new_items
}
}
fn collect_idents_in_array_pat(elems: &[Option<Pat>]) -> Vec<Atom> {
let mut ids = Vec::new();
for elem in elems.iter().flatten() {
match elem {
Pat::Ident(ident) => {
ids.push(ident.sym.clone());
}
Pat::Array(array) => {
ids.extend(collect_idents_in_array_pat(&array.elems));
}
Pat::Object(object) => {
ids.extend(collect_idents_in_object_pat(&object.props));
}
Pat::Rest(rest) => {
if let Pat::Ident(ident) = &*rest.arg {
ids.push(ident.sym.clone());
}
}
_ => {}
}
}
ids
}
fn collect_idents_in_object_pat(props: &[ObjectPatProp]) -> Vec<Atom> {
let mut ids = Vec::new();
for prop in props {
match prop {
ObjectPatProp::KeyValue(KeyValuePatProp { key, value }) => {
if let PropName::Ident(ident) = key {
ids.push(ident.sym.clone());
}
match &**value {
Pat::Ident(ident) => {
ids.push(ident.sym.clone());
}
Pat::Array(array) => {
ids.extend(collect_idents_in_array_pat(&array.elems));
}
Pat::Object(object) => {
ids.extend(collect_idents_in_object_pat(&object.props));
}
_ => {}
}
}
ObjectPatProp::Assign(AssignPatProp { key, .. }) => {
ids.push(key.sym.clone());
}
ObjectPatProp::Rest(RestPat { arg, .. }) => {
if let Pat::Ident(ident) = &**arg {
ids.push(ident.sym.clone());
}
}
}
}
ids
}
fn collect_idents_in_var_decls(decls: &[VarDeclarator]) -> Vec<Atom> {
let mut ids = Vec::new();
for decl in decls {
match &decl.name {
Pat::Ident(ident) => {
ids.push(ident.sym.clone());
}
Pat::Array(array) => {
ids.extend(collect_idents_in_array_pat(&array.elems));
}
Pat::Object(object) => {
ids.extend(collect_idents_in_object_pat(&object.props));
}
_ => {}
}
}
ids
}
|