-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(turbopack): support swc transform plugins #50401
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/next-swc/crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use anyhow::Result; | ||
use turbo_tasks_fs::FileSystemPathVc; | ||
use turbopack_binding::turbopack::ecmascript::OptionTransformPluginVc; | ||
|
||
use crate::next_config::NextConfigVc; | ||
|
||
#[turbo_tasks::function] | ||
pub async fn get_swc_ecma_transform_plugin( | ||
#[cfg_attr(not(feature = "plugin"), allow(unused))] project_path: FileSystemPathVc, | ||
next_config: NextConfigVc, | ||
) -> Result<OptionTransformPluginVc> { | ||
let config = next_config.await?; | ||
match config.experimental.swc_plugins.as_ref() { | ||
Some(plugin_configs) if !plugin_configs.is_empty() => { | ||
#[cfg(feature = "plugin")] | ||
{ | ||
use anyhow::{bail, Context}; | ||
use turbo_tasks::Value; | ||
use turbo_tasks_fs::FileContent; | ||
use turbopack_binding::turbopack::{ | ||
core::{ | ||
asset::Asset, | ||
issue::{IssueSeverity, OptionIssueSourceVc}, | ||
reference_type::ReferenceType, | ||
resolve::{ | ||
handle_resolve_error, parse::RequestVc, pattern::Pattern, resolve, | ||
PrimaryResolveResult, | ||
}, | ||
}, | ||
ecmascript::{OptionTransformPluginVc, TransformPluginVc}, | ||
ecmascript_plugin::transform::swc_ecma_transform_plugins::{ | ||
SwcEcmaTransformPluginsTransformer, SwcPluginModule, SwcPluginModuleVc, | ||
}, | ||
turbopack::{resolve_options, resolve_options_context::ResolveOptionsContext}, | ||
}; | ||
|
||
let mut plugins = vec![]; | ||
for (name, config) in plugin_configs.iter() { | ||
// [TODO]: SWC's current experimental config supports | ||
// two forms of plugin path, | ||
// one for implicit package name resolves to node_modules, | ||
// and one for explicit path to a .wasm binary. | ||
// Current resolve will fail with latter. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does the latter fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turbopack does not resolves to an absolute path not related to project root. |
||
let request = RequestVc::parse(Value::new(Pattern::Constant(name.to_string()))); | ||
let resolve_options = resolve_options( | ||
project_path, | ||
ResolveOptionsContext { | ||
enable_node_modules: Some(project_path.root().resolve().await?), | ||
enable_node_native_modules: true, | ||
..Default::default() | ||
} | ||
.cell(), | ||
); | ||
|
||
let plugin_wasm_module_resolve_result = handle_resolve_error( | ||
resolve(project_path, request, resolve_options), | ||
Value::new(ReferenceType::Undefined), | ||
project_path, | ||
request, | ||
resolve_options, | ||
OptionIssueSourceVc::none(), | ||
IssueSeverity::Error.cell(), | ||
) | ||
.await?; | ||
let plugin_wasm_module_resolve_result = | ||
&*plugin_wasm_module_resolve_result.await?; | ||
|
||
let primary = plugin_wasm_module_resolve_result | ||
.primary | ||
.first() | ||
.context("Unable to resolve primary context")?; | ||
|
||
let PrimaryResolveResult::Asset(plugin_module_asset) = primary else { | ||
bail!("Expected to find asset"); | ||
}; | ||
|
||
let content = &*plugin_module_asset.content().file_content().await?; | ||
|
||
let FileContent::Content(file) = content else { | ||
bail!("Expected file content for plugin module"); | ||
}; | ||
|
||
plugins.push(( | ||
SwcPluginModuleVc::cell(SwcPluginModule::new( | ||
name, | ||
file.content().to_bytes()?.to_vec(), | ||
)), | ||
config.clone(), | ||
)); | ||
} | ||
|
||
return Ok(OptionTransformPluginVc::cell(Some( | ||
TransformPluginVc::cell(Box::new(SwcEcmaTransformPluginsTransformer::new( | ||
plugins, | ||
))), | ||
))); | ||
} | ||
|
||
#[cfg(not(feature = "plugin"))] | ||
Ok(OptionTransformPluginVc::cell(None)) | ||
} | ||
_ => Ok(OptionTransformPluginVc::cell(None)), | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Can this be hoisted higher, so that we don't need an extra branch path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably yes, have to double check.