diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a9e4355..936d8d4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,7 +14,7 @@ jobs: - uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: stable + toolchain: 1.65.0 target: wasm32-wasi override: true diff --git a/README.md b/README.md index 3e1dd5a..690ded7 100644 --- a/README.md +++ b/README.md @@ -38,18 +38,22 @@ https://swc.rs/docs/configuration/swcrc "$schema": "https://json.schemastore.org/swcrc", "jsc": { "experimental": { - "plugins": ["@lingui/swc-plugin", { - - // Optional - // Unlike the JS version this option must be passed as object only. - // Docs https://lingui.dev/ref/conf#runtimeconfigmodule - // "runtimeModules": { - // "i18n": ["@lingui/core", "i18n"], - // "trans": ["@lingui/react", "Trans"] - // } - }] - } - } + "plugins": [ + [ + "@lingui/swc-plugin", + { + // Optional + // Unlike the JS version this option must be passed as object only. + // Docs https://lingui.dev/ref/conf#runtimeconfigmodule + // "runtimeModules": { + // "i18n": ["@lingui/core", "i18n"], + // "trans": ["@lingui/react", "Trans"] + // } + }, + ], + ], + }, + }, } ``` diff --git a/src/lib.rs b/src/lib.rs index 2c929a1..3944009 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,12 +133,16 @@ impl<'a> Fold for LinguiMacroFolder { let (i18n_source, i18n_export) = self.ctx.options.runtime_modules.i18n.clone(); let (trans_source, trans_export) = self.ctx.options.runtime_modules.trans.clone(); + let mut insert_index: usize = 0; + let mut index = 0; + n.retain(|m| { if let ModuleItem::ModuleDecl(ModuleDecl::Import(imp)) = m { // drop macro imports if &imp.src.value == "@lingui/macro" { self.has_lingui_macro_imports = true; self.ctx.register_macro_import(imp); + insert_index = index; return false; } @@ -159,20 +163,21 @@ impl<'a> Fold for LinguiMacroFolder { } } - true + index +=1; + true }); - n = n.fold_children_with(self); + n = n.fold_children_with(self); - if !has_i18n_import && self.ctx.should_add_18n_import { - n.insert(0, create_import(i18n_source.into(), quote_ident!(i18n_export[..]))); - } + if !has_i18n_import && self.ctx.should_add_18n_import { + n.insert(insert_index, create_import(i18n_source.into(), quote_ident!(i18n_export[..]))); + } - if !has_trans_import && self.ctx.should_add_trans_import { - n.insert(0, create_import(trans_source.into(), quote_ident!(trans_export[..]))); - } + if !has_trans_import && self.ctx.should_add_trans_import { + n.insert(insert_index, create_import(trans_source.into(), quote_ident!(trans_export[..]))); + } - n + n } fn fold_expr(&mut self, expr: Expr) -> Expr { diff --git a/src/tests/imports.rs b/src/tests/imports.rs index 85ed39f..b841916 100644 --- a/src/tests/imports.rs +++ b/src/tests/imports.rs @@ -183,3 +183,23 @@ to!( ;; "# ); +to!( + // https://github.com/lingui/swc-plugin/issues/21 + should_add_imports_after_directive_prologues, + r#" + "use client"; + import {t} from "@lingui/macro" + import foo from "bar" + t`Text` + "#, + + r#" + "use client"; + import { i18n } from "@lingui/core"; + import foo from "bar"; + i18n._({ + id: "xeiujy", + message: "Text" + }); + "# +);