-
Notifications
You must be signed in to change notification settings - Fork 122
feat(transform): add element template transform #2517
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
Open
Yradex
wants to merge
3
commits into
lynx-family:main
Choose a base branch
from
Yradex:wt/element-template
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,3 @@ | ||
| --- | ||
|
|
||
| --- |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
29 changes: 29 additions & 0 deletions
29
packages/react/transform/crates/swc_plugin_element_template/Cargo.toml
This file contains hidden or 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,29 @@ | ||
| [package] | ||
| name = "swc_plugin_element_template" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| path = "lib.rs" | ||
|
|
||
| [features] | ||
| napi = ["dep:napi", "dep:napi-derive", "swc_plugins_shared/napi"] | ||
|
|
||
| [dependencies] | ||
| convert_case = { workspace = true } | ||
| hex = { workspace = true } | ||
| napi = { workspace = true, optional = true, features = ["serde-json"] } | ||
| napi-derive = { workspace = true, optional = true } | ||
| once_cell = { workspace = true } | ||
| regex = { workspace = true } | ||
| serde = { workspace = true, features = ["derive"] } | ||
| serde_json = { workspace = true, features = ["preserve_order"] } | ||
| sha-1 = { workspace = true } | ||
| swc_core = { workspace = true, features = ["base", "ecma_codegen", "ecma_parser", "ecma_transforms_typescript", "ecma_utils", "ecma_quote", "ecma_transforms_react", "ecma_transforms_optimization", "ecma_visit", "testing_transform"] } | ||
| swc_plugins_shared = { path = "../swc_plugins_shared" } | ||
|
|
||
| [lints.rust] | ||
| unexpected_cfgs = { level = "warn", check-cfg = ['cfg(swc_ast_unknown)'] } | ||
|
|
||
| [dev-dependencies] | ||
| insta = { version = "1.34", features = ["json"] } |
65 changes: 65 additions & 0 deletions
65
packages/react/transform/crates/swc_plugin_element_template/asset.rs
This file contains hidden or 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,65 @@ | ||
| use serde::Serialize; | ||
| use swc_core::common::comments::Comments; | ||
|
|
||
| use super::JSXTransformer; | ||
|
|
||
| #[derive(Serialize, Debug, Clone)] | ||
| pub struct ElementTemplateAsset { | ||
| // The compiled template is exported out-of-band from the JS module. The JS | ||
| // output keeps using a synthetic component tag so existing React/SWC passes can | ||
| // continue to own expression lowering and runtime value transport. | ||
| pub template_id: String, | ||
| pub compiled_template: serde_json::Value, | ||
| pub source_file: String, | ||
| } | ||
|
|
||
| const BUILTIN_RAW_TEXT_TEMPLATE_ID: &str = "__et_builtin_raw_text__"; | ||
| const BUILTIN_SOURCE_FILE: &str = "<builtin>"; | ||
|
|
||
| impl<C> JSXTransformer<C> | ||
| where | ||
| C: Comments + Clone, | ||
| { | ||
| fn builtin_raw_text_template_asset(&self) -> ElementTemplateAsset { | ||
| ElementTemplateAsset { | ||
| template_id: BUILTIN_RAW_TEXT_TEMPLATE_ID.to_string(), | ||
| compiled_template: serde_json::json!({ | ||
| "kind": "element", | ||
| "type": "raw-text", | ||
| "attributesArray": [ | ||
| { | ||
| "kind": "attribute", | ||
| "key": "text", | ||
| "binding": "slot", | ||
| "attrSlotIndex": 0, | ||
| } | ||
| ], | ||
| "children": [], | ||
| }), | ||
| source_file: BUILTIN_SOURCE_FILE.to_string(), | ||
| } | ||
| } | ||
|
|
||
| pub(super) fn ensure_builtin_element_templates(&self) { | ||
| let Some(element_templates) = &self.element_templates else { | ||
| return; | ||
| }; | ||
|
|
||
| let mut templates = element_templates.borrow_mut(); | ||
| if templates.is_empty() { | ||
| return; | ||
| } | ||
|
|
||
| // Raw text can also appear as dynamic element-slot content. Emitting the | ||
| // builtin template only when user ET templates exist keeps non-ET transforms | ||
| // free of template metadata while giving runtime a stable key for text slots. | ||
| if templates | ||
| .iter() | ||
| .any(|template| template.template_id == BUILTIN_RAW_TEXT_TEMPLATE_ID) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| templates.push(self.builtin_raw_text_template_asset()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.