Skip to content

Commit

Permalink
fix(styled-components): Detect imported helper functions (#236)
Browse files Browse the repository at this point in the history
This fixes some tagged template literals using styled-components helper functions imported by name imports are not detected.

For example, previously the following styles were not detected and minified.

```js
import { createGlobalStyle, css, keyframes } from 'styled-components'

const key = keyframes`
  to {
    transform: rotate(360deg);
  }
`

const color = css`
  color: ${theColor};
`

const GlobalStyles = createGlobalStyle`
  html {
    color: red;
  }
`
```

---------

Co-authored-by: 강동윤 (Donny) <[email protected]>
  • Loading branch information
ciffelia and kdy1 authored Nov 25, 2023
1 parent 0538650 commit f3d55f4
Show file tree
Hide file tree
Showing 28 changed files with 67 additions and 84 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.30",
"version": "0.1.31",
"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.25.0"
version = "0.26.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.100",
"version": "2.5.101",
"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.61.0"
version = "0.62.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/jest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-jest",
"version": "1.5.100",
"version": "1.5.101",
"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.100",
"version": "0.3.101",
"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.98",
"version": "1.5.99",
"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.100",
"version": "1.5.101",
"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.13.0"
version = "0.14.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.100",
"version": "1.5.101",
"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.33.0"
version = "0.34.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.100",
"version": "1.5.101",
"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.14.0"
version = "0.15.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.100",
"version": "1.5.101",
"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.85.0"
version = "0.86.0"

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

Expand Down
12 changes: 8 additions & 4 deletions packages/styled-components/transform/src/utils/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,20 @@ impl Visit for Analyzer<'_> {
for s in &i.specifiers {
match s {
ImportSpecifier::Named(s) => {
if s.imported
let imported = s
.imported
.as_ref()
.map(|v| match v {
ModuleExportName::Ident(v) => &*v.sym,
ModuleExportName::Str(v) => &*v.value,
})
.unwrap_or(&*s.local.sym)
== "styled"
{
.unwrap_or(&*s.local.sym);
if imported == "styled" {
self.state.imported_local_name = Some(s.local.to_id());
} else {
self.state
.imported_local_named
.insert(imported.to_string(), s.local.to_id());
}
}
ImportSpecifier::Default(s) => {
Expand Down
61 changes: 23 additions & 38 deletions packages/styled-components/transform/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, cell::RefCell};
use std::{borrow::Cow, collections::HashMap};

use swc_atoms::js_word;
use swc_common::{collections::AHashMap, SyntaxContext};
use swc_common::SyntaxContext;
use swc_ecma_ast::*;

pub use self::analyzer::{analyze, analyzer};
Expand Down Expand Up @@ -58,10 +58,12 @@ pub struct State {

unresolved_ctxt: Option<SyntaxContext>,

/// Default import
imported_local_name: Option<Id>,
/// Named imports
imported_local_named: HashMap<String, Id>,
/// Namespace imports
imported_local_ns: Option<Id>,
import_name_cache: RefCell<AHashMap<Id, Id>>,
}

impl State {
Expand Down Expand Up @@ -213,46 +215,29 @@ impl State {
pub(crate) fn import_local_name(
&self,
name: &str,
cache_identifier: Option<&Ident>,
_cache_identifier: Option<&Ident>,
) -> Option<Id> {
if name == "default" {
if let Some(cached) = self.imported_local_name.clone() {
return Some(cached);
}
if let Some(cached) = self.imported_local_ns.clone() {
return Some(cached);
}
}

if let Some(..) = self.imported_local_ns {
return Some((name.into(), Default::default()));
}

let cache_key = cache_identifier.map(|i| i.to_id()).unwrap_or_default();

let ctxt = self.unresolved_ctxt.unwrap_or_default();

let local_name = if self.styled_required.is_some() {
Some(if name == "default" {
"styled".into()
if self.imported_local_name.is_some() {
self.imported_local_name.clone()
} else if self.imported_local_ns.is_some() {
self.imported_local_ns.clone()
} else if self.styled_required.is_some() {
Some(("styled".into(), self.unresolved_ctxt.unwrap_or_default()))
} else {
name.into()
})
None
}
} else {
None
};

if let Some(cached) = self.import_name_cache.borrow().get(&cache_key) {
return Some(cached.clone());
}

let name = local_name.map(|word| (word, ctxt));

if let Some(name) = name.clone() {
self.import_name_cache.borrow_mut().insert(cache_key, name);
if self.imported_local_ns.is_some() {
Some((name.into(), Default::default()))
} else if self.imported_local_named.contains_key(name) {
self.imported_local_named.get(name).cloned()
} else if self.styled_required.is_some() {
Some((name.into(), self.unresolved_ctxt.unwrap_or_default()))
} else {
None
}
}

name
}

pub(crate) fn set_import_name(&mut self, id: Id) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ssr": false,
"displayName": false,
"transpileTemplateLiterals": false,
"minify": true
}
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.100",
"version": "1.5.101",
"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.62.0"
version = "0.63.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.100",
"version": "1.5.101",
"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.8.0"
version = "0.9.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.100",
"version": "1.5.101",
"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.57.0"
version = "0.58.0"

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

Expand Down

0 comments on commit f3d55f4

Please sign in to comment.