feat(plugin): Pass unresolved_mark to plugins (#5212)

This commit is contained in:
OJ Kwon 2022-07-14 18:11:17 -07:00 committed by GitHub
parent 1639b04b7c
commit 92c0153cb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1101 additions and 8 deletions

View File

@ -542,6 +542,7 @@ impl Options {
source_map,
experimental,
plugin_context,
unresolved_mark,
)
};
@ -565,7 +566,14 @@ impl Options {
swc_plugin_runner::cache::init_plugin_module_cache_once();
let comments = comments.cloned();
let source_map = cm.clone();
crate::plugin::plugins(None, comments, source_map, experimental, plugin_context)
crate::plugin::plugins(
None,
comments,
source_map,
experimental,
plugin_context,
unresolved_mark,
)
};
#[cfg(not(feature = "plugin"))]

View File

@ -48,6 +48,7 @@ pub fn plugins(
source_map: std::sync::Arc<swc_common::SourceMap>,
config: crate::config::JscExperimental,
plugin_context: PluginContext,
unresolved_mark: swc_common::Mark,
) -> impl Fold {
{
RustPlugins {
@ -56,6 +57,7 @@ pub fn plugins(
source_map,
plugins: config.plugins,
plugin_context,
unresolved_mark,
}
}
}
@ -71,6 +73,7 @@ struct RustPlugins {
plugins: Option<Vec<PluginConfig>>,
source_map: std::sync::Arc<swc_common::SourceMap>,
plugin_context: PluginContext,
unresolved_mark: swc_common::Mark,
}
impl RustPlugins {
@ -179,6 +182,7 @@ impl RustPlugins {
&serialized_program,
&serialized_config_json,
&serialized_context_json,
self.unresolved_mark,
should_enable_comments_proxy,
)
.with_context(|| {
@ -250,6 +254,7 @@ impl RustPlugins {
&serialized_program,
&serialized_config_json,
&serialized_context_json,
self.unresolved_mark,
should_enable_comments_proxy,
)
.with_context(|| {

View File

@ -81,6 +81,7 @@ pub struct TransformPluginProgramMetadata {
/// This is readonly. Changing value in plugin doesn't affect host's
/// behavior.
pub plugin_config: String,
pub unresolved_mark: crate::syntax_pos::Mark,
/// Stringified JSON value for relative context while running transform,
/// like filenames.
/// /// This is readonly. Changing value in plugin doesn't affect host's

View File

@ -67,7 +67,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
// There are some cases error won't be wrapped up however - for example, we expect
// serialization of PluginError itself should succeed.
#[no_mangle]
pub fn #transform_process_impl_ident(ast_ptr: *const u8, ast_ptr_len: i32, config_str_ptr: *const u8, config_str_ptr_len: i32, context_str_ptr: *const u8, context_str_ptr_len: i32, should_enable_comments_proxy: i32) -> i32 {
pub fn #transform_process_impl_ident(ast_ptr: *const u8, ast_ptr_len: i32, config_str_ptr: *const u8, config_str_ptr_len: i32, context_str_ptr: *const u8, context_str_ptr_len: i32, unresolved_mark: u32, should_enable_comments_proxy: i32) -> i32 {
// Reconstruct `Program` & config string from serialized program
// Host (SWC) should allocate memory, copy bytes and pass ptr to plugin.
let program = unsafe { swc_plugin::deserialize_from_ptr(ast_ptr, ast_ptr_len).map(|v| v.into_inner()) };
@ -114,6 +114,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
comments: plugin_comments_proxy,
source_map: swc_plugin::source_map::PluginSourceMapProxy,
plugin_config: config,
unresolved_mark: swc_plugin::syntax_pos::Mark::from_u32(unresolved_mark),
transform_context: context
};

View File

@ -11,7 +11,7 @@ use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use once_cell::sync::Lazy;
use swc_common::{
plugin::{PluginSerializedBytes, VersionedSerializable},
FileName, FilePathMapping, SourceMap,
FileName, FilePathMapping, Mark, SourceMap,
};
use swc_ecma_ast::EsVersion;
use swc_ecma_parser::parse_file_as_program;
@ -84,6 +84,7 @@ fn bench_transform(b: &mut Bencher, plugin_dir: &Path) {
"{}",
)))
.unwrap(),
Mark::new(),
true,
)
.unwrap();

View File

@ -13,7 +13,7 @@ use crate::memory_interop::write_into_memory_view;
/// A struct encapsule executing a plugin's transform interop to its teardown
pub struct TransformExecutor {
// Main transform interface plugin exports
exported_plugin_transform: wasmer::NativeFunc<(i32, i32, i32, i32, i32, i32, i32), i32>,
exported_plugin_transform: wasmer::NativeFunc<(i32, i32, i32, i32, i32, i32, u32, i32), i32>,
// Schema version interface exports
exported_plugin_transform_schema_version: wasmer::NativeFunc<(), u32>,
// `__free` function automatically exported via swc_plugin sdk to allow deallocation in guest
@ -41,7 +41,7 @@ impl TransformExecutor {
let tracker = TransformExecutor {
exported_plugin_transform: instance
.exports
.get_native_function::<(i32, i32, i32, i32, i32, i32, i32), i32>(
.get_native_function::<(i32, i32, i32, i32, i32, i32, u32, i32), i32>(
"__transform_plugin_process_impl",
)?,
exported_plugin_transform_schema_version: instance
@ -138,6 +138,7 @@ impl TransformExecutor {
program: &PluginSerializedBytes,
config: &PluginSerializedBytes,
context: &PluginSerializedBytes,
unresolved_mark: swc_common::Mark,
should_enable_comments_proxy: bool,
) -> Result<PluginSerializedBytes, Error> {
let should_enable_comments_proxy = if should_enable_comments_proxy { 1 } else { 0 };
@ -152,6 +153,7 @@ impl TransformExecutor {
config_str_ptr.1,
context_str_ptr.0,
context_str_ptr.1,
unresolved_mark.as_u32(),
should_enable_comments_proxy,
)?;

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@ use swc_common::{
errors::HANDLER,
plugin::{PluginSerializedBytes, VersionedSerializable},
sync::Lazy,
FileName,
FileName, Mark,
};
use swc_ecma_ast::{CallExpr, Callee, EsVersion, Expr, Lit, MemberExpr, Program, Str};
use swc_ecma_parser::{parse_file_as_program, EsConfig, Syntax};
@ -107,7 +107,7 @@ fn internal() -> Result<(), Error> {
.expect("Should load plugin");
let program_bytes = plugin_transform_executor
.transform(&program, &config, &context, false)
.transform(&program, &config, &context, Mark::new(), false)
.expect("Plugin should apply transform");
let program: Program = program_bytes
@ -159,7 +159,7 @@ fn internal() -> Result<(), Error> {
.expect("Should load plugin");
plugin_transform_executor
.transform(&program, &config, &context, false)
.transform(&program, &config, &context, Mark::new(), false)
.expect("Plugin should apply transform")
});
@ -202,6 +202,7 @@ fn internal() -> Result<(), Error> {
"{sourceFileName: 'multiple_plugin_test'}".to_string(),
))
.expect("Should serializable"),
Mark::new(),
false,
)
.expect("Plugin should apply transform");
@ -222,6 +223,7 @@ fn internal() -> Result<(), Error> {
"{sourceFileName: 'multiple_plugin_test2'}".to_string(),
))
.expect("Should serializable"),
Mark::new(),
false,
)
.expect("Plugin should apply transform");