Skip to content

Commit c863faa

Browse files
chore(vite-plugin): add error handling for dynamic import of @lingui/core/macro (#2056)
Co-authored-by: Timofei Iatsenko <[email protected]>
1 parent 9601701 commit c863faa

File tree

10 files changed

+74
-20
lines changed

10 files changed

+74
-20
lines changed

packages/vite-plugin/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ export function lingui({
5454
)
5555
}
5656
},
57+
resolveDynamicImport(id, importer) {
58+
if (macroIds.has(id as string)) {
59+
throw new Error(
60+
`The macro you imported from "${id}" cannot be dynamically imported. \n` +
61+
`Please check the import statement in file "${importer}". \n` +
62+
`Please see the documentation for how to configure Vite with Lingui correctly: ` +
63+
"https://lingui.dev/tutorials/setup-vite"
64+
)
65+
}
66+
},
5767
},
5868
{
5969
name: "vite-plugin-lingui",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export async function load() {
2+
const { t } = await import("@lingui/core/macro")
3+
return t`Ola`
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from "@lingui/cli"
2+
3+
export default defineConfig({
4+
locales: ["en"],
5+
catalogs: [
6+
{
7+
path: "<rootDir>/locale/{locale}",
8+
},
9+
],
10+
format: "po",
11+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
msgid "Hello World"
2+
msgstr "Hello World"
3+
4+
msgid "My name is {name}"
5+
msgstr "My name is {name}"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createDefaultViteConfig } from "../default-vite.config"
2+
import { UserConfig } from "vite"
3+
import macrosPlugin from "vite-plugin-babel-macros"
4+
5+
const config: UserConfig = {
6+
...createDefaultViteConfig(__dirname),
7+
}
8+
9+
config.plugins.push(macrosPlugin())
10+
11+
export default config

packages/vite-plugin/test/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ skipOnWindows("vite-plugin", () => {
6464

6565
expect(res.stderr).toContain("Compilation error for 2 translation(s)")
6666
})
67+
68+
it("should report error when @lingui/macro is dynamically imported", async () => {
69+
expect.assertions(1)
70+
try {
71+
await runVite(`dynamic-macro-error/vite.config.ts`)
72+
} catch (e) {
73+
expect(e.stderr).toContain(
74+
'The macro you imported from "@lingui/core/macro" cannot be dynamically imported.'
75+
)
76+
}
77+
})
6778
})
6879

6980
async function runVite(configPath: string) {

packages/vite-plugin/test/macro-usage/.linguirc

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from "@lingui/cli"
2+
3+
export default defineConfig({
4+
locales: ["en"],
5+
catalogs: [
6+
{
7+
path: "<rootDir>/locale/{locale}",
8+
},
9+
],
10+
format: "po",
11+
})

packages/vite-plugin/test/no-macro-error/.linguirc

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from "@lingui/cli"
2+
3+
export default defineConfig({
4+
locales: ["en"],
5+
catalogs: [
6+
{
7+
path: "<rootDir>/locale/{locale}",
8+
},
9+
],
10+
format: "po",
11+
})

0 commit comments

Comments
 (0)