2021-11-28 11:02:14 +03:00
|
|
|
use std::{
|
|
|
|
env, fs,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::{Command, Stdio},
|
|
|
|
};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
|
|
|
use anyhow::{anyhow, Error};
|
2022-02-18 08:50:51 +03:00
|
|
|
use swc_common::{errors::HANDLER, plugin::Serialized, sync::Lazy, FileName};
|
2022-01-11 13:11:04 +03:00
|
|
|
use swc_ecma_ast::{CallExpr, Callee, EsVersion, Expr, Lit, MemberExpr, Program, Str};
|
2022-02-26 09:41:14 +03:00
|
|
|
use swc_ecma_parser::{parse_file_as_program, EsConfig, Syntax};
|
2022-01-10 15:34:16 +03:00
|
|
|
use swc_ecma_visit::{Visit, VisitWith};
|
2022-02-18 08:50:51 +03:00
|
|
|
use swc_plugin_runner::cache::PluginModuleCache;
|
2021-11-28 11:02:14 +03:00
|
|
|
|
|
|
|
/// Returns the path to the built plugin
|
|
|
|
fn build_plugin(dir: &Path) -> Result<PathBuf, Error> {
|
|
|
|
{
|
2022-01-24 09:13:30 +03:00
|
|
|
let mut cmd = Command::new("cargo");
|
2021-11-28 11:02:14 +03:00
|
|
|
cmd.current_dir(dir);
|
2022-01-24 09:13:30 +03:00
|
|
|
cmd.args(["build", "--target=wasm32-wasi"])
|
|
|
|
.stderr(Stdio::inherit());
|
|
|
|
cmd.output()?;
|
2021-11-28 11:02:14 +03:00
|
|
|
}
|
|
|
|
|
2022-01-15 07:40:00 +03:00
|
|
|
for entry in fs::read_dir(&dir.join("target").join("wasm32-wasi").join("debug"))? {
|
2021-11-28 11:02:14 +03:00
|
|
|
let entry = entry?;
|
|
|
|
|
|
|
|
let s = entry.file_name().to_string_lossy().into_owned();
|
2022-01-07 09:13:46 +03:00
|
|
|
if s.eq_ignore_ascii_case("swc_internal_plugin.wasm") {
|
2021-11-28 11:02:14 +03:00
|
|
|
return Ok(entry.path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(anyhow!("Could not find built plugin"))
|
|
|
|
}
|
|
|
|
|
2022-01-10 15:34:16 +03:00
|
|
|
struct TestVisitor {
|
|
|
|
pub plugin_transform_found: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visit for TestVisitor {
|
|
|
|
fn visit_call_expr(&mut self, call: &CallExpr) {
|
2022-01-10 16:54:42 +03:00
|
|
|
if let Callee::Expr(expr) = &call.callee {
|
2022-01-10 15:34:16 +03:00
|
|
|
if let Expr::Member(MemberExpr { obj, .. }) = &**expr {
|
2022-01-10 16:54:42 +03:00
|
|
|
if let Expr::Ident(ident) = &**obj {
|
|
|
|
if ident.sym == *"console" {
|
|
|
|
let args = &*(call.args[0].expr);
|
|
|
|
if let Expr::Lit(Lit::Str(Str { value, .. })) = args {
|
|
|
|
self.plugin_transform_found = value == "changed_via_plugin";
|
2022-01-10 15:34:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 11:02:14 +03:00
|
|
|
#[test]
|
|
|
|
fn internal() -> Result<(), Error> {
|
|
|
|
let path = build_plugin(
|
|
|
|
&PathBuf::from(env::var("CARGO_MANIFEST_DIR")?)
|
|
|
|
.join("..")
|
|
|
|
.join("..")
|
|
|
|
.join("tests")
|
|
|
|
.join("rust-plugins")
|
|
|
|
.join("swc_internal_plugin"),
|
|
|
|
)?;
|
|
|
|
|
2022-01-11 13:11:04 +03:00
|
|
|
// run single plugin
|
2021-11-28 11:02:14 +03:00
|
|
|
testing::run_test(false, |cm, _handler| {
|
|
|
|
let fm = cm.new_source_file(FileName::Anon, "console.log(foo)".into());
|
|
|
|
|
2022-02-26 09:41:14 +03:00
|
|
|
let program = parse_file_as_program(
|
|
|
|
&fm,
|
2021-11-28 11:02:14 +03:00
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
EsVersion::latest(),
|
|
|
|
None,
|
2022-02-26 09:41:14 +03:00
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-11-28 11:02:14 +03:00
|
|
|
|
2022-01-11 13:11:04 +03:00
|
|
|
let program = Serialized::serialize(&program).expect("Should serializable");
|
2022-01-12 12:32:52 +03:00
|
|
|
let config = Serialized::serialize(&"{}".to_string()).expect("Should serializable");
|
2022-02-15 11:03:17 +03:00
|
|
|
let context = Serialized::serialize(&"{sourceFileName: 'single_plugin_test'}".to_string())
|
|
|
|
.expect("Should serializable");
|
2022-01-11 13:11:04 +03:00
|
|
|
|
2022-02-18 08:50:51 +03:00
|
|
|
let cache: Lazy<PluginModuleCache> = Lazy::new(PluginModuleCache::new);
|
|
|
|
|
2022-03-16 01:45:40 +03:00
|
|
|
let program_bytes = swc_plugin_runner::apply_transform_plugin(
|
2022-02-15 11:03:17 +03:00
|
|
|
"internal-test",
|
|
|
|
&path,
|
2022-02-18 08:50:51 +03:00
|
|
|
&cache,
|
2022-02-15 11:03:17 +03:00
|
|
|
program,
|
|
|
|
config,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.expect("Plugin should apply transform");
|
2022-01-10 15:34:16 +03:00
|
|
|
|
2022-01-11 13:11:04 +03:00
|
|
|
let program: Program =
|
|
|
|
Serialized::deserialize(&program_bytes).expect("Should able to deserialize");
|
|
|
|
let mut visitor = TestVisitor {
|
|
|
|
plugin_transform_found: false,
|
|
|
|
};
|
|
|
|
program.visit_with(&mut visitor);
|
|
|
|
|
|
|
|
visitor
|
|
|
|
.plugin_transform_found
|
|
|
|
.then(|| visitor.plugin_transform_found)
|
|
|
|
.ok_or(())
|
|
|
|
})
|
|
|
|
.expect("Should able to run single plugin transform");
|
|
|
|
|
2022-01-25 11:13:39 +03:00
|
|
|
// run single plugin with handler
|
|
|
|
testing::run_test2(false, |cm, handler| {
|
|
|
|
let fm = cm.new_source_file(FileName::Anon, "console.log(foo)".into());
|
|
|
|
|
2022-02-26 09:41:14 +03:00
|
|
|
let program = parse_file_as_program(
|
|
|
|
&fm,
|
2022-01-25 11:13:39 +03:00
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
EsVersion::latest(),
|
|
|
|
None,
|
2022-02-26 09:41:14 +03:00
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-01-25 11:13:39 +03:00
|
|
|
|
|
|
|
let program = Serialized::serialize(&program).expect("Should serializable");
|
|
|
|
let config = Serialized::serialize(&"{}".to_string()).expect("Should serializable");
|
2022-02-15 11:03:17 +03:00
|
|
|
let context =
|
|
|
|
Serialized::serialize(&"{sourceFileName: 'single_plugin_handler_test'}".to_string())
|
|
|
|
.expect("Should serializable");
|
2022-01-25 11:13:39 +03:00
|
|
|
|
2022-02-18 08:50:51 +03:00
|
|
|
let cache: Lazy<PluginModuleCache> = Lazy::new(PluginModuleCache::new);
|
|
|
|
|
2022-01-25 11:13:39 +03:00
|
|
|
let _res = HANDLER.set(&handler, || {
|
2022-03-16 01:45:40 +03:00
|
|
|
swc_plugin_runner::apply_transform_plugin(
|
2022-02-15 11:03:17 +03:00
|
|
|
"internal-test",
|
|
|
|
&path,
|
2022-02-18 08:50:51 +03:00
|
|
|
&cache,
|
2022-02-15 11:03:17 +03:00
|
|
|
program,
|
|
|
|
config,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.expect("Plugin should apply transform")
|
2022-01-25 11:13:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.expect("Should able to run single plugin transform with handler");
|
|
|
|
|
2022-01-11 13:11:04 +03:00
|
|
|
// Run multiple plugins.
|
|
|
|
testing::run_test(false, |cm, _handler| {
|
|
|
|
let fm = cm.new_source_file(FileName::Anon, "console.log(foo)".into());
|
|
|
|
|
2022-02-26 09:41:14 +03:00
|
|
|
let program = parse_file_as_program(
|
|
|
|
&fm,
|
2022-01-11 13:11:04 +03:00
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
EsVersion::latest(),
|
|
|
|
None,
|
2022-02-26 09:41:14 +03:00
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-01-11 13:11:04 +03:00
|
|
|
|
|
|
|
let mut serialized_program = Serialized::serialize(&program).expect("Should serializable");
|
2022-02-18 08:50:51 +03:00
|
|
|
let cache: Lazy<PluginModuleCache> = Lazy::new(PluginModuleCache::new);
|
2022-01-11 13:11:04 +03:00
|
|
|
|
2022-03-16 01:45:40 +03:00
|
|
|
serialized_program = swc_plugin_runner::apply_transform_plugin(
|
2022-01-11 13:11:04 +03:00
|
|
|
"internal-test",
|
|
|
|
&path,
|
2022-02-18 08:50:51 +03:00
|
|
|
&cache,
|
2022-01-11 13:11:04 +03:00
|
|
|
serialized_program,
|
2022-02-15 11:03:17 +03:00
|
|
|
Serialized::serialize(&"{}".to_string()).expect("Should serializable"),
|
|
|
|
Serialized::serialize(&"{sourceFileName: 'multiple_plugin_test'}".to_string())
|
|
|
|
.expect("Should serializable"),
|
2022-01-11 13:11:04 +03:00
|
|
|
)
|
|
|
|
.expect("Plugin should apply transform");
|
|
|
|
|
|
|
|
// TODO: we'll need to apply 2 different plugins
|
2022-03-16 01:45:40 +03:00
|
|
|
serialized_program = swc_plugin_runner::apply_transform_plugin(
|
2022-01-11 13:11:04 +03:00
|
|
|
"internal-test",
|
|
|
|
&path,
|
2022-02-18 08:50:51 +03:00
|
|
|
&cache,
|
2022-01-11 13:11:04 +03:00
|
|
|
serialized_program,
|
2022-02-15 11:03:17 +03:00
|
|
|
Serialized::serialize(&"{}".to_string()).expect("Should serializable"),
|
|
|
|
Serialized::serialize(&"{sourceFileName: 'multiple_plugin_test2'}".to_string())
|
|
|
|
.expect("Should serializable"),
|
2022-01-11 13:11:04 +03:00
|
|
|
)
|
|
|
|
.expect("Plugin should apply transform");
|
|
|
|
|
|
|
|
let program: Program =
|
|
|
|
Serialized::deserialize(&serialized_program).expect("Should able to deserialize");
|
2022-01-10 15:34:16 +03:00
|
|
|
let mut visitor = TestVisitor {
|
|
|
|
plugin_transform_found: false,
|
|
|
|
};
|
|
|
|
program.visit_with(&mut visitor);
|
2021-11-28 11:02:14 +03:00
|
|
|
|
2022-01-10 15:34:16 +03:00
|
|
|
visitor
|
|
|
|
.plugin_transform_found
|
|
|
|
.then(|| visitor.plugin_transform_found)
|
|
|
|
.ok_or(())
|
2021-11-28 11:02:14 +03:00
|
|
|
})
|
2022-01-11 13:11:04 +03:00
|
|
|
.expect("Should able to run multiple plugins transform");
|
2021-11-28 11:02:14 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|