-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
plugin.rs
120 lines (97 loc) · 3.4 KB
/
plugin.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
//! This module always exists because cfg attributes are not stabilized in
//! expressions at the moment.
#![cfg_attr(not(feature = "plugin"), allow(unused))]
use serde::{Deserialize, Serialize};
#[cfg(feature = "plugin")]
use swc_ecma_ast::*;
#[cfg(not(feature = "plugin"))]
use swc_ecma_transforms::pass::noop;
use swc_ecma_visit::{noop_fold_type, Fold};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct PluginConfig(String, serde_json::Value);
struct PluginBinary(String, Vec<u8>, String);
pub fn plugins(config: crate::config::JscExperimental) -> impl Fold {
#[cfg(feature = "plugin")]
{
use anyhow::Context;
let mut plugin_binaries = vec![];
let mut plugin_errors = vec![];
if let Some(plugins) = &config.plugins {
for p in plugins {
let config_json = serde_json::to_string(&p.1)
.context("failed to serialize plugin config as json");
if config_json.is_err() {
plugin_errors.push(config_json.err().unwrap());
continue;
}
let path = swc_plugin_runner::resolve::resolve(&p.0);
if path.is_err() {
plugin_errors.push(path.err().unwrap());
continue;
}
let module_bytes = std::fs::read(&path.unwrap().as_ref());
if module_bytes.is_err() {
plugin_errors.push(
module_bytes
.context("Failed to read plugin binary")
.err()
.unwrap(),
);
continue;
}
plugin_binaries.push(PluginBinary(
p.0.clone(),
module_bytes.ok().unwrap(),
config_json.ok().unwrap(),
));
}
};
let cache_root =
swc_plugin_runner::resolve::resolve_plugin_cache_root(config.cache_root).ok();
RustPlugins {
plugins: plugin_binaries,
plugin_errors,
plugin_cache: cache_root,
}
}
#[cfg(not(feature = "plugin"))]
{
noop()
}
}
struct RustPlugins {
plugins: Vec<PluginBinary>,
plugin_errors: Vec<anyhow::Error>,
/// TODO: it is unclear how we'll support plugin itself in wasm target of
/// swc, as well as cache.
#[cfg(feature = "plugin")]
plugin_cache: Option<swc_plugin_runner::resolve::PluginCache>,
}
impl RustPlugins {
#[cfg(feature = "plugin")]
fn apply(&mut self, mut n: Program) -> Result<Program, anyhow::Error> {
for e in self.plugin_errors.drain(..) {
return Err(e);
}
for p in &self.plugins {
n = swc_plugin_runner::apply_js_plugin(&p.0, &p.1, &mut self.plugin_cache, &p.2, n)?;
}
Ok(n)
}
}
impl Fold for RustPlugins {
noop_fold_type!();
#[cfg(feature = "plugin")]
fn fold_module(&mut self, n: Module) -> Module {
self.apply(Program::Module(n))
.expect("failed to invoke plugin")
.expect_module()
}
#[cfg(feature = "plugin")]
fn fold_script(&mut self, n: Script) -> Script {
self.apply(Program::Script(n))
.expect("failed to invoke plugin")
.expect_script()
}
}