Skip to content

Commit ce660d7

Browse files
fix(webpack + vite): fix dependency watching in loader (#1662)
* fix(webpack + vite): fix dependency watching in loader * refactor(cli): add `pathe` to avoid inconsistence on win/posix
1 parent dad2c06 commit ce660d7

File tree

10 files changed

+117
-13
lines changed

10 files changed

+117
-13
lines changed

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"micromatch": "4.0.2",
7272
"normalize-path": "^3.0.0",
7373
"ora": "^5.1.0",
74+
"pathe": "^1.1.0",
7475
"pkg-up": "^3.1.0",
7576
"pofile": "^1.1.4",
7677
"pseudolocale": "^2.0.0",

packages/cli/src/api/catalog/getCatalogDependentFiles.test.ts

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@ import { getCatalogDependentFiles, getFormat } from "@lingui/cli/api"
22
import { makeConfig } from "@lingui/conf"
33
import { Catalog } from "../catalog"
44
import { FormatterWrapper } from "../formats"
5+
import mockFs from "mock-fs"
56

67
describe("getCatalogDependentFiles", () => {
78
let format: FormatterWrapper
89

910
beforeAll(async () => {
1011
format = await getFormat("po", {}, "en")
1112
})
13+
afterEach(() => {
14+
mockFs.restore()
15+
})
16+
17+
it("Should return list template + fallbacks + sourceLocale", async () => {
18+
mockFs({
19+
"src/locales": {
20+
"messages.pot": "bla",
21+
"en.po": "bla",
22+
"pl.po": "bla",
23+
"es.po": "bla",
24+
"pt-PT.po": "bla",
25+
"pt-BR.po": "bla",
26+
},
27+
})
1228

13-
it("Should return list template + fallbacks + sourceLocale", () => {
1429
const config = makeConfig(
1530
{
1631
locales: ["en", "pl", "es", "pt-PT", "pt-BR"],
@@ -34,7 +49,10 @@ describe("getCatalogDependentFiles", () => {
3449
config
3550
)
3651

37-
expect(getCatalogDependentFiles(catalog, "pt-PT")).toMatchInlineSnapshot(`
52+
const actual = await getCatalogDependentFiles(catalog, "pt-PT")
53+
mockFs.restore()
54+
55+
expect(actual).toMatchInlineSnapshot(`
3856
[
3957
src/locales/messages.pot,
4058
src/locales/pt-BR.po,
@@ -43,7 +61,18 @@ describe("getCatalogDependentFiles", () => {
4361
`)
4462
})
4563

46-
it("Should not return itself", () => {
64+
it("Should not return itself", async () => {
65+
mockFs({
66+
"src/locales": {
67+
"messages.pot": "bla",
68+
"en.po": "bla",
69+
"pl.po": "bla",
70+
"es.po": "bla",
71+
"pt-PT.po": "bla",
72+
"pt-BR.po": "bla",
73+
},
74+
})
75+
4776
const config = makeConfig(
4877
{
4978
locales: ["en", "pl", "es", "pt-PT", "pt-BR"],
@@ -67,10 +96,59 @@ describe("getCatalogDependentFiles", () => {
6796
config
6897
)
6998

70-
expect(getCatalogDependentFiles(catalog, "en")).toMatchInlineSnapshot(`
99+
const actual = await getCatalogDependentFiles(catalog, "en")
100+
mockFs.restore()
101+
102+
expect(actual).toMatchInlineSnapshot(`
71103
[
72104
src/locales/messages.pot,
73105
]
74106
`)
75107
})
108+
109+
it("Should not return non-existing files", async () => {
110+
mockFs({
111+
"src/locales": {
112+
// "messages.pot": "bla",
113+
"en.po": "bla",
114+
"pl.po": "bla",
115+
"es.po": "bla",
116+
"pt-PT.po": "bla",
117+
"pt-BR.po": "bla",
118+
},
119+
})
120+
121+
const config = makeConfig(
122+
{
123+
locales: ["en", "pl", "es", "pt-PT", "pt-BR"],
124+
sourceLocale: "en",
125+
fallbackLocales: {
126+
"pt-PT": "pt-BR",
127+
default: "en",
128+
},
129+
},
130+
{ skipValidation: true }
131+
)
132+
133+
const catalog = new Catalog(
134+
{
135+
name: null,
136+
path: "src/locales/{locale}",
137+
include: ["src/"],
138+
exclude: [],
139+
format,
140+
},
141+
config
142+
)
143+
144+
const actual = await getCatalogDependentFiles(catalog, "pt-PT")
145+
mockFs.restore()
146+
147+
expect(actual).toMatchInlineSnapshot(`
148+
[
149+
src/locales/pt-BR.po,
150+
src/locales/en.po,
151+
]
152+
`)
153+
})
76154
})

packages/cli/src/api/catalog/getCatalogDependentFiles.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { Catalog } from "../catalog"
22
import { getFallbackListForLocale } from "./getFallbackListForLocale"
3+
import path from "pathe"
4+
import fs from "node:fs/promises"
5+
6+
const fileExists = async (path: string) =>
7+
!!(await fs.stat(path).catch(() => false))
38

49
/**
510
* Return all files catalog implicitly depends on.
611
*/
7-
export function getCatalogDependentFiles(
12+
export async function getCatalogDependentFiles(
813
catalog: Catalog,
914
locale: string
10-
): string[] {
15+
): Promise<string[]> {
1116
const files = new Set([catalog.templateFile])
1217
getFallbackListForLocale(catalog.config.fallbackLocales, locale).forEach(
1318
(locale) => {
@@ -19,5 +24,14 @@ export function getCatalogDependentFiles(
1924
files.add(catalog.getFilename(catalog.config.sourceLocale))
2025
}
2126

22-
return Array.from(files.values())
27+
const out: string[] = []
28+
29+
for (const file of files) {
30+
const filePath = path.join(catalog.config.rootDir, file)
31+
if (await fileExists(filePath)) {
32+
out.push(filePath)
33+
}
34+
}
35+
36+
return out
2337
}

packages/loader/src/webpackLoader.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ const loader: LoaderDefinitionFunction<LinguiLoaderOptions> = async function (
2929
await getCatalogs(config)
3030
)
3131

32-
getCatalogDependentFiles(catalog, locale).forEach((locale) => {
33-
this.addDependency(catalog.getFilename(locale))
34-
})
32+
const dependency = await getCatalogDependentFiles(catalog, locale)
33+
dependency.forEach((file) => this.addDependency(file))
3534

3635
const messages = await catalog.getTranslations(locale, {
3736
fallbackLocales: config.fallbackLocales,

packages/loader/test/json-format/lingui.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ export default {
77
path: "<rootDir>/locale/{locale}",
88
},
99
],
10+
fallbackLocales: {
11+
default: "en",
12+
},
1013
format: formatter({ style: "minimal" }),
1114
}

packages/loader/test/po-format/.linguirc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"catalogs": [{
44
"path": "<rootDir>/locale/{locale}"
55
}],
6+
"fallbackLocales": {
7+
"default": "en"
8+
},
69
"format": "po"
710
}

packages/vite-plugin/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ Please check that catalogs.path is filled properly.\n`
5353

5454
const { locale, catalog } = fileCatalog
5555

56-
getCatalogDependentFiles(catalog, locale).forEach((locale) => {
57-
this.addWatchFile(catalog.getFilename(locale))
58-
})
56+
const dependency = await getCatalogDependentFiles(catalog, locale)
57+
dependency.forEach((file) => this.addWatchFile(file))
5958

6059
const messages = await catalog.getTranslations(locale, {
6160
fallbackLocales: config.fallbackLocales,

packages/vite-plugin/test/json-format/lingui.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ export default {
77
path: "<rootDir>/locale/{locale}",
88
},
99
],
10+
fallbackLocales: {
11+
default: "en",
12+
},
1013
format: formatter({ style: "minimal" }),
1114
}

packages/vite-plugin/test/po-format/.linguirc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"catalogs": [{
44
"path": "<rootDir>/locale/{locale}"
55
}],
6+
"fallbackLocales": {
7+
"default": "en"
8+
},
69
"format": "po"
710
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,6 +2694,7 @@ __metadata:
26942694
mockdate: ^3.0.5
26952695
normalize-path: ^3.0.0
26962696
ora: ^5.1.0
2697+
pathe: ^1.1.0
26972698
pkg-up: ^3.1.0
26982699
pofile: ^1.1.4
26992700
pseudolocale: ^2.0.0

0 commit comments

Comments
 (0)