File: ecma_integration.rs

package info (click to toggle)
rust-swc-core 35.0.0~ds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 62,816 kB
  • sloc: javascript: 873; xml: 538; sh: 358; makefile: 35; python: 5
file content (264 lines) | stat: -rw-r--r-- 8,477 bytes parent folder | download | duplicates (2)
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
use std::{
    env, fs,
    path::{Path, PathBuf},
    process::{Command, Stdio},
    sync::Arc,
};

use anyhow::{anyhow, Error};
use rustc_hash::FxHashMap;
use serde_json::json;
use swc_common::{
    errors::HANDLER,
    plugin::{metadata::TransformPluginMetadataContext, serialized::PluginSerializedBytes},
    sync::Lazy,
    FileName, Mark,
};
use swc_ecma_ast::{CallExpr, Callee, EsVersion, Expr, Lit, MemberExpr, Program, Str};
use swc_ecma_parser::{parse_file_as_program, Syntax};
use swc_ecma_visit::{Visit, VisitWith};
use swc_plugin_runner::{plugin_module_bytes::CompiledPluginModuleBytes, runtime::Runtime};
use testing::CARGO_TARGET_DIR;

/// Returns the path to the built plugin
fn build_plugin(dir: &Path) -> Result<PathBuf, Error> {
    {
        let mut cmd = Command::new("cargo");
        cmd.env("CARGO_TARGET_DIR", &*CARGO_TARGET_DIR);
        cmd.current_dir(dir);
        cmd.args(["build", "--target=wasm32-wasip1"])
            .stderr(Stdio::inherit());
        cmd.output()?;

        if !cmd
            .status()
            .expect("Exit code should be available")
            .success()
        {
            return Err(anyhow!("Failed to build plugin"));
        }
    }

    for entry in fs::read_dir(CARGO_TARGET_DIR.join("wasm32-wasip1").join("debug"))? {
        let entry = entry?;

        let s = entry.file_name().to_string_lossy().into_owned();
        if s.eq_ignore_ascii_case("swc_internal_plugin.wasm") {
            return Ok(entry.path());
        }
    }

    Err(anyhow!("Could not find built plugin"))
}

struct TestVisitor {
    pub plugin_transform_found: bool,
}

impl Visit for TestVisitor {
    fn visit_call_expr(&mut self, call: &CallExpr) {
        if let Callee::Expr(expr) = &call.callee {
            if let Expr::Member(MemberExpr { obj, .. }) = &**expr {
                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";
                        }
                    }
                }
            }
        }
    }
}

fn internal(rt: Arc<dyn Runtime>, module: &'static CompiledPluginModuleBytes) {
    use swc_common::plugin::serialized::VersionedSerializable;
    use swc_transform_common::output::capture;

    // run single plugin
    testing::run_test(false, |cm, _handler| {
        eprint!("First run start");

        let fm = cm.new_source_file(FileName::Anon.into(), "console.log(foo)");

        let program = parse_file_as_program(
            &fm,
            Syntax::Es(Default::default()),
            EsVersion::latest(),
            None,
            &mut Vec::new(),
        )
        .unwrap();

        let program = PluginSerializedBytes::try_serialize(&VersionedSerializable::new(program))
            .expect("Should serializable");
        let experimental_metadata: FxHashMap<String, String> = [
            (
                "TestExperimental".to_string(),
                "ExperimentalValue".to_string(),
            ),
            ("OtherTest".to_string(), "OtherVal".to_string()),
        ]
        .into_iter()
        .collect();

        let mut plugin_transform_executor = swc_plugin_runner::create_plugin_transform_executor(
            &cm,
            &Mark::new(),
            &Arc::new(TransformPluginMetadataContext::new(
                None,
                "development".to_string(),
                Some(experimental_metadata),
            )),
            None,
            Box::new(module.clone_module(&*rt)),
            Some(json!({ "pluginConfig": "testValue" })),
            rt.clone(),
        );

        /* [TODO]: reenable this later
        assert!(!plugin_transform_executor
            .plugin_core_diag
            .pkg_version
            .is_empty());
         */

        let (program_bytes, captured_output) = capture(|| {
            plugin_transform_executor
                .transform(&program, Some(false))
                .expect("Plugin should apply transform")
        });
        let captured_output = serde_json::to_string(&captured_output).unwrap();
        assert_eq!(captured_output, "{\"foo\":\"bar\"}");

        let program: Program = program_bytes
            .deserialize()
            .expect("Should able to deserialize")
            .into_inner();
        eprintln!("First run retured");
        let mut visitor = TestVisitor {
            plugin_transform_found: false,
        };
        program.visit_with(&mut visitor);

        visitor
            .plugin_transform_found
            .then_some(visitor.plugin_transform_found)
            .ok_or(())
    })
    .expect("Should able to run single plugin transform");

    // run single plugin with handler
    testing::run_test2(false, |cm, handler| {
        eprintln!("Second run start");
        let fm = cm.new_source_file(FileName::Anon.into(), "console.log(foo)");

        let program = parse_file_as_program(
            &fm,
            Syntax::Es(Default::default()),
            EsVersion::latest(),
            None,
            &mut Vec::new(),
        )
        .unwrap();

        let program = PluginSerializedBytes::try_serialize(&VersionedSerializable::new(program))
            .expect("Should serializable");
        let experimental_metadata: FxHashMap<String, String> = [
            (
                "TestExperimental".to_string(),
                "ExperimentalValue".to_string(),
            ),
            ("OtherTest".to_string(), "OtherVal".to_string()),
        ]
        .into_iter()
        .collect();

        let _res = HANDLER.set(&handler, || {
            let mut plugin_transform_executor = swc_plugin_runner::create_plugin_transform_executor(
                &cm,
                &Mark::new(),
                &Arc::new(TransformPluginMetadataContext::new(
                    None,
                    "development".to_string(),
                    Some(experimental_metadata),
                )),
                None,
                Box::new(module.clone_module(&*rt)),
                Some(json!({ "pluginConfig": "testValue" })),
                rt.clone(),
            );

            capture(|| {
                plugin_transform_executor
                    .transform(&program, Some(false))
                    .expect("Plugin should apply transform")
            })
            .1
        });

        eprintln!("Second run retured");

        Ok(())
    })
    .expect("Should able to run single plugin transform with handler");
}

#[test]
fn wasmer() {
    use swc_plugin_backend_wasmer::WasmerRuntime;

    static PLUGIN_BYTES: Lazy<CompiledPluginModuleBytes> = Lazy::new(|| {
        let path = build_plugin(
            &PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
                .join("tests")
                .join("fixture")
                .join("swc_internal_plugin"),
        )
        .unwrap();

        let raw_module_bytes = std::fs::read(&path).expect("Should able to read plugin bytes");
        let module = WasmerRuntime.prepare_module(&raw_module_bytes).unwrap();

        swc_plugin_runner::plugin_module_bytes::CompiledPluginModuleBytes::new(
            path.as_os_str()
                .to_str()
                .expect("Should able to get path")
                .to_string(),
            module,
        )
    });

    tokio::runtime::Runtime::new().unwrap().block_on(async {
        internal(Arc::new(WasmerRuntime), &PLUGIN_BYTES);
    });
}

#[test]
fn wasmtime() {
    use swc_plugin_backend_wasmtime::WasmtimeRuntime;

    static PLUGIN_BYTES: Lazy<CompiledPluginModuleBytes> = Lazy::new(|| {
        let path = build_plugin(
            &PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
                .join("tests")
                .join("fixture")
                .join("swc_internal_plugin"),
        )
        .unwrap();

        let raw_module_bytes = std::fs::read(&path).expect("Should able to read plugin bytes");
        let module = WasmtimeRuntime.prepare_module(&raw_module_bytes).unwrap();

        swc_plugin_runner::plugin_module_bytes::CompiledPluginModuleBytes::new(
            path.as_os_str()
                .to_str()
                .expect("Should able to get path")
                .to_string(),
            module,
        )
    });

    internal(Arc::new(WasmtimeRuntime), &PLUGIN_BYTES);
}