2020-10-09 13:10:25 +03:00
|
|
|
use std::{
|
|
|
|
self,
|
|
|
|
collections::HashMap,
|
2021-08-14 09:34:04 +03:00
|
|
|
fs::read_dir,
|
2020-10-09 13:10:25 +03:00
|
|
|
io,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
|
|
|
use anyhow::Error;
|
2020-11-02 09:22:21 +03:00
|
|
|
use swc_atoms::js_word;
|
2020-12-02 16:48:35 +03:00
|
|
|
use swc_bundler::{BundleKind, Bundler, Config, ModuleRecord};
|
2022-09-01 19:30:21 +03:00
|
|
|
use swc_common::{errors::HANDLER, FileName, Globals, Span};
|
2020-11-02 09:22:21 +03:00
|
|
|
use swc_ecma_ast::{
|
2022-01-10 16:54:42 +03:00
|
|
|
Bool, Expr, Ident, KeyValueProp, Lit, MemberExpr, MemberProp, MetaPropExpr, MetaPropKind,
|
|
|
|
PropName, Str,
|
2020-11-02 09:22:21 +03:00
|
|
|
};
|
2020-10-09 13:10:25 +03:00
|
|
|
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
|
2021-08-16 11:34:53 +03:00
|
|
|
use swc_ecma_loader::NODE_BUILTINS;
|
2021-10-09 11:08:13 +03:00
|
|
|
use swc_ecma_transforms_base::fixer::fixer;
|
2020-10-09 13:10:25 +03:00
|
|
|
use swc_ecma_visit::FoldWith;
|
|
|
|
use testing::NormalizedOutput;
|
|
|
|
|
2022-02-09 09:33:32 +03:00
|
|
|
use self::common::*;
|
|
|
|
|
2020-12-02 16:48:35 +03:00
|
|
|
#[path = "common/mod.rs"]
|
|
|
|
mod common;
|
|
|
|
|
2021-08-14 09:34:04 +03:00
|
|
|
fn do_test(entry: &Path, entries: HashMap<String, FileName>, inline: bool) {
|
2022-09-01 19:30:21 +03:00
|
|
|
testing::run_test2(false, |cm, handler| {
|
|
|
|
HANDLER.set(&handler, || {
|
|
|
|
let globals = Globals::default();
|
|
|
|
let mut bundler = Bundler::new(
|
|
|
|
&globals,
|
|
|
|
cm.clone(),
|
|
|
|
Loader { cm: cm.clone() },
|
|
|
|
NodeResolver,
|
|
|
|
Config {
|
|
|
|
require: true,
|
|
|
|
disable_inliner: !inline,
|
|
|
|
external_modules: NODE_BUILTINS.iter().copied().map(From::from).collect(),
|
|
|
|
module: Default::default(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
Box::new(Hook),
|
|
|
|
);
|
|
|
|
|
|
|
|
let modules = bundler
|
|
|
|
.bundle(entries)
|
|
|
|
.map_err(|err| println!("{:?}", err))?;
|
|
|
|
println!("Bundled as {} modules", modules.len());
|
|
|
|
|
|
|
|
let mut error = false;
|
|
|
|
|
|
|
|
for bundled in modules {
|
|
|
|
let code = {
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut emitter = Emitter {
|
2023-03-30 11:06:02 +03:00
|
|
|
cfg: Default::default(),
|
2022-09-01 19:30:21 +03:00
|
|
|
cm: cm.clone(),
|
|
|
|
comments: None,
|
|
|
|
wr: Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None)),
|
|
|
|
};
|
|
|
|
|
|
|
|
emitter
|
|
|
|
.emit_module(&bundled.module.fold_with(&mut fixer(None)))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
String::from_utf8_lossy(&buf).to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
let name = match bundled.kind {
|
|
|
|
BundleKind::Named { name } | BundleKind::Lib { name } => PathBuf::from(name),
|
|
|
|
BundleKind::Dynamic => format!("dynamic.{}.js", bundled.id).into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let output_dir = entry.join("output");
|
|
|
|
|
|
|
|
let output_path = if inline {
|
|
|
|
output_dir
|
|
|
|
.join(name.file_name().unwrap())
|
|
|
|
.with_file_name(format!(
|
|
|
|
"{}.inlined.{}",
|
|
|
|
name.file_stem().unwrap().to_string_lossy(),
|
|
|
|
name.extension().unwrap().to_string_lossy()
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
output_dir.join(name.file_name().unwrap())
|
|
|
|
};
|
|
|
|
|
|
|
|
println!("Printing {}", output_path.display());
|
|
|
|
|
|
|
|
let s = NormalizedOutput::from(code.to_string());
|
|
|
|
|
|
|
|
match s.compare_to_file(&output_path) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
println!("Diff: {:?}", err);
|
|
|
|
error = true;
|
|
|
|
}
|
2020-10-09 13:10:25 +03:00
|
|
|
}
|
2021-01-20 10:10:12 +03:00
|
|
|
}
|
2020-10-09 13:10:25 +03:00
|
|
|
|
2022-09-01 19:30:21 +03:00
|
|
|
if error {
|
|
|
|
return Err(());
|
|
|
|
}
|
2020-10-09 13:10:25 +03:00
|
|
|
|
2022-09-01 19:30:21 +03:00
|
|
|
Ok(())
|
|
|
|
})
|
2021-01-20 10:10:12 +03:00
|
|
|
})
|
|
|
|
.expect("failed to process a module");
|
2020-10-09 13:10:25 +03:00
|
|
|
}
|
|
|
|
|
2021-08-14 09:34:04 +03:00
|
|
|
#[testing::fixture("tests/fixture/**/input")]
|
|
|
|
fn pass(entry: PathBuf) {
|
|
|
|
let entries = read_dir(&entry)
|
|
|
|
.unwrap()
|
|
|
|
.filter(|e| match e {
|
2022-01-13 17:06:11 +03:00
|
|
|
Ok(e) => e
|
|
|
|
.path()
|
|
|
|
.file_name()
|
|
|
|
.unwrap()
|
|
|
|
.to_string_lossy()
|
|
|
|
.starts_with("entry"),
|
2021-08-14 09:34:04 +03:00
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
.map(|e| -> Result<_, io::Error> {
|
|
|
|
let e = e?;
|
|
|
|
Ok((
|
|
|
|
e.file_name().to_string_lossy().to_string(),
|
|
|
|
FileName::Real(e.path()),
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.collect::<Result<HashMap<_, _>, _>>()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
do_test(entry.parent().unwrap(), entries.clone(), true);
|
|
|
|
do_test(entry.parent().unwrap(), entries, false);
|
2020-10-09 13:10:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Hook;
|
|
|
|
|
|
|
|
impl swc_bundler::Hook for Hook {
|
2020-11-02 09:22:21 +03:00
|
|
|
fn get_import_meta_props(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
module_record: &ModuleRecord,
|
|
|
|
) -> Result<Vec<KeyValueProp>, Error> {
|
2022-03-22 10:54:08 +03:00
|
|
|
let file_name = module_record.file_name.to_string();
|
|
|
|
|
2020-11-02 09:22:21 +03:00
|
|
|
Ok(vec![
|
|
|
|
KeyValueProp {
|
|
|
|
key: PropName::Ident(Ident::new(js_word!("url"), span)),
|
|
|
|
value: Box::new(Expr::Lit(Lit::Str(Str {
|
|
|
|
span,
|
2022-03-22 10:54:08 +03:00
|
|
|
raw: None,
|
|
|
|
value: file_name.into(),
|
2020-11-02 09:22:21 +03:00
|
|
|
}))),
|
|
|
|
},
|
|
|
|
KeyValueProp {
|
|
|
|
key: PropName::Ident(Ident::new(js_word!("main"), span)),
|
|
|
|
value: Box::new(if module_record.is_entry {
|
|
|
|
Expr::Member(MemberExpr {
|
|
|
|
span,
|
2022-01-10 16:54:42 +03:00
|
|
|
obj: Box::new(Expr::MetaProp(MetaPropExpr {
|
|
|
|
span,
|
|
|
|
kind: MetaPropKind::ImportMeta,
|
|
|
|
})),
|
|
|
|
prop: MemberProp::Ident(Ident::new(js_word!("main"), span)),
|
2020-11-02 09:22:21 +03:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Expr::Lit(Lit::Bool(Bool { span, value: false }))
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
])
|
2020-10-09 13:10:25 +03:00
|
|
|
}
|
|
|
|
}
|