2020-08-14 19:47:28 +03:00
|
|
|
use std::{collections::HashMap, io::stdout};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
|
|
|
use anyhow::Error;
|
2020-11-05 03:53:18 +03:00
|
|
|
use swc_bundler::{BundleKind, Bundler, Config, Hook, Load, ModuleData, ModuleRecord, Resolve};
|
2020-10-02 05:07:40 +03:00
|
|
|
use swc_common::{sync::Lrc, FileName, FilePathMapping, Globals, SourceMap, Span};
|
2020-11-02 09:22:21 +03:00
|
|
|
use swc_ecma_ast::KeyValueProp;
|
2020-08-12 16:18:47 +03:00
|
|
|
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
|
2023-03-30 11:06:02 +03:00
|
|
|
use swc_ecma_parser::{parse_file_as_module, Syntax};
|
2020-08-12 16:18:47 +03:00
|
|
|
|
|
|
|
fn main() {
|
2021-09-27 12:19:15 +03:00
|
|
|
let _log = testing::init();
|
2020-08-12 16:18:47 +03:00
|
|
|
|
|
|
|
let globals = Globals::new();
|
|
|
|
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
// This example does not use core modules.
|
|
|
|
let external_modules = vec![];
|
2021-10-15 06:49:37 +03:00
|
|
|
let mut bundler = Bundler::new(
|
2020-08-12 16:18:47 +03:00
|
|
|
&globals,
|
|
|
|
cm.clone(),
|
|
|
|
PathLoader { cm: cm.clone() },
|
|
|
|
PathResolver,
|
|
|
|
Config {
|
|
|
|
require: true,
|
|
|
|
external_modules,
|
2020-09-04 16:40:03 +03:00
|
|
|
..Default::default()
|
2020-08-12 16:18:47 +03:00
|
|
|
},
|
2020-10-02 05:07:40 +03:00
|
|
|
Box::new(Noop),
|
2020-08-12 16:18:47 +03:00
|
|
|
);
|
2020-08-14 19:47:28 +03:00
|
|
|
let mut entries = HashMap::default();
|
2020-08-12 16:18:47 +03:00
|
|
|
entries.insert("main".to_string(), FileName::Real("assets/main.js".into()));
|
|
|
|
|
|
|
|
let mut bundles = bundler.bundle(entries).expect("failed to bundle");
|
|
|
|
assert_eq!(
|
|
|
|
bundles.len(),
|
|
|
|
1,
|
|
|
|
"There's no conditional / dynamic imports and we provided only one entry"
|
|
|
|
);
|
|
|
|
let bundle = bundles.pop().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
bundle.kind,
|
|
|
|
BundleKind::Named {
|
|
|
|
name: "main".into()
|
|
|
|
},
|
|
|
|
"We provided it"
|
|
|
|
);
|
|
|
|
|
|
|
|
let wr = stdout();
|
|
|
|
let mut emitter = Emitter {
|
2023-09-05 15:24:25 +03:00
|
|
|
cfg: swc_ecma_codegen::Config::default(),
|
2020-08-12 16:18:47 +03:00
|
|
|
cm: cm.clone(),
|
|
|
|
comments: None,
|
2022-01-13 17:06:11 +03:00
|
|
|
wr: Box::new(JsWriter::new(cm, "\n", wr.lock(), None)),
|
2020-08-12 16:18:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
emitter.emit_module(&bundle.module).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PathLoader {
|
|
|
|
cm: Lrc<SourceMap>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Load for PathLoader {
|
2020-11-05 03:53:18 +03:00
|
|
|
fn load(&self, file: &FileName) -> Result<ModuleData, Error> {
|
2020-08-12 16:18:47 +03:00
|
|
|
let file = match file {
|
|
|
|
FileName::Real(v) => v,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let fm = self.cm.load_file(file)?;
|
2022-02-26 09:41:14 +03:00
|
|
|
|
|
|
|
let module = parse_file_as_module(
|
|
|
|
&fm,
|
2023-03-30 11:06:02 +03:00
|
|
|
Syntax::Es(Default::default()),
|
2020-08-12 16:18:47 +03:00
|
|
|
Default::default(),
|
|
|
|
None,
|
2022-02-26 09:41:14 +03:00
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.expect("This should not happen");
|
2020-08-12 16:18:47 +03:00
|
|
|
|
2020-11-05 03:53:18 +03:00
|
|
|
Ok(ModuleData {
|
|
|
|
fm,
|
|
|
|
module,
|
|
|
|
helpers: Default::default(),
|
|
|
|
})
|
2020-08-12 16:18:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
struct PathResolver;
|
|
|
|
|
|
|
|
impl Resolve for PathResolver {
|
|
|
|
fn resolve(&self, base: &FileName, module_specifier: &str) -> Result<FileName, Error> {
|
|
|
|
assert!(
|
2022-01-13 17:06:11 +03:00
|
|
|
module_specifier.starts_with('.'),
|
2020-08-12 16:18:47 +03:00
|
|
|
"We are not using node_modules within this example"
|
|
|
|
);
|
|
|
|
|
|
|
|
let base = match base {
|
|
|
|
FileName::Real(v) => v,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(FileName::Real(
|
|
|
|
base.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join(module_specifier)
|
|
|
|
.with_extension("js"),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 05:07:40 +03:00
|
|
|
|
|
|
|
struct Noop;
|
|
|
|
|
|
|
|
impl Hook for Noop {
|
2020-11-02 09:22:21 +03:00
|
|
|
fn get_import_meta_props(&self, _: Span, _: &ModuleRecord) -> Result<Vec<KeyValueProp>, Error> {
|
2020-10-02 05:07:40 +03:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|