|
| 1 | +#![cfg(feature = "plugin")] |
| 2 | + |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use anyhow::{Context, Result}; |
| 6 | +use common::{ |
| 7 | + comments::SingleThreadedComments, |
| 8 | + errors::Handler, |
| 9 | + plugin::{metadata::TransformPluginMetadataContext, serialized::PluginSerializedBytes}, |
| 10 | + Mark, SourceFile, GLOBALS, |
| 11 | +}; |
| 12 | +use par_iter::iter::{IntoParallelRefIterator, ParallelIterator}; |
| 13 | +use serde::Deserialize; |
| 14 | +use swc_config::IsModule; |
| 15 | +use swc_ecma_ast::EsVersion; |
| 16 | +use swc_ecma_parser::Syntax; |
| 17 | +use swc_ecma_transforms::resolver; |
| 18 | + |
| 19 | +use crate::{plugin::PluginConfig, Compiler}; |
| 20 | + |
| 21 | +impl Compiler { |
| 22 | + /// Run analysis using Wasm plugins. |
| 23 | + pub fn run_wasm_analysis( |
| 24 | + &self, |
| 25 | + fm: Arc<SourceFile>, |
| 26 | + handler: &Handler, |
| 27 | + opts: &WasmAnalysisOptions, |
| 28 | + comments: SingleThreadedComments, |
| 29 | + ) -> Result<String> { |
| 30 | + self.run(|| { |
| 31 | + GLOBALS.with(|globals| { |
| 32 | + let unresolved_mark = Mark::new(); |
| 33 | + let top_level_mark = Mark::new(); |
| 34 | + |
| 35 | + let mut program = self.parse_js( |
| 36 | + fm.clone(), |
| 37 | + handler, |
| 38 | + EsVersion::latest(), |
| 39 | + opts.parser, |
| 40 | + opts.module, |
| 41 | + Some(&comments), |
| 42 | + )?; |
| 43 | + |
| 44 | + program.mutate(resolver( |
| 45 | + unresolved_mark, |
| 46 | + top_level_mark, |
| 47 | + opts.parser.typescript(), |
| 48 | + )); |
| 49 | + |
| 50 | + let serialized = { |
| 51 | + let _span = tracing::span!(tracing::Level::INFO, "serialize_program").entered(); |
| 52 | + let program = |
| 53 | + swc_common::plugin::serialized::VersionedSerializable::new(program); |
| 54 | + PluginSerializedBytes::try_serialize(&program)? |
| 55 | + }; |
| 56 | + |
| 57 | + let transform_metadata_context = Arc::new(TransformPluginMetadataContext::new( |
| 58 | + Some(fm.name.to_string()), |
| 59 | + crate::config::default_env_name(), |
| 60 | + None, |
| 61 | + )); |
| 62 | + |
| 63 | + let result = opts |
| 64 | + .plugins |
| 65 | + .par_iter() |
| 66 | + .map(|p| { |
| 67 | + GLOBALS.set(globals, || { |
| 68 | + let plugin_module_bytes = crate::config::PLUGIN_MODULE_CACHE |
| 69 | + .inner |
| 70 | + .get() |
| 71 | + .unwrap() |
| 72 | + .lock() |
| 73 | + .get(&p.0) |
| 74 | + .expect("plugin module should be loaded"); |
| 75 | + |
| 76 | + let plugin_name = plugin_module_bytes.get_module_name().to_string(); |
| 77 | + let runtime = swc_plugin_runner::wasix_runtime::build_wasi_runtime( |
| 78 | + crate::config::PLUGIN_MODULE_CACHE |
| 79 | + .inner |
| 80 | + .get() |
| 81 | + .unwrap() |
| 82 | + .lock() |
| 83 | + .get_fs_cache_root() |
| 84 | + .map(std::path::PathBuf::from), |
| 85 | + ); |
| 86 | + let mut transform_plugin_executor = |
| 87 | + swc_plugin_runner::create_plugin_transform_executor( |
| 88 | + &self.cm, |
| 89 | + &unresolved_mark, |
| 90 | + &transform_metadata_context, |
| 91 | + plugin_module_bytes, |
| 92 | + Some(p.1.clone()), |
| 93 | + runtime, |
| 94 | + ); |
| 95 | + |
| 96 | + let span = tracing::span!( |
| 97 | + tracing::Level::INFO, |
| 98 | + "execute_plugin_runner", |
| 99 | + plugin_module = p.0.as_str() |
| 100 | + ) |
| 101 | + .entered(); |
| 102 | + |
| 103 | + let (result, output) = swc_transform_common::output::capture(|| { |
| 104 | + transform_plugin_executor |
| 105 | + .transform(&serialized, Some(true)) |
| 106 | + .with_context(|| { |
| 107 | + format!( |
| 108 | + "failed to invoke `{}` as js analysis plugin at {}", |
| 109 | + &p.0, plugin_name |
| 110 | + ) |
| 111 | + }) |
| 112 | + }); |
| 113 | + result?; |
| 114 | + drop(span); |
| 115 | + |
| 116 | + Ok(output) |
| 117 | + }) |
| 118 | + }) |
| 119 | + .collect::<Result<Vec<_>>>()?; |
| 120 | + |
| 121 | + serde_json::to_string(&result) |
| 122 | + .map_err(|e| anyhow::anyhow!("Failed to serialize output: {e}")) |
| 123 | + }) |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#[derive(Debug, Deserialize)] |
| 129 | +pub struct WasmAnalysisOptions { |
| 130 | + #[serde(default)] |
| 131 | + pub parser: Syntax, |
| 132 | + #[serde(default)] |
| 133 | + pub module: IsModule, |
| 134 | + |
| 135 | + pub plugins: Vec<PluginConfig>, |
| 136 | +} |
0 commit comments