From 50b5c91b35614ec2c91251f09c8513e600d3be17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Fri, 22 Sep 2023 11:46:21 +0900 Subject: [PATCH] Add a package --- Cargo.lock | 32 +++++++++ Cargo.toml | 1 + packages/react-extra-srcmap/.npmignore | 2 + packages/react-extra-srcmap/Cargo.toml | 23 ++++++ packages/react-extra-srcmap/README.md | 22 ++++++ packages/react-extra-srcmap/package.json | 21 ++++++ packages/react-extra-srcmap/src/lib.rs | 18 +++++ .../react-extra-srcmap/transform/Cargo.toml | 24 +++++++ .../react-extra-srcmap/transform/src/lib.rs | 70 +++++++++++++++++++ .../transform/tests/fixture.rs | 44 ++++++++++++ .../tests/fixture/custom/simple/input.js | 9 +++ .../tests/fixture/custom/simple/output.js | 11 +++ .../tests/fixture/default/simple/input.js | 11 +++ .../tests/fixture/default/simple/output.js | 15 ++++ 14 files changed, 303 insertions(+) create mode 100644 packages/react-extra-srcmap/.npmignore create mode 100644 packages/react-extra-srcmap/Cargo.toml create mode 100644 packages/react-extra-srcmap/README.md create mode 100644 packages/react-extra-srcmap/package.json create mode 100644 packages/react-extra-srcmap/src/lib.rs create mode 100644 packages/react-extra-srcmap/transform/Cargo.toml create mode 100644 packages/react-extra-srcmap/transform/src/lib.rs create mode 100644 packages/react-extra-srcmap/transform/tests/fixture.rs create mode 100644 packages/react-extra-srcmap/transform/tests/fixture/custom/simple/input.js create mode 100644 packages/react-extra-srcmap/transform/tests/fixture/custom/simple/output.js create mode 100644 packages/react-extra-srcmap/transform/tests/fixture/default/simple/input.js create mode 100644 packages/react-extra-srcmap/transform/tests/fixture/default/simple/output.js diff --git a/Cargo.lock b/Cargo.lock index 99ca51f5a..7983a7317 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1535,6 +1535,22 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "react_extra_srcmap" +version = "0.2.0" +dependencies = [ + "serde", + "swc_atoms", + "swc_cached", + "swc_common", + "swc_ecma_ast", + "swc_ecma_parser", + "swc_ecma_transforms_base", + "swc_ecma_transforms_testing", + "swc_ecma_visit", + "testing", +] + [[package]] name = "react_remove_properties" version = "0.2.0" @@ -2787,6 +2803,22 @@ dependencies = [ "tracing", ] +[[package]] +name = "swc_plugin_react_extra_srcmap" +version = "0.13.3" +dependencies = [ + "react_extra_srcmap", + "serde_json", + "swc_cached", + "swc_common", + "swc_core", + "swc_ecma_ast", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_plugin_macro", + "tracing", +] + [[package]] name = "swc_plugin_react_remove_properties" version = "0.13.3" diff --git a/Cargo.toml b/Cargo.toml index a8f621786..ac41fe467 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "packages/jest", "packages/loadable-components", "packages/noop", + "packages/react-extra-srcmap", "packages/react-remove-properties", "packages/relay", "packages/remove-console", diff --git a/packages/react-extra-srcmap/.npmignore b/packages/react-extra-srcmap/.npmignore new file mode 100644 index 000000000..1ed674f50 --- /dev/null +++ b/packages/react-extra-srcmap/.npmignore @@ -0,0 +1,2 @@ +transform/ +tests/ \ No newline at end of file diff --git a/packages/react-extra-srcmap/Cargo.toml b/packages/react-extra-srcmap/Cargo.toml new file mode 100644 index 000000000..22f660b55 --- /dev/null +++ b/packages/react-extra-srcmap/Cargo.toml @@ -0,0 +1,23 @@ +[package] +authors = ["강동윤 "] +description = "SWC plugin for https://www.npmjs.com/package/babel-plugin-react-extra-srcmap" +edition = "2021" +license = "Apache-2.0" +name = "swc_plugin_react_extra_srcmap" +publish = false +version = "0.13.3" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +react_extra_srcmap = { path = "./transform" } +serde_json = "1.0.79" +swc_cached = "0.3.17" +swc_common = { version = "0.32.1", features = ["concurrent"] } +swc_core = { version = "0.83.5", features = ["ecma_plugin_transform"] } +swc_ecma_ast = "0.109.1" +swc_ecma_utils = "0.123.0" +swc_ecma_visit = "0.95.1" +swc_plugin_macro = "0.9.15" +tracing = { version = "0.1.37", features = ["release_max_level_off"] } diff --git a/packages/react-extra-srcmap/README.md b/packages/react-extra-srcmap/README.md new file mode 100644 index 000000000..101663499 --- /dev/null +++ b/packages/react-extra-srcmap/README.md @@ -0,0 +1,22 @@ +# react-extra-srcmap + +See https://nextjs.org/docs/architecture/nextjs-compiler#remove-react-properties for more information. + +## Config + +```json +["react-extra-srcmap"] +``` + +or + +```json +[ + "react-extra-srcmap", + { + // The regexes defined here are processed in Rust so the syntax is different from + // JavaScript `RegExp`s. See https://docs.rs/regex. + "properties": ["^data-custom$"] + } +] +``` diff --git a/packages/react-extra-srcmap/package.json b/packages/react-extra-srcmap/package.json new file mode 100644 index 000000000..dc654cfc9 --- /dev/null +++ b/packages/react-extra-srcmap/package.json @@ -0,0 +1,21 @@ +{ + "name": "@swc/plugin-react-extra-srcmap", + "version": "1.5.86", + "description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-react-extra-srcmap", + "main": "swc_plugin_react_extra_srcmap.wasm", + "scripts": { + "prepack": "cp ../../target/wasm32-wasi/release/swc_plugin_react_extra_srcmap.wasm ." + }, + "homepage": "https://swc.rs", + "repository": { + "type": "git", + "url": "+https://github.com/swc-project/plugins.git" + }, + "bugs": { + "url": "https://github.com/swc-project/plugins/issues" + }, + "author": "강동윤 ", + "keywords": [], + "license": "Apache-2.0", + "preferUnplugged": true +} diff --git a/packages/react-extra-srcmap/src/lib.rs b/packages/react-extra-srcmap/src/lib.rs new file mode 100644 index 000000000..164c566c1 --- /dev/null +++ b/packages/react-extra-srcmap/src/lib.rs @@ -0,0 +1,18 @@ +#![allow(clippy::not_unsafe_ptr_arg_deref)] +use swc_core::{ + ecma::{ast::Program, visit::FoldWith}, + plugin::{plugin_transform, proxies::TransformPluginProgramMetadata}, +}; + +#[plugin_transform] +fn swc_plugin(program: Program, data: TransformPluginProgramMetadata) -> Program { + let config = serde_json::from_str::>( + &data + .get_transform_plugin_config() + .expect("failed to get plugin config for react-extra-srcmap"), + ) + .expect("invalid packages") + .unwrap_or_else(|| react_extra_srcmap::Config::All(true)); + + program.fold_with(&mut react_extra_srcmap::react_extra_srcmap(config)) +} diff --git a/packages/react-extra-srcmap/transform/Cargo.toml b/packages/react-extra-srcmap/transform/Cargo.toml new file mode 100644 index 000000000..8ae086769 --- /dev/null +++ b/packages/react-extra-srcmap/transform/Cargo.toml @@ -0,0 +1,24 @@ +[package] +authors = ["강동윤 "] +description = "AST Transforms for import modularizer" +edition = "2021" +license = "Apache-2.0" +name = "react_extra_srcmap" +repository = "https://github.com/swc-project/plugins.git" +version = "0.2.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1", features = ["derive"] } +swc_atoms = "0.5.9" +swc_cached = "0.3.17" +swc_common = "0.32.1" +swc_ecma_ast = "0.109.1" +swc_ecma_visit = "0.95.1" + +[dev-dependencies] +swc_ecma_parser = "0.140.0" +swc_ecma_transforms_base = "0.133.2" +swc_ecma_transforms_testing = "0.136.1" +testing = "0.34.1" diff --git a/packages/react-extra-srcmap/transform/src/lib.rs b/packages/react-extra-srcmap/transform/src/lib.rs new file mode 100644 index 000000000..7e73a39e8 --- /dev/null +++ b/packages/react-extra-srcmap/transform/src/lib.rs @@ -0,0 +1,70 @@ +use serde::Deserialize; +use swc_cached::regex::CachedRegex; +use swc_ecma_ast::*; +use swc_ecma_visit::{noop_fold_type, Fold, FoldWith}; + +#[derive(Clone, Debug, Deserialize)] +#[serde(untagged)] +pub enum Config { + All(bool), + WithOptions(Options), +} + +impl Config { + pub fn truthy(&self) -> bool { + match self { + Config::All(b) => *b, + Config::WithOptions(_) => true, + } + } +} + +#[derive(Clone, Debug, Deserialize)] +pub struct Options { + #[serde(default)] + pub properties: Vec, +} + +pub fn react_extra_srcmap(config: Config) -> impl Fold { + let mut properties: Vec = match config { + Config::WithOptions(x) => x + .properties + .iter() + .map(|pattern| { + CachedRegex::new(pattern).unwrap_or_else(|e| { + panic!("error compiling property regex `{}`: {}", pattern, e); + }) + }) + .collect(), + _ => vec![], + }; + if properties.is_empty() { + // Keep the default regex identical to `babel-plugin-react-extra-srcmap`. + properties.push(CachedRegex::new(r"^data-test").unwrap()); + } + RemoveProperties { properties } +} + +struct RemoveProperties { + properties: Vec, +} + +impl RemoveProperties { + fn should_remove_property(&self, name: &str) -> bool { + self.properties.iter().any(|p| p.is_match(name)) + } +} + +impl Fold for RemoveProperties { + noop_fold_type!(); + + fn fold_jsx_opening_element(&mut self, mut el: JSXOpeningElement) -> JSXOpeningElement { + el.attrs.retain(|attr| { + !matches!(attr, JSXAttrOrSpread::JSXAttr(JSXAttr { + name: JSXAttrName::Ident(ident), + .. + }) if self.should_remove_property(ident.sym.as_ref())) + }); + el.fold_children_with(self) + } +} diff --git a/packages/react-extra-srcmap/transform/tests/fixture.rs b/packages/react-extra-srcmap/transform/tests/fixture.rs new file mode 100644 index 000000000..a020d4a16 --- /dev/null +++ b/packages/react-extra-srcmap/transform/tests/fixture.rs @@ -0,0 +1,44 @@ +use std::path::PathBuf; + +use react_extra_srcmap::Options; +use swc_common::{chain, Mark}; +use swc_ecma_parser::{EsConfig, Syntax}; +use swc_ecma_transforms_base::resolver; +use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig}; + +fn syntax() -> Syntax { + Syntax::Es(EsConfig { + jsx: true, + ..Default::default() + }) +} + +#[testing::fixture("tests/fixture/**/input.js")] +fn fixture(input: PathBuf) { + let output = input.parent().unwrap().join("output.js"); + test_fixture( + syntax(), + &|_tr| { + let unresolved_mark = Mark::new(); + let top_level_mark = Mark::new(); + + chain!( + resolver(unresolved_mark, top_level_mark, false), + react_extra_srcmap::react_extra_srcmap( + if input.to_string_lossy().contains("custom") { + react_extra_srcmap::Config::WithOptions(Options { + properties: vec!["^data-custom$".into()], + }) + } else { + react_extra_srcmap::Config::All(true) + } + ) + ) + }, + &input, + &output, + FixtureTestConfig { + ..Default::default() + }, + ); +} diff --git a/packages/react-extra-srcmap/transform/tests/fixture/custom/simple/input.js b/packages/react-extra-srcmap/transform/tests/fixture/custom/simple/input.js new file mode 100644 index 000000000..aea9309cd --- /dev/null +++ b/packages/react-extra-srcmap/transform/tests/fixture/custom/simple/input.js @@ -0,0 +1,9 @@ +export default function Home() { + return ( +
+
+

Hello World!

+
+
+ ) +} diff --git a/packages/react-extra-srcmap/transform/tests/fixture/custom/simple/output.js b/packages/react-extra-srcmap/transform/tests/fixture/custom/simple/output.js new file mode 100644 index 000000000..d2224111f --- /dev/null +++ b/packages/react-extra-srcmap/transform/tests/fixture/custom/simple/output.js @@ -0,0 +1,11 @@ +export default function Home() { + return
+ +
+ +

Hello World!

+ +
+ +
; +} diff --git a/packages/react-extra-srcmap/transform/tests/fixture/default/simple/input.js b/packages/react-extra-srcmap/transform/tests/fixture/default/simple/input.js new file mode 100644 index 000000000..ee9956974 --- /dev/null +++ b/packages/react-extra-srcmap/transform/tests/fixture/default/simple/input.js @@ -0,0 +1,11 @@ +export default function Home() { + return ( +
+
+

nested
}> + Hello World! +

+
+
+ ) +} diff --git a/packages/react-extra-srcmap/transform/tests/fixture/default/simple/output.js b/packages/react-extra-srcmap/transform/tests/fixture/default/simple/output.js new file mode 100644 index 000000000..03621e62b --- /dev/null +++ b/packages/react-extra-srcmap/transform/tests/fixture/default/simple/output.js @@ -0,0 +1,15 @@ +export default function Home() { + return
+ +
+ +

nested
}> + + Hello World! + +

+ +
+ +
; +}