Skip to content

Commit

Permalink
fix(transform-imports): FIx default behavior (#332)
Browse files Browse the repository at this point in the history
Inverts #324 as it caused some problems for next.js
  • Loading branch information
kdy1 authored Jul 11, 2024
1 parent 07b6c5a commit c34b259
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions packages/emotion/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ impl<C: Comments> EmotionTransformer<C> {
.and_then(|s| {
s.rfind('\\')
.map(|pos| &s[pos + 1..]) // if backslashes are found, take the last part
.or(Some(s)) // otherwise use the whole path
})
.or(Some(s)) // otherwise use the whole path
})
.map(|s| s.to_owned()),
cm,
comments,
Expand Down
6 changes: 6 additions & 0 deletions packages/transform-imports/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-transform-imports

## 2.0.9

### Patch Changes

- f39705a: Disable default/namespace import handling by default

## 2.0.8

### Patch Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/transform-imports/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

# @swc/plugin-transform-imports

## 2.0.9

### Patch Changes

- f39705a: Disable default/namespace import handling by default

## 2.0.8

### Patch Changes
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": "2.0.8",
"version": "2.0.9",
"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 @@ -11,7 +11,7 @@ homepage = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
version = "0.68.18"
version = "0.68.19"

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

Expand Down
10 changes: 7 additions & 3 deletions packages/transform-imports/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct PackageConfig {
#[serde(default)]
pub prevent_full_import: bool,
#[serde(default)]
pub handle_default_import: bool,
#[serde(default)]
pub handle_namespace_import: bool,
#[serde(default)]
pub skip_default_conversion: bool,
}

Expand Down Expand Up @@ -185,7 +189,7 @@ impl<'a> Rewriter<'a> {
with: None,
});
}
ExportSpecifier::Namespace(ns_spec) if !self.config.prevent_full_import => {
ExportSpecifier::Namespace(ns_spec) if self.config.handle_namespace_import => {
let name_str = match &ns_spec.name {
ModuleExportName::Ident(x) => x.as_ref(),
ModuleExportName::Str(x) => x.value.as_ref(),
Expand Down Expand Up @@ -253,7 +257,7 @@ impl<'a> Rewriter<'a> {
phase: Default::default(),
});
}
ImportSpecifier::Namespace(ns_spec) if !self.config.prevent_full_import => {
ImportSpecifier::Namespace(ns_spec) if self.config.handle_namespace_import => {
let name_str = ns_spec.local.as_ref();
let new_path = self.new_path(Some(name_str));
let specifier = ImportSpecifier::Namespace(ns_spec.clone());
Expand All @@ -266,7 +270,7 @@ impl<'a> Rewriter<'a> {
phase: Default::default(),
});
}
ImportSpecifier::Default(def_spec) if !self.config.prevent_full_import => {
ImportSpecifier::Default(def_spec) if self.config.handle_default_import => {
let name_str = def_spec.local.as_ref();
let new_path = self.new_path(Some(name_str));
let specifier = ImportSpecifier::Default(def_spec.clone());
Expand Down
12 changes: 12 additions & 0 deletions packages/transform-imports/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ fn modularize_imports_fixture(input: PathBuf) {
transform: "react-bootstrap/lib/{{member}}".into(),
prevent_full_import: false,
skip_default_conversion: false,
handle_default_import: true,
handle_namespace_import: true,
},
),
(
Expand All @@ -34,6 +36,8 @@ fn modularize_imports_fixture(input: PathBuf) {
transform: "my-library/{{ matches.[1] }}/{{member}}".into(),
prevent_full_import: false,
skip_default_conversion: false,
handle_default_import: true,
handle_namespace_import: true,
},
),
(
Expand All @@ -42,6 +46,8 @@ fn modularize_imports_fixture(input: PathBuf) {
transform: "my-library-2/{{ camelCase member }}".into(),
prevent_full_import: false,
skip_default_conversion: true,
handle_default_import: true,
handle_namespace_import: true,
},
),
(
Expand All @@ -50,6 +56,8 @@ fn modularize_imports_fixture(input: PathBuf) {
transform: "my-library-3/{{ kebabCase member }}".into(),
prevent_full_import: false,
skip_default_conversion: true,
handle_default_import: true,
handle_namespace_import: true,
},
),
(
Expand All @@ -76,6 +84,8 @@ fn modularize_imports_fixture(input: PathBuf) {
.into(),
prevent_full_import: false,
skip_default_conversion: true,
handle_default_import: true,
handle_namespace_import: true,
},
),
(
Expand All @@ -84,6 +94,8 @@ fn modularize_imports_fixture(input: PathBuf) {
transform: "transformed-{{matches.[1]}}".into(),
prevent_full_import: false,
skip_default_conversion: true,
handle_default_import: true,
handle_namespace_import: true,
},
),
]
Expand Down

0 comments on commit c34b259

Please sign in to comment.