diff --git a/Cargo.lock b/Cargo.lock index 2e6f00e38..ba5d1c620 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -604,9 +604,12 @@ dependencies = [ [[package]] name = "deranged" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2696e8a945f658fd14dc3b87242e6b80cd0f36ff04ea560fa39082368847946" +checksum = "0f32d04922c60427da6f9fef14d042d9edddef64cb9d4ce0d64d0685fbeb1fd3" +dependencies = [ + "powerfmt", +] [[package]] name = "diff" @@ -1163,7 +1166,7 @@ dependencies = [ [[package]] name = "modularize_imports" -version = "0.50.0" +version = "0.51.0" dependencies = [ "convert_case", "handlebars", @@ -1476,6 +1479,12 @@ dependencies = [ "syn 2.0.32", ] +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1659,7 +1668,7 @@ dependencies = [ [[package]] name = "react_remove_properties" -version = "0.5.0" +version = "0.6.0" dependencies = [ "serde", "swc_atoms", @@ -1734,7 +1743,7 @@ checksum = "c707298afce11da2efef2f600116fa93ffa7a032b5d7b628aa17711ec81383ca" [[package]] name = "remove_console" -version = "0.6.0" +version = "0.7.0" dependencies = [ "serde", "swc_atoms", @@ -1881,9 +1890,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.171" +version = "1.0.189" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30e27d1e4fd7659406c492fd6cfaf2066ba8773de45ca75e855590f856dc34a9" +checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537" dependencies = [ "serde_derive", ] @@ -1901,9 +1910,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.171" +version = "1.0.189" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "389894603bd18c46fa56231694f8d827779c0951a667087194cf9de94ed24682" +checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" dependencies = [ "proc-macro2", "quote", @@ -1912,9 +1921,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.106" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc66a619ed80bf7a0f6b17dd063a84b88f6dea1813737cf469aef1d081142c2" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" dependencies = [ "itoa", "ryu", @@ -2127,7 +2136,7 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "styled_components" -version = "0.77.0" +version = "0.78.0" dependencies = [ "Inflector", "once_cell", @@ -2148,7 +2157,7 @@ dependencies = [ [[package]] name = "styled_jsx" -version = "0.54.0" +version = "0.55.0" dependencies = [ "easy-error", "lightningcss", @@ -2233,9 +2242,9 @@ dependencies = [ [[package]] name = "swc_common" -version = "0.33.0" +version = "0.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "490e199e25d2aa3fbef675524fa81408651f4e7178b51110470ddd1b3e3bbe75" +checksum = "a7c5aff4c50b1cb313487bb3dff99082779e1509a9d7e3b993790cb9ff72d14b" dependencies = [ "anyhow", "ast_node", @@ -2291,7 +2300,7 @@ dependencies = [ [[package]] name = "swc_constify" -version = "0.17.0" +version = "0.18.0" dependencies = [ "once_cell", "rustc-hash", @@ -2310,9 +2319,9 @@ dependencies = [ [[package]] name = "swc_core" -version = "0.86.1" +version = "0.86.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d40c049be93138bf3f521dd42500301762fdd5f5d326e11de012588dcd8135ca" +checksum = "7fc150980a536967131caf89ccedfbf425ba83fc08be3c8809dd310c73712853" dependencies = [ "once_cell", "swc_atoms", @@ -2444,9 +2453,9 @@ dependencies = [ [[package]] name = "swc_ecma_ast" -version = "0.110.0" +version = "0.110.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cbbf9918976a7e7fbdb4f76fe659d08e291a8b56b524b424183fc67d1189679" +checksum = "ad53346adc2eb4f2accc5ff612ef6c5e0e4a687ce45c354816e628e5f7c8d876" dependencies = [ "bitflags 2.4.0", "bytecheck", @@ -2463,9 +2472,9 @@ dependencies = [ [[package]] name = "swc_ecma_codegen" -version = "0.146.1" +version = "0.146.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fba119c76654599b71099a0150094f5790f00db63aab6cda1790e731f42c98f" +checksum = "25072d4697d22370f48f9807a57cf81cabb257793834c839b229242d720191fa" dependencies = [ "memchr", "num-bigint", @@ -2529,9 +2538,9 @@ dependencies = [ [[package]] name = "swc_ecma_parser" -version = "0.141.1" +version = "0.141.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a26e535c623db7beb04ba8ebfa821c287b72a23f9fb523990b54db6c1355c990" +checksum = "cfdb522b9e8e2bdea5d4f0a7bf311acf38ba44d48e52b4cb9ab8dcb8814cb7ed" dependencies = [ "either", "num-bigint", @@ -2549,9 +2558,9 @@ dependencies = [ [[package]] name = "swc_ecma_quote_macros" -version = "0.52.1" +version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "687e5944bd1ccf5729104c2ed11c182b1f679d18d620980da442bfe326391ddf" +checksum = "f7a3fa8831a60e0791c0c858273907c822c99ade76987fb276d6883adf27c557" dependencies = [ "anyhow", "pmutil", @@ -2567,9 +2576,9 @@ dependencies = [ [[package]] name = "swc_ecma_testing" -version = "0.22.0" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57c456e2c330c0049f77780cd412e61d9c9d0ae7ff9b6c0e4f8262270b7c718e" +checksum = "cc49bcb4c6db93972e12f965892dcfd9e1f06c3fc3a1d195972e4c9ade9e0b12" dependencies = [ "anyhow", "hex", @@ -2594,9 +2603,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_base" -version = "0.134.4" +version = "0.134.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d30c5aa540b6516875d507d3dc2ca7edf77a30fe9e070868ccd2d90b85a3a3" +checksum = "5c7692452ac60c89b570c1592e4a0723a201d8e3933c3838f44846f9d7aaa2b5" dependencies = [ "better_scoped_tls", "bitflags 2.4.0", @@ -2678,9 +2687,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_testing" -version = "0.137.4" +version = "0.137.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06b02eb5f5972cc9000aedf436aefdd1723d6f7235fc829eb1a2f29fcff38f86" +checksum = "30b410ed8b663592843306d9b7c92492aa3a77446b7fb54431a5b3d5b4f1176f" dependencies = [ "ansi_term", "anyhow", @@ -2721,9 +2730,9 @@ dependencies = [ [[package]] name = "swc_ecma_utils" -version = "0.124.4" +version = "0.124.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d5dd053e9a21c433504664d7083869c9d02394eb5141b101c81067067536471" +checksum = "b85d6043cc8f771d2b8dae78062931ce65ad4552198c6a4fdcc5c4888facf01a" dependencies = [ "indexmap", "num_cpus", @@ -2739,9 +2748,9 @@ dependencies = [ [[package]] name = "swc_ecma_visit" -version = "0.96.0" +version = "0.96.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47081acd84cdb2d49d6340ed3204e17738b444da10a3e1dd1eb3d7c8e4d47091" +checksum = "582559a61496786b159640b50e99320ca5cd7f3f4ea09afd5f48c02253ba579b" dependencies = [ "num-bigint", "swc_atoms", @@ -2753,7 +2762,7 @@ dependencies = [ [[package]] name = "swc_emotion" -version = "0.53.0" +version = "0.54.0" dependencies = [ "base64", "byteorder", @@ -2792,9 +2801,9 @@ dependencies = [ [[package]] name = "swc_error_reporters" -version = "0.17.0" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "015cbdf2b13ccc76eb12d1702a90fb9aae7b3cddacaf2c56a1b1a4a02f9fcd81" +checksum = "c1318961e85f58032030e137e5e42b4a90b3a166e05acf340d0195ad5652b764" dependencies = [ "anyhow", "miette", @@ -2827,6 +2836,22 @@ dependencies = [ "syn 2.0.32", ] +[[package]] +name = "swc_magic" +version = "0.1.0" +dependencies = [ + "serde", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_parser", + "swc_ecma_transforms_base", + "swc_ecma_transforms_testing", + "swc_ecma_utils", + "swc_ecma_visit", + "testing", +] + [[package]] name = "swc_plugin" version = "0.90.0" @@ -2925,9 +2950,9 @@ dependencies = [ [[package]] name = "swc_plugin_proxy" -version = "0.39.0" +version = "0.39.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55e32ea3aeb4930d0b6fcf1f052d33a4bb115a9d58e32bf3baade3316ef6bdd3" +checksum = "f6934fd5732f841f6aef392e492ee6936c8797cdfbc4baafe57b1c9ef1c6c944" dependencies = [ "better_scoped_tls", "rkyv", @@ -3024,6 +3049,15 @@ dependencies = [ "tracing", ] +[[package]] +name = "swc_plugin_swc_magic" +version = "0.1.0" +dependencies = [ + "serde_json", + "swc_core", + "swc_magic", +] + [[package]] name = "swc_plugin_transform_imports" version = "0.13.3" @@ -3041,7 +3075,7 @@ dependencies = [ [[package]] name = "swc_relay" -version = "0.25.0" +version = "0.26.0" dependencies = [ "once_cell", "regex", @@ -3163,9 +3197,9 @@ dependencies = [ [[package]] name = "testing" -version = "0.35.0" +version = "0.35.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e149694212e0152671c616dc21e4029a0f39710085f32705b2fb07fda89e437f" +checksum = "76f3d926524064129f7725899543b1724519dfc753df27236b30c21669e4b2a8" dependencies = [ "ansi_term", "cargo_metadata 0.15.4", @@ -3241,12 +3275,13 @@ dependencies = [ [[package]] name = "time" -version = "0.3.26" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a79d09ac6b08c1ab3906a2f7cc2e81a0e27c7ae89c63812df75e52bef0751e07" +checksum = "c4a34ab300f2dee6e562c10a046fc05e358b29f9bf92277f30c3c8d82275f6f5" dependencies = [ "deranged", "itoa", + "powerfmt", "serde", "time-core", "time-macros", @@ -3254,15 +3289,15 @@ dependencies = [ [[package]] name = "time-core" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.12" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75c65469ed6b3a4809d987a41eb1dc918e9bc1d92211cbad7ae82931846f7451" +checksum = "4ad70d68dba9e1f8aceda7aa6711965dfec1cac869f311a51bd08b3a2ccbce20" dependencies = [ "time-core", ] diff --git a/Cargo.toml b/Cargo.toml index 0be9ed180..7d98dcc32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ members = [ "packages/remove-console", "packages/styled-components", "packages/styled-jsx", + "packages/swc-magic", "packages/transform-imports", ] diff --git a/packages/constify/package.json b/packages/constify/package.json index 1e0b8f012..6bc56e251 100644 --- a/packages/constify/package.json +++ b/packages/constify/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-constify", - "version": "0.1.22", + "version": "0.1.23", "description": "SWC plugin for optimization", "main": "swc_plugin_constify.wasm", "scripts": { diff --git a/packages/constify/transform/Cargo.toml b/packages/constify/transform/Cargo.toml index 95365b188..dd9532a79 100644 --- a/packages/constify/transform/Cargo.toml +++ b/packages/constify/transform/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" license = "Apache-2.0" name = "swc_constify" repository = "https://github.com/swc-project/plugins.git" -version = "0.17.0" +version = "0.18.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/packages/emotion/package.json b/packages/emotion/package.json index 943a7bd9a..1463ad15b 100644 --- a/packages/emotion/package.json +++ b/packages/emotion/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-emotion", - "version": "2.5.92", + "version": "2.5.93", "description": "SWC plugin for emotion css-in-js library", "main": "swc_plugin_emotion.wasm", "scripts": { diff --git a/packages/emotion/transform/Cargo.toml b/packages/emotion/transform/Cargo.toml index ad0d558f4..70da1bfdd 100644 --- a/packages/emotion/transform/Cargo.toml +++ b/packages/emotion/transform/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" license = "Apache-2.0" name = "swc_emotion" repository = "https://github.com/swc-project/plugins.git" -version = "0.53.0" +version = "0.54.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/packages/jest/package.json b/packages/jest/package.json index b76ad3f86..f126d3ae4 100644 --- a/packages/jest/package.json +++ b/packages/jest/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-jest", - "version": "1.5.92", + "version": "1.5.93", "description": "SWC plugin for jest", "main": "swc_plugin_jest.wasm", "scripts": { diff --git a/packages/loadable-components/package.json b/packages/loadable-components/package.json index 871c95ef9..ea6bfa09e 100644 --- a/packages/loadable-components/package.json +++ b/packages/loadable-components/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-loadable-components", - "version": "0.3.92", + "version": "0.3.93", "description": "SWC plugin for `@loadable/components`", "main": "swc_plugin_loadable_components.wasm", "scripts": { diff --git a/packages/noop/package.json b/packages/noop/package.json index fc792bf0b..b4c881cd8 100644 --- a/packages/noop/package.json +++ b/packages/noop/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-noop", - "version": "1.5.90", + "version": "1.5.91", "description": "Noop SWC plugin, for debugging", "main": "swc_plugin_noop.wasm", "scripts": { diff --git a/packages/react-remove-properties/package.json b/packages/react-remove-properties/package.json index 6246f8476..14907ac21 100644 --- a/packages/react-remove-properties/package.json +++ b/packages/react-remove-properties/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-react-remove-properties", - "version": "1.5.92", + "version": "1.5.93", "description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-react-remove-properties", "main": "swc_plugin_react_remove_properties.wasm", "scripts": { diff --git a/packages/react-remove-properties/transform/Cargo.toml b/packages/react-remove-properties/transform/Cargo.toml index 7421a5372..6764088b0 100644 --- a/packages/react-remove-properties/transform/Cargo.toml +++ b/packages/react-remove-properties/transform/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" license = "Apache-2.0" name = "react_remove_properties" repository = "https://github.com/swc-project/plugins.git" -version = "0.5.0" +version = "0.6.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/packages/relay/package.json b/packages/relay/package.json index 78f034149..20f7c7fde 100644 --- a/packages/relay/package.json +++ b/packages/relay/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-relay", - "version": "1.5.92", + "version": "1.5.93", "description": "SWC plugin for relay", "main": "swc_plugin_relay.wasm", "types": "./types.d.ts", diff --git a/packages/relay/transform/Cargo.toml b/packages/relay/transform/Cargo.toml index 8cf4132d4..f5c470844 100644 --- a/packages/relay/transform/Cargo.toml +++ b/packages/relay/transform/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" license = "Apache-2.0" name = "swc_relay" repository = "https://github.com/swc-project/plugins.git" -version = "0.25.0" +version = "0.26.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/packages/remove-console/package.json b/packages/remove-console/package.json index dab8c2050..ce166400f 100644 --- a/packages/remove-console/package.json +++ b/packages/remove-console/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-remove-console", - "version": "1.5.92", + "version": "1.5.93", "description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-remove-console", "main": "swc_plugin_remove_console.wasm", "scripts": { diff --git a/packages/remove-console/transform/Cargo.toml b/packages/remove-console/transform/Cargo.toml index 0a6a9112d..d19901a85 100644 --- a/packages/remove-console/transform/Cargo.toml +++ b/packages/remove-console/transform/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" license = "Apache-2.0" name = "remove_console" repository = "https://github.com/swc-project/plugins.git" -version = "0.6.0" +version = "0.7.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/packages/styled-components/package.json b/packages/styled-components/package.json index 04fd6ac02..c4307bf8e 100644 --- a/packages/styled-components/package.json +++ b/packages/styled-components/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-styled-components", - "version": "1.5.92", + "version": "1.5.93", "description": "SWC plugin for styled-components", "main": "swc_plugin_styled_components.wasm", "scripts": { diff --git a/packages/styled-components/transform/Cargo.toml b/packages/styled-components/transform/Cargo.toml index 48c0aae30..fedef5eec 100644 --- a/packages/styled-components/transform/Cargo.toml +++ b/packages/styled-components/transform/Cargo.toml @@ -6,7 +6,7 @@ include = ["Cargo.toml", "src/**/*.rs"] license = "Apache-2.0" name = "styled_components" repository = "https://github.com/swc-project/plugins.git" -version = "0.77.0" +version = "0.78.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/packages/styled-jsx/package.json b/packages/styled-jsx/package.json index 1a83d6ae4..47950b389 100644 --- a/packages/styled-jsx/package.json +++ b/packages/styled-jsx/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-styled-jsx", - "version": "1.5.92", + "version": "1.5.93", "description": "SWC plugin for styled-jsx", "main": "swc_plugin_styled_jsx.wasm", "scripts": { diff --git a/packages/styled-jsx/transform/Cargo.toml b/packages/styled-jsx/transform/Cargo.toml index 9c753f4f8..9da1c914d 100644 --- a/packages/styled-jsx/transform/Cargo.toml +++ b/packages/styled-jsx/transform/Cargo.toml @@ -4,7 +4,7 @@ description = "AST transforms visitor for styled-jsx" edition = "2021" license = "Apache-2.0" name = "styled_jsx" -version = "0.54.0" +version = "0.55.0" [features] custom_transform = ["swc_common/concurrent"] diff --git a/packages/swc-magic/.npmignore b/packages/swc-magic/.npmignore new file mode 100644 index 000000000..1ed674f50 --- /dev/null +++ b/packages/swc-magic/.npmignore @@ -0,0 +1,2 @@ +transform/ +tests/ \ No newline at end of file diff --git a/packages/swc-magic/Cargo.toml b/packages/swc-magic/Cargo.toml new file mode 100644 index 000000000..ffd44fec6 --- /dev/null +++ b/packages/swc-magic/Cargo.toml @@ -0,0 +1,18 @@ +[package] +authors = ["강동윤 "] +description = "SWC Plugin for swc-magic" +edition = "2021" +license = "Apache-2.0" +name = "swc_plugin_swc_magic" +publish = false +version = "0.1.0" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +serde_json = "1.0.107" +swc_core = { version = "0.86.16", features = ["ecma_plugin_transform"] } + + +swc_magic = { path = "./transform" } diff --git a/packages/swc-magic/README.md b/packages/swc-magic/README.md new file mode 100644 index 000000000..6954bb4ab --- /dev/null +++ b/packages/swc-magic/README.md @@ -0,0 +1,21 @@ +# @swc/plugin-swc-magic + +## Usage + +.swcrc: + +```json +{ + "jsc": { + "experimental": { + "plugins": ["@swc/plugin-swc-magic"] + } + } +} +``` + +```js +import { markAsPure } from "@swc/magic"; + +markAsPure(() => console.log("This will be removed by the SWC minifier")); +``` diff --git a/packages/swc-magic/package.json b/packages/swc-magic/package.json new file mode 100644 index 000000000..5d3de3942 --- /dev/null +++ b/packages/swc-magic/package.json @@ -0,0 +1,21 @@ +{ + "name": "@swc/plugin-swc-magic", + "version": "1.5.93", + "description": "SWC plugin for swc-magic", + "main": "swc_plugin_swc_magic.wasm", + "scripts": { + "prepack": "cp ../../target/wasm32-wasi/release/swc_plugin_swc_magic.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/swc-magic/src/lib.rs b/packages/swc-magic/src/lib.rs new file mode 100644 index 000000000..6d2ac561c --- /dev/null +++ b/packages/swc-magic/src/lib.rs @@ -0,0 +1,30 @@ +#![allow(clippy::not_unsafe_ptr_arg_deref)] +#![feature(box_patterns)] + +use swc_core::{ + ecma::{ast::Program, visit::VisitMutWith}, + plugin::{ + plugin_transform, + proxies::{PluginCommentsProxy, TransformPluginProgramMetadata}, + }, +}; + +#[plugin_transform] +fn swc_magic_plugin(mut program: Program, data: TransformPluginProgramMetadata) -> Program { + let config = serde_json::from_str::( + &data + .get_transform_plugin_config() + .expect("failed to get plugin config for swc-magic"), + ) + .expect("invalid config for swc-magic"); + + let unresolved_mark = data.unresolved_mark; + + program.visit_mut_with(&mut swc_magic::swc_magic( + unresolved_mark, + config, + PluginCommentsProxy, + )); + + program +} diff --git a/packages/swc-magic/transform/Cargo.toml b/packages/swc-magic/transform/Cargo.toml new file mode 100644 index 000000000..4020efd51 --- /dev/null +++ b/packages/swc-magic/transform/Cargo.toml @@ -0,0 +1,21 @@ +[package] +authors = ["강동윤 "] +description = "AST transforms visitor for swc-magic" +edition = "2021" +license = "Apache-2.0" +name = "swc_magic" +version = "0.1.0" + +[dependencies] +serde = { version = "1.0.189", features = ["derive"] } +swc_atoms = "0.6.0" +swc_common = "0.33.1" +swc_ecma_ast = "0.110.1" +swc_ecma_utils = "0.124.8" +swc_ecma_visit = "0.96.1" + +[dev-dependencies] +swc_ecma_parser = "0.141.4" +swc_ecma_transforms_base = "0.134.9" +swc_ecma_transforms_testing = "0.137.9" +testing = "0.35.1" diff --git a/packages/swc-magic/transform/src/import_analyzer.rs b/packages/swc-magic/transform/src/import_analyzer.rs new file mode 100644 index 000000000..be4667100 --- /dev/null +++ b/packages/swc-magic/transform/src/import_analyzer.rs @@ -0,0 +1,92 @@ +use swc_atoms::JsWord; +use swc_common::collections::{AHashMap, AHashSet}; +use swc_ecma_ast::*; +use swc_ecma_visit::{noop_visit_type, Visit, VisitWith}; + +#[derive(Debug, Default)] +pub(crate) struct ImportMap { + /// Map from module name to (module path, exported symbol) + imports: AHashMap, + + namespace_imports: AHashMap, + + imported_modules: AHashSet, +} + +impl ImportMap { + /// Returns true if `e` is an import of `orig_name` from `module`. + pub fn is_import(&self, e: &Expr, module: &str, orig_name: &str) -> bool { + match e { + Expr::Ident(i) => { + if let Some((i_src, i_sym)) = self.imports.get(&i.to_id()) { + i_src == module && i_sym == orig_name + } else { + false + } + } + + Expr::Member(MemberExpr { + obj: box Expr::Ident(obj), + prop: MemberProp::Ident(prop), + .. + }) => { + if let Some(obj_src) = self.namespace_imports.get(&obj.to_id()) { + obj_src == module && prop.sym == *orig_name + } else { + false + } + } + + _ => false, + } + } + + pub fn analyze(m: &Module) -> Self { + let mut data = ImportMap::default(); + + m.visit_with(&mut Analyzer { data: &mut data }); + + data + } +} + +struct Analyzer<'a> { + data: &'a mut ImportMap, +} + +impl Visit for Analyzer<'_> { + noop_visit_type!(); + + fn visit_import_decl(&mut self, import: &ImportDecl) { + self.data.imported_modules.insert(import.src.value.clone()); + + for s in &import.specifiers { + let (local, orig_sym) = match s { + ImportSpecifier::Named(ImportNamedSpecifier { + local, imported, .. + }) => match imported { + Some(imported) => (local.to_id(), orig_name(imported)), + _ => (local.to_id(), local.sym.clone()), + }, + ImportSpecifier::Default(s) => (s.local.to_id(), "default".into()), + ImportSpecifier::Namespace(s) => { + self.data + .namespace_imports + .insert(s.local.to_id(), import.src.value.clone()); + continue; + } + }; + + self.data + .imports + .insert(local, (import.src.value.clone(), orig_sym)); + } + } +} + +fn orig_name(n: &ModuleExportName) -> JsWord { + match n { + ModuleExportName::Ident(v) => v.sym.clone(), + ModuleExportName::Str(v) => v.value.clone(), + } +} diff --git a/packages/swc-magic/transform/src/lib.rs b/packages/swc-magic/transform/src/lib.rs new file mode 100644 index 000000000..e28bd4d05 --- /dev/null +++ b/packages/swc-magic/transform/src/lib.rs @@ -0,0 +1,115 @@ +#![feature(box_patterns)] + +use import_analyzer::ImportMap; +use serde::Deserialize; +use swc_atoms::Atom; +use swc_common::{ + comments::Comments, errors::HANDLER, util::take::Take, Mark, Span, Spanned, DUMMY_SP, +}; +use swc_ecma_ast::{CallExpr, Callee, EmptyStmt, Expr, Module, ModuleDecl, ModuleItem, Stmt}; +use swc_ecma_visit::{VisitMut, VisitMutWith}; + +mod import_analyzer; + +#[derive(Debug, Clone, Deserialize)] +pub struct Config { + #[serde(default = "default_import_path")] + pub import_path: Atom, +} + +fn default_import_path() -> Atom { + Atom::from("@swc/magic") +} + +impl Config {} + +pub fn swc_magic(_unreolved_mark: Mark, config: Config, comments: C) -> impl VisitMut +where + C: Comments, +{ + Magic { + config, + comments, + imports: Default::default(), + } +} + +const MARK_AS_PURE_FN_NAME: &str = "markAsPure"; + +/// Handles functions from `@swc/magic`. +struct Magic +where + C: Comments, +{ + config: Config, + comments: C, + imports: ImportMap, +} + +impl VisitMut for Magic +where + C: Comments, +{ + fn visit_mut_expr(&mut self, e: &mut Expr) { + e.visit_mut_children_with(self); + + if let Expr::Call(CallExpr { + span, + callee: Callee::Expr(callee), + args, + .. + }) = e + { + if !self + .imports + .is_import(callee, &self.config.import_path, MARK_AS_PURE_FN_NAME) + { + return; + } + + if args.len() != 1 { + HANDLER.with(|handler| { + handler + .struct_span_err(*span, "markAsPure() does not support multiple arguments") + .emit(); + }); + return; + } + + *e = *args[0].expr.take(); + + let mut lo = e.span().lo; + if lo.is_dummy() { + lo = Span::dummy_with_cmt().lo; + } + + self.comments.add_pure_comment(lo); + } + } + + fn visit_mut_module(&mut self, m: &mut Module) { + self.imports = ImportMap::analyze(&m); + + m.visit_mut_children_with(self); + + // Remove Stmt::Empty + m.body.retain(|item| { + if let ModuleItem::Stmt(Stmt::Empty(..)) = item { + false + } else { + true + } + }); + } + + fn visit_mut_module_item(&mut self, m: &mut ModuleItem) { + if let ModuleItem::ModuleDecl(ModuleDecl::Import(import)) = m { + if import.src.value == self.config.import_path { + *m = ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP })); + return; + } + } + + m.visit_mut_children_with(self); + } +} diff --git a/packages/swc-magic/transform/tests/fixture.rs b/packages/swc-magic/transform/tests/fixture.rs new file mode 100644 index 000000000..0bb37107f --- /dev/null +++ b/packages/swc-magic/transform/tests/fixture.rs @@ -0,0 +1,33 @@ +use std::path::PathBuf; + +use swc_common::{chain, Mark}; +use swc_ecma_parser::Syntax; +use swc_ecma_transforms_base::resolver; +use swc_ecma_transforms_testing::test_fixture; +use swc_ecma_visit::as_folder; + +#[testing::fixture("tests/fixture/**/input.js")] +fn pure(input: PathBuf) { + let output = input.parent().unwrap().join("output.js"); + test_fixture( + Syntax::default(), + &|tr| { + let unresolved_mark = Mark::new(); + let top_level_mark = Mark::new(); + + chain!( + resolver(unresolved_mark, top_level_mark, false), + as_folder(swc_magic::swc_magic( + unresolved_mark, + swc_magic::Config { + import_path: "@swc/magic".into() + }, + tr.comments.clone() + )) + ) + }, + &input, + &output, + Default::default(), + ); +} diff --git a/packages/swc-magic/transform/tests/fixture/no-name-clash/input.js b/packages/swc-magic/transform/tests/fixture/no-name-clash/input.js new file mode 100644 index 000000000..1d971f4f1 --- /dev/null +++ b/packages/swc-magic/transform/tests/fixture/no-name-clash/input.js @@ -0,0 +1,3 @@ +import { markAsPure } from 'not-swc-magic' + +markAsPure(console.log('test!')) \ No newline at end of file diff --git a/packages/swc-magic/transform/tests/fixture/no-name-clash/output.js b/packages/swc-magic/transform/tests/fixture/no-name-clash/output.js new file mode 100644 index 000000000..cbd17ad00 --- /dev/null +++ b/packages/swc-magic/transform/tests/fixture/no-name-clash/output.js @@ -0,0 +1,2 @@ +import { markAsPure } from 'not-swc-magic'; +markAsPure(console.log('test!')); diff --git a/packages/swc-magic/transform/tests/fixture/simple/input.js b/packages/swc-magic/transform/tests/fixture/simple/input.js new file mode 100644 index 000000000..5001f9827 --- /dev/null +++ b/packages/swc-magic/transform/tests/fixture/simple/input.js @@ -0,0 +1,3 @@ +import { markAsPure } from '@swc/magic' + +markAsPure(console.log('test!')) \ No newline at end of file diff --git a/packages/swc-magic/transform/tests/fixture/simple/output.js b/packages/swc-magic/transform/tests/fixture/simple/output.js new file mode 100644 index 000000000..764370a2b --- /dev/null +++ b/packages/swc-magic/transform/tests/fixture/simple/output.js @@ -0,0 +1 @@ +/*#__PURE__*/ console.log('test!'); diff --git a/packages/transform-imports/package.json b/packages/transform-imports/package.json index 47e1f39cb..ba4a7c701 100644 --- a/packages/transform-imports/package.json +++ b/packages/transform-imports/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-transform-imports", - "version": "1.5.92", + "version": "1.5.93", "description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-transform-imports", "main": "swc_plugin_transform_imports.wasm", "scripts": { diff --git a/packages/transform-imports/transform/Cargo.toml b/packages/transform-imports/transform/Cargo.toml index 8947ae7eb..15d92698e 100644 --- a/packages/transform-imports/transform/Cargo.toml +++ b/packages/transform-imports/transform/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" license = "Apache-2.0" name = "modularize_imports" repository = "https://github.com/swc-project/plugins.git" -version = "0.50.0" +version = "0.51.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html