Skip to content

Commit

Permalink
fix(emotion): Remove multiline comments (#229)
Browse files Browse the repository at this point in the history
This is because emotion@10 runtime cannot handle multiline comments properly, the @emotion/babel-plugin removes comments as well.
  • Loading branch information
JSerFeng authored Nov 7, 2023
1 parent d4bdd4d commit 02f9822
Show file tree
Hide file tree
Showing 25 changed files with 80 additions and 30 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/constify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-constify",
"version": "0.1.26",
"version": "0.1.27",
"description": "SWC plugin for optimization",
"main": "swc_plugin_constify.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/constify/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_constify"
repository = "https://github.com/swc-project/plugins.git"
version = "0.21.0"
version = "0.22.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-emotion",
"version": "2.5.96",
"version": "2.5.97",
"description": "SWC plugin for emotion css-in-js library",
"main": "swc_plugin_emotion.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_emotion"
repository = "https://github.com/swc-project/plugins.git"
version = "0.57.0"
version = "0.58.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
29 changes: 29 additions & 0 deletions packages/emotion/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ static INVALID_SINGLE_LINE_COMMENT: Lazy<Regex> = Lazy::new(|| {
.unwrap()
});

static MULTI_LINE_COMMENT: Lazy<Regex> = Lazy::new(|| {
RegexBuilder::new(r"(?s)/\*.*?\*/")
.multi_line(true)
.build()
.unwrap()
});

static SPACE_AROUND_COLON: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\s*(?P<s>[:;,\{,\}])\s*").unwrap());

Expand Down Expand Up @@ -838,6 +845,16 @@ fn match_css_export(item: &ExportItem, prop: &MemberProp) -> bool {

#[inline]
fn minify_css_string(input: &str, is_first_item: bool, is_last_item: bool) -> Cow<str> {
match MULTI_LINE_COMMENT.replace_all(input, "$s") {
Cow::Borrowed(borrowed) => remove_space_and_comments(borrowed, is_first_item, is_last_item),
Cow::Owned(owned) => remove_space_and_comments(&owned, is_first_item, is_last_item)
.into_owned()
.into(),
}
}

#[inline]
fn remove_space_and_comments(input: &str, is_first_item: bool, is_last_item: bool) -> Cow<str> {
match INVALID_SINGLE_LINE_COMMENT.replace_all(input, "$s") {
Cow::Borrowed(borrowed) => remove_space_around_colon(borrowed, is_first_item, is_last_item),
Cow::Owned(owned) => remove_space_around_colon(&owned, is_first_item, is_last_item)
Expand Down Expand Up @@ -895,4 +912,16 @@ mod test_emotion {
"color:red;background-image:url(http://dummy-url)"
)
}

#[test]
fn should_remove_comments() {
assert_eq!(
minify_css_string(
"color: red;/*comment\ncomments*/background-image:url(http://dummy-url).foo{/*comments\n*/\n}",
true,
true
),
"color:red;background-image:url(http://dummy-url).foo{}"
)
}
}
13 changes: 13 additions & 0 deletions packages/emotion/transform/tests/fixture/comments/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import styled from "@emotion/styled";

export default styled.div`
color: red;
.foo {
color: blue;
/**
multi line comments
*/
}
/* /* */
width: 10px;
`;
8 changes: 8 additions & 0 deletions packages/emotion/transform/tests/fixture/comments/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styled from "@emotion/styled";
export default /*#__PURE__*/ styled("div", {
target: "e1h8m75k0",
label: "[local]",
})(
"color:red;.foo{color:blue;}width:10px;",
"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBzdHlsZWQgZnJvbSBcIkBlbW90aW9uL3N0eWxlZFwiO1xuXG5leHBvcnQgZGVmYXVsdCBzdHlsZWQuZGl2YFxuICBjb2xvcjogcmVkO1xuICAuZm9vIHtcbiAgICBjb2xvcjogYmx1ZTtcbiAgICAvKipcbiAgICAgIG11bHRpIGxpbmUgY29tbWVudHNcbiAgICAqL1xuICB9XG4gIC8qIC8qICovXG4gIHdpZHRoOiAxMHB4O1xuYDtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFZSJ9 */"
);
2 changes: 1 addition & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-jest",
"version": "1.5.96",
"version": "1.5.97",
"description": "SWC plugin for jest",
"main": "swc_plugin_jest.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/loadable-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-loadable-components",
"version": "0.3.96",
"version": "0.3.97",
"description": "SWC plugin for `@loadable/components`",
"main": "swc_plugin_loadable_components.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/noop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-noop",
"version": "1.5.94",
"version": "1.5.95",
"description": "Noop SWC plugin, for debugging",
"main": "swc_plugin_noop.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-remove-properties/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-react-remove-properties",
"version": "1.5.96",
"version": "1.5.97",
"description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-react-remove-properties",
"main": "swc_plugin_react_remove_properties.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-remove-properties/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "react_remove_properties"
repository = "https://github.com/swc-project/plugins.git"
version = "0.9.0"
version = "0.10.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-relay",
"version": "1.5.96",
"version": "1.5.97",
"description": "SWC plugin for relay",
"main": "swc_plugin_relay.wasm",
"types": "./types.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "swc_relay"
repository = "https://github.com/swc-project/plugins.git"
version = "0.29.0"
version = "0.30.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion packages/remove-console/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-remove-console",
"version": "1.5.96",
"version": "1.5.97",
"description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-remove-console",
"main": "swc_plugin_remove_console.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/remove-console/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "remove_console"
repository = "https://github.com/swc-project/plugins.git"
version = "0.10.0"
version = "0.11.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion packages/styled-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-styled-components",
"version": "1.5.96",
"version": "1.5.97",
"description": "SWC plugin for styled-components",
"main": "swc_plugin_styled_components.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-components/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.81.0"
version = "0.82.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion packages/styled-jsx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-styled-jsx",
"version": "1.5.96",
"version": "1.5.97",
"description": "SWC plugin for styled-jsx",
"main": "swc_plugin_styled_jsx.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-jsx/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "AST transforms visitor for styled-jsx"
edition = "2021"
license = "Apache-2.0"
name = "styled_jsx"
version = "0.58.0"
version = "0.59.0"

[features]
custom_transform = ["swc_common/concurrent"]
Expand Down
2 changes: 1 addition & 1 deletion packages/swc-magic/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-swc-magic",
"version": "1.5.96",
"version": "1.5.97",
"description": "SWC plugin for swc-magic",
"main": "swc_plugin_swc_magic.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/swc-magic/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "AST transforms visitor for swc-magic"
edition = "2021"
license = "Apache-2.0"
name = "swc_magic"
version = "0.4.0"
version = "0.5.0"

[dependencies]
serde = { version = "1.0.189", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion packages/transform-imports/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-transform-imports",
"version": "1.5.96",
"version": "1.5.97",
"description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-transform-imports",
"main": "swc_plugin_transform_imports.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/transform-imports/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "modularize_imports"
repository = "https://github.com/swc-project/plugins.git"
version = "0.54.0"
version = "0.55.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down

0 comments on commit 02f9822

Please sign in to comment.