swc_bundler/bundler/load.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 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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
#![allow(dead_code)]
use anyhow::{Context, Error};
use is_macro::Is;
#[cfg(feature = "rayon")]
use rayon::iter::ParallelIterator;
use swc_common::{
sync::{Lock, Lrc},
FileName, SourceFile, SyntaxContext,
};
use swc_ecma_ast::{
CallExpr, Callee, Expr, Ident, ImportDecl, ImportSpecifier, MemberExpr, MemberProp, Module,
ModuleDecl, ModuleExportName, Str, SuperProp, SuperPropExpr,
};
use swc_ecma_transforms_base::resolver;
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith,
};
use super::{export::Exports, helpers::Helpers, Bundler};
use crate::{
bundler::{export::RawExports, import::RawImports},
id::{Id, ModuleId},
load::ModuleData,
util,
util::IntoParallelIterator,
Load, Resolve,
};
/// Module after applying transformations.
#[derive(Debug, Clone)]
pub(crate) struct TransformedModule {
pub id: ModuleId,
pub fm: Lrc<SourceFile>,
pub module: Lrc<Module>,
pub imports: Lrc<Imports>,
pub exports: Lrc<Exports>,
/// If false, the module will be wrapped with a small helper function.
pub is_es6: bool,
/// Used helpers
pub helpers: Lrc<Helpers>,
pub swc_helpers: Lrc<Lock<swc_ecma_transforms_base::helpers::HelperData>>,
local_ctxt: SyntaxContext,
export_ctxt: SyntaxContext,
}
impl TransformedModule {
/// [SyntaxContext] for exported items.
pub fn export_ctxt(&self) -> SyntaxContext {
self.export_ctxt
}
/// Top level contexts.
pub fn local_ctxt(&self) -> SyntaxContext {
self.local_ctxt
}
}
impl<L, R> Bundler<'_, L, R>
where
L: Load,
R: Resolve,
{
/// Phase 1 (discovery)
///
/// We apply transforms at this phase to make cache efficient.
/// As we cache in this phase, changing dependency does not affect cache.
pub(super) fn load_transformed(
&self,
file_name: &FileName,
) -> Result<Option<TransformedModule>, Error> {
self.run(|| {
tracing::trace!("load_transformed: ({})", file_name);
// In case of common module
if let Some(cached) = self.scope.get_module_by_path(file_name) {
tracing::debug!("Cached: {}", file_name);
return Ok(Some(cached));
}
let (_, data) = self.load(file_name).context("Bundler.load() failed")?;
let (v, mut files) = self
.analyze(file_name, data)
.context("failed to analyze module")?;
files.dedup_by_key(|v| v.1.clone());
tracing::debug!(
"({:?}, {:?}, {:?}) Storing module: {}",
v.id,
v.local_ctxt(),
v.export_ctxt(),
file_name
);
self.scope.store_module(v.clone());
// Load dependencies and store them in the `Scope`
let results = files
.into_par_iter()
.map(|(_src, path)| {
tracing::trace!("loading dependency: {}", path);
self.load_transformed(&path)
})
.collect::<Vec<_>>();
// Do tasks in parallel, and then wait for result
for result in results {
result?;
}
Ok(Some(v))
})
}
fn load(&self, file_name: &FileName) -> Result<(ModuleId, ModuleData), Error> {
self.run(|| {
let (module_id, _, _) = self.scope.module_id_gen.gen(file_name);
let data = self
.loader
.load(file_name)
.with_context(|| format!("Bundler.loader.load({}) failed", file_name))?;
self.scope.mark_as_loaded(module_id);
Ok((module_id, data))
})
}
/// This methods returns [Source]s which should be loaded.
fn analyze(
&self,
file_name: &FileName,
mut data: ModuleData,
) -> Result<(TransformedModule, Vec<(Source, Lrc<FileName>)>), Error> {
self.run(|| {
tracing::trace!("transform_module({})", data.fm.name);
let (id, local_mark, export_mark) = self.scope.module_id_gen.gen(file_name);
data.module.visit_mut_with(&mut ClearMark);
data.module
.visit_mut_with(&mut resolver(self.unresolved_mark, local_mark, false));
// {
// let code = self
// .swc
// .print(
// &module.clone().fold_with(&mut HygieneVisualizer),
// SourceMapsConfig::Bool(false),
// None,
// false,
// )
// .unwrap()
// .code;
//
// println!("Resolved:\n{}\n\n", code);
// }
let imports = self.extract_import_info(file_name, &mut data.module, local_mark);
// {
// let code = self
// .swc
// .print(
// &module.clone().fold_with(&mut HygieneVisualizer),
// SourceMapsConfig::Bool(false),
// None,
// false,
// )
// .unwrap()
// .code;
//
// println!("After imports:\n{}\n", code,);
// }
let exports = self.extract_export_info(
file_name,
&mut data.module,
SyntaxContext::empty().apply_mark(export_mark),
);
let is_es6 = if !self.config.require {
true
} else {
let mut v = Es6ModuleDetector {
forced_es6: false,
found_other: false,
};
data.module.visit_with(&mut v);
v.forced_es6 || !v.found_other
};
let (imports, exports) = util::join(
|| self.resolve_imports(file_name, imports),
|| self.resolve_exports(file_name, exports),
);
let (imports, mut import_files) = imports?;
let (exports, reexport_files) = exports?;
import_files.extend(reexport_files);
Ok((
TransformedModule {
id,
fm: data.fm,
module: Lrc::new(data.module),
imports: Lrc::new(imports),
exports: Lrc::new(exports),
is_es6,
helpers: Default::default(),
swc_helpers: Lrc::new(Lock::new(data.helpers.data())),
local_ctxt: SyntaxContext::empty().apply_mark(local_mark),
export_ctxt: SyntaxContext::empty().apply_mark(export_mark),
},
import_files,
))
})
}
/// Resolve re-exports.
fn resolve_exports(
&self,
base: &FileName,
raw: RawExports,
) -> Result<(Exports, Vec<(Source, Lrc<FileName>)>), Error> {
self.run(|| {
tracing::trace!("resolve_exports({})", base);
let mut files = Vec::new();
let mut exports = Exports::default();
let items = raw
.items
.into_par_iter()
.map(|(src, ss)| -> Result<_, Error> {
self.run(|| {
let info = match src {
Some(src) => {
let name = self.resolve(base, &src.value)?;
let (id, local_mark, export_mark) =
self.scope.module_id_gen.gen(&name);
Some((id, local_mark, export_mark, name, src))
}
None => None,
};
Ok((info, ss))
})
})
.collect::<Vec<_>>();
for res in items {
let (info, specifiers) = res?;
match info {
None => exports.items.extend(specifiers),
Some((id, local_mark, export_mark, name, src)) => {
//
let src = Source {
is_loaded_synchronously: true,
is_unconditional: false,
module_id: id,
local_ctxt: SyntaxContext::empty().apply_mark(local_mark),
export_ctxt: SyntaxContext::empty().apply_mark(export_mark),
src,
};
exports.reexports.push((src.clone(), specifiers));
files.push((src, name));
}
}
}
Ok((exports, files))
})
}
/// Resolve dependencies
fn resolve_imports(
&self,
base: &FileName,
info: RawImports,
) -> Result<(Imports, Vec<(Source, Lrc<FileName>)>), Error> {
self.run(|| {
tracing::trace!("resolve_imports({})", base);
let mut files = Vec::new();
let mut merged = Imports::default();
let RawImports {
imports,
lazy_imports,
dynamic_imports,
forced_ns,
} = info;
let loaded = imports
.into_par_iter()
.map(|v| (v, false, true))
.chain(lazy_imports.into_par_iter().map(|v| (v, false, false)))
.chain(dynamic_imports.into_par_iter().map(|src| {
(
ImportDecl {
span: src.span,
specifiers: Vec::new(),
src: Box::new(src),
type_only: false,
with: None,
phase: Default::default(),
},
true,
false,
)
}))
.map(|(decl, dynamic, unconditional)| -> Result<_, Error> {
self.run(|| {
//
let file_name = self.resolve(base, &decl.src.value)?;
let (id, local_mark, export_mark) =
self.scope.module_id_gen.gen(&file_name);
Ok((
id,
local_mark,
export_mark,
file_name,
decl,
dynamic,
unconditional,
))
})
})
.collect::<Vec<_>>();
for res in loaded {
// TODO: Report error and proceed instead of returning an error
let (id, local_mark, export_mark, file_name, decl, is_dynamic, is_unconditional) =
res?;
let src = Source {
is_loaded_synchronously: !is_dynamic,
is_unconditional,
module_id: id,
local_ctxt: SyntaxContext::empty().apply_mark(local_mark),
export_ctxt: SyntaxContext::empty().apply_mark(export_mark),
src: *decl.src,
};
files.push((src.clone(), file_name));
// TODO: Handle rename
let mut specifiers = Vec::new();
for s in decl.specifiers {
match s {
ImportSpecifier::Named(s) => {
let imported = match s.imported {
Some(ModuleExportName::Ident(ident)) => Some(ident),
Some(ModuleExportName::Str(..)) => {
unimplemented!("module string names unimplemented")
}
_ => None,
};
specifiers.push(Specifier::Specific {
local: s.local.into(),
alias: imported.map(From::from),
})
}
ImportSpecifier::Default(s) => specifiers.push(Specifier::Specific {
local: s.local.into(),
alias: Some(Id::new("default".into(), SyntaxContext::empty())),
}),
ImportSpecifier::Namespace(s) => {
specifiers.push(Specifier::Namespace {
local: s.local.into(),
all: forced_ns.contains(&src.src.value),
});
}
}
}
merged.specifiers.push((src, specifiers));
}
Ok((merged, files))
})
}
}
#[derive(Debug, Default)]
pub(crate) struct Imports {
/// If imported ids are empty, it is a side-effect import.
pub specifiers: Vec<(Source, Vec<Specifier>)>,
}
/// Clone is relatively cheap
#[derive(Debug, Clone, Is)]
pub(crate) enum Specifier {
Specific {
local: Id,
alias: Option<Id>,
},
Namespace {
local: Id,
/// True for `import * as foo from 'foo'; foo[computedKey()]`
all: bool,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct Source {
pub is_loaded_synchronously: bool,
pub is_unconditional: bool,
pub module_id: ModuleId,
pub local_ctxt: SyntaxContext,
pub export_ctxt: SyntaxContext,
// Clone is relatively cheap, thanks to hstr.
pub src: Str,
}
struct Es6ModuleDetector {
/// If import statement or export is detected, it's an es6 module regardless
/// of other codes.
forced_es6: bool,
/// True if other module system is detected.
found_other: bool,
}
impl Visit for Es6ModuleDetector {
noop_visit_type!();
fn visit_call_expr(&mut self, e: &CallExpr) {
e.visit_children_with(self);
match &e.callee {
Callee::Expr(e) => {
if let Expr::Ident(Ident { sym: _require, .. }) = &**e {
self.found_other = true;
}
}
Callee::Super(_) | Callee::Import(_) => {}
}
}
fn visit_member_expr(&mut self, e: &MemberExpr) {
e.obj.visit_with(self);
if let MemberProp::Computed(c) = &e.prop {
c.visit_with(self);
}
if let Expr::Ident(i) = &*e.obj {
// TODO: Check syntax context (Check if marker is the global mark)
if i.sym == *"module" {
self.found_other = true;
}
if i.sym == *"exports" {
self.found_other = true;
}
}
}
fn visit_super_prop_expr(&mut self, e: &SuperPropExpr) {
if let SuperProp::Computed(c) = &e.prop {
c.visit_with(self);
}
}
fn visit_module_decl(&mut self, decl: &ModuleDecl) {
match decl {
ModuleDecl::Import(_)
| ModuleDecl::ExportDecl(_)
| ModuleDecl::ExportNamed(_)
| ModuleDecl::ExportDefaultDecl(_)
| ModuleDecl::ExportDefaultExpr(_)
| ModuleDecl::ExportAll(_) => {
self.forced_es6 = true;
}
ModuleDecl::TsImportEquals(_) => {}
ModuleDecl::TsExportAssignment(_) => {}
ModuleDecl::TsNamespaceExport(_) => {}
}
}
}
#[derive(Clone, Copy)]
struct ClearMark;
impl VisitMut for ClearMark {
noop_visit_mut_type!(fail);
fn visit_mut_ident(&mut self, ident: &mut Ident) {
ident.ctxt = SyntaxContext::empty();
}
}