Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenshain-sc authored Dec 31, 2024
2 parents 3c65a25 + a7a22a2 commit 5c0abf3
Show file tree
Hide file tree
Showing 28 changed files with 172 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-vans-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@swc/plugin-transform-imports": patch
---

feat: Support dynamic imports from `transform-imports` plugin
4 changes: 2 additions & 2 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 crates/swc_icu_messageformat_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = { workspace = true }
license = { workspace = true }
name = "swc_icu_messageformat_parser"
repository = { workspace = true }
version = "3.0.0"
version = "3.0.1"


[features]
Expand Down
22 changes: 19 additions & 3 deletions crates/swc_icu_messageformat_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,16 @@ impl<'s> Parser<'s> {
self.bump();
}

&self.message[start_offset..self.offset()]
let length = self.offset() - start_offset;
#[cfg(feature = "utf16")]
let (start_offset, length) = (
self.message_utf16[..start_offset].to_string().len(),
self.message_utf16[start_offset..start_offset + length]
.to_string()
.len(),
);

&self.message[start_offset..start_offset + length]
}

fn parse_literal(&self, nesting_level: usize, parent_arg_type: &str) -> Result<AstElement> {
Expand Down Expand Up @@ -1735,9 +1744,16 @@ impl<'s> Parser<'s> {
if self.is_eof() {
return None;
}
self.message[self.offset() + self.char().len_utf8()..]

#[cfg(feature = "utf16")]
return self.message_utf16[self.offset() + self.char().len_utf16()..]
.chars()
.next()
.next();

#[cfg(not(feature = "utf16"))]
return self.message[self.offset() + self.char().len_utf8()..]
.chars()
.next();
}

/// Returns true if the next call to `bump` would return false.
Expand Down
43 changes: 43 additions & 0 deletions crates/swc_icu_messageformat_parser/tests/fixtures/unicode_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<a>ö🚀</a>
---
{}
---
{
"err": null,
"val": [
{
"children": [
{
"location": {
"end": {
"column": 6,
"line": 1,
"offset": 6
},
"start": {
"column": 4,
"line": 1,
"offset": 3
}
},
"type": 0,
"value": "ö🚀"
}
],
"location": {
"end": {
"column": 10,
"line": 1,
"offset": 10
},
"start": {
"column": 1,
"line": 1,
"offset": 0
}
},
"type": 8,
"value": "a"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn read_sections(file: PathBuf) -> TestFixtureSections {
fixture("tests/fixtures/treat_unicode_nbsp_as_whitespace")
)]
#[cfg_attr(feature = "utf16", fixture("tests/fixtures/trivial_2"))]
#[cfg_attr(feature = "utf16", fixture("tests/fixtures/unicode_1"))]
#[fixture("tests/fixtures/uppercase_tag_1")]
#[fixture("tests/fixtures/expect_number_arg_skeleton_token_1")]
#[fixture("tests/fixtures/self_closing_tag_1")]
Expand Down
3 changes: 2 additions & 1 deletion packages/emotion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/emotion"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
6 changes: 6 additions & 0 deletions packages/formatjs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-formatjs

## 2.0.4

### Patch Changes

- 2ae5319: Fix parsing of unicode

## 2.0.3

### Patch Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/formatjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ FormatJS SWC plugin, maintained by SWC team.

# @swc/plugin-formatjs

## 2.0.4

### Patch Changes

- 2ae5319: Fix parsing of unicode

## 2.0.3

### Patch Changes
Expand Down
5 changes: 3 additions & 2 deletions packages/formatjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-formatjs",
"version": "2.0.3",
"version": "2.0.4",
"description": "FormatJS SWC plugin",
"main": "swc_plugin_formatjs.wasm",
"scripts": {
Expand All @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/formatjs"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
2 changes: 1 addition & 1 deletion packages/formatjs/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = { workspace = true }
license = { workspace = true }
name = "swc_formatjs_transform"
repository = { workspace = true }
version = "3.0.0"
version = "3.0.1"

[features]
custom_transform = []
Expand Down
3 changes: 2 additions & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/jest"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/loadable-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/loadable-components"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/noop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/noop"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/prefresh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/prefresh"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/react-remove-properties/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/react-remove-properties"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/relay"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/remove-console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/remove-console"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/styled-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/styled-components"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/styled-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/styled-jsx"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/swc-confidential/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/swc-confidential"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/swc-magic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/swc-magic"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/swc-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/swc-sdk"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
3 changes: 2 additions & 1 deletion packages/transform-imports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://swc.rs",
"repository": {
"type": "git",
"url": "+https://github.com/swc-project/plugins.git"
"url": "git+https://github.com/swc-project/plugins.git",
"directory": "packages/transform-imports"
},
"bugs": {
"url": "https://github.com/swc-project/plugins/issues"
Expand Down
44 changes: 43 additions & 1 deletion packages/transform-imports/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use swc_atoms::Atom;
use swc_cached::regex::CachedRegex;
use swc_common::DUMMY_SP;
use swc_ecma_ast::{ImportDecl, ImportSpecifier, ModuleExportName, *};
use swc_ecma_visit::{fold_pass, noop_fold_type, Fold};
use swc_ecma_visit::{fold_pass, noop_fold_type, Fold, FoldWith};

static DUP_SLASH_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"//").unwrap());

Expand Down Expand Up @@ -323,11 +323,52 @@ impl FoldImports {
}
None
}

fn handle_dynamic_import(&mut self, call: &CallExpr) -> Option<Atom> {
let first_arg = call.args.first()?;
if first_arg.spread.is_some() {
return None;
}

match &*first_arg.expr {
Expr::Lit(Lit::Str(s)) => {
let rewriter = self.should_rewrite(&s.value)?;

let new_module = rewriter.new_path(None);
Some(new_module)
}

Expr::Tpl(tpl) => {
if tpl.exprs.is_empty() {
let cooked = tpl.quasis[0].cooked.as_ref()?;
let rewriter = self.should_rewrite(cooked)?;

let new_module = rewriter.new_path(None);
Some(new_module)
} else {
None
}
}
_ => None,
}
}
}

impl Fold for FoldImports {
noop_fold_type!();

fn fold_call_expr(&mut self, mut call: CallExpr) -> CallExpr {
call = call.fold_children_with(self);

if call.callee.is_import() {
if let Some(new_module) = self.handle_dynamic_import(&call) {
call.args.first_mut().unwrap().expr = new_module.into();
}
}

call
}

fn fold_module(&mut self, mut module: Module) -> Module {
let mut new_items: Vec<ModuleItem> = vec![];
for item in module.body {
Expand Down Expand Up @@ -392,6 +433,7 @@ impl Fold for FoldImports {
}
}
}
let new_items = new_items.fold_children_with(self);
module.body = new_items;
module
}
Expand Down
Loading

0 comments on commit 5c0abf3

Please sign in to comment.