Skip to content

Commit f76a83e

Browse files
authored
Add an option to parse FTL during build (#107)
1 parent b301847 commit f76a83e

File tree

13 files changed

+289
-89
lines changed

13 files changed

+289
-89
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ export default defineConfig({
4444
SFCFluentPlugin({ // define messages in SFCs
4545
blockType: 'fluent', // default 'fluent' - name of the block in SFCs
4646
checkSyntax: true, // default true - whether to check syntax of the messages
47+
parseFtl: false, // default false - whether to parse ftl files during build
4748
}),
4849
ExternalFluentPlugin({ // define messages in external ftl files
4950
baseDir: path.resolve('src'), // required - base directory for Vue files
5051
ftlDir: path.resolve('src/locales'), // required - directory with ftl files
5152
locales: ['en', 'da'], // required - list of locales
5253
checkSyntax: true, // default true - whether to check syntax of the messages
54+
parseFtl: false, // default false - whether to parse ftl files during build
5355
}),
5456
],
5557
})

__tests__/fixtures/errors.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ shared-photos =
1919
[female] her stream
2020
*[other] their stream
2121
}.
22+
23+
entry-without-error = Hello, World!
2224
</fluent>
Binary file not shown.
Binary file not shown.

__tests__/frameworks/vite/external.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ describe('Vite external', () => {
7474
expect(code).toMatchSnapshot()
7575
})
7676

77+
describe('parseFtl', () => {
78+
it('parses ftl syntax during compilation', async () => {
79+
// Arrange
80+
// Act
81+
const code = await compile({
82+
plugins: [
83+
vue3({
84+
compiler,
85+
}),
86+
ExternalFluentPlugin({
87+
baseDir: resolve(baseDir, 'fixtures'),
88+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
89+
locales: ['en', 'da'],
90+
parseFtl: true,
91+
}),
92+
],
93+
}, '/fixtures/components/external.vue')
94+
95+
// Assert
96+
expect(code).toMatchSnapshot()
97+
})
98+
})
99+
77100
it('virtual:ftl-for-file', async () => {
78101
// Arrange
79102
// Act
@@ -106,4 +129,57 @@ describe('Vite external', () => {
106129
"
107130
`)
108131
})
132+
133+
it('can import FTL files', async () => {
134+
// Arrange
135+
// Act
136+
const code = await compile({
137+
plugins: [
138+
vue3({
139+
compiler,
140+
}),
141+
ExternalFluentPlugin({
142+
baseDir: resolve(baseDir, 'fixtures'),
143+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
144+
locales: ['en', 'da'],
145+
}),
146+
],
147+
}, '/fixtures/ftl/en/importer.js.ftl')
148+
149+
// Assert
150+
expect(code).toMatchInlineSnapshot(`
151+
"=== /fixtures/ftl/en/importer.js.ftl ===
152+
153+
import { FluentResource } from "/@id/virtual:empty:fluent-bundle"
154+
155+
export default /*#__PURE__*/ new FluentResource("key = Translations for js file")
156+
"
157+
`)
158+
})
159+
160+
it('can parse FTL files', async () => {
161+
// Arrange
162+
// Act
163+
const code = await compile({
164+
plugins: [
165+
vue3({
166+
compiler,
167+
}),
168+
ExternalFluentPlugin({
169+
baseDir: resolve(baseDir, 'fixtures'),
170+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
171+
locales: ['en', 'da'],
172+
parseFtl: true,
173+
}),
174+
],
175+
}, '/fixtures/ftl/en/importer.js.ftl')
176+
177+
// Assert
178+
expect(code).toMatchInlineSnapshot(`
179+
"=== /fixtures/ftl/en/importer.js.ftl ===
180+
181+
export default {"body":[{"id":"key","value":"Translations for js file","attributes":{}}]}
182+
"
183+
`)
184+
})
109185
})

__tests__/frameworks/vite/sfc.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,46 @@ describe('Vite SFC', () => {
2323
expect(code).toMatchSnapshot()
2424
})
2525

26+
describe('parseFtl', () => {
27+
it('parses ftl syntax during compilation', async () => {
28+
// Arrange
29+
// Act
30+
const code = await compile({
31+
plugins: [
32+
vue3({
33+
compiler,
34+
}),
35+
SFCFluentPlugin({
36+
parseFtl: true,
37+
checkSyntax: false,
38+
}),
39+
],
40+
}, '/fixtures/test.vue')
41+
42+
// Assert
43+
expect(code).toMatchSnapshot()
44+
})
45+
46+
it('generates block code even if it has errors', async () => {
47+
// Arrange
48+
// Act
49+
const code = await compile({
50+
plugins: [
51+
vue3({
52+
compiler,
53+
}),
54+
SFCFluentPlugin({
55+
parseFtl: true,
56+
checkSyntax: false,
57+
}),
58+
],
59+
}, '/fixtures/errors.vue')
60+
61+
// Assert
62+
expect(code).toMatchSnapshot()
63+
})
64+
})
65+
2666
it('supports custom blockType', async () => {
2767
// Arrange
2868
// Act

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
"release": "dotenv release-it"
9696
},
9797
"peerDependencies": {
98+
"@fluent/bundle": "*",
9899
"@nuxt/kit": "^3"
99100
},
100101
"peerDependenciesMeta": {
@@ -111,6 +112,7 @@
111112
},
112113
"devDependencies": {
113114
"@antfu/eslint-config": "^4.3.0",
115+
"@fluent/bundle": "^0.18.0",
114116
"@nuxt/kit": "^3.15.4",
115117
"@nuxt/schema": "^3.15.4",
116118
"@release-it-plugins/lerna-changelog": "7.0.0",

pnpm-lock.yaml

Lines changed: 45 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/loader-query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ export function parseVueRequest(id: string) {
1717
ret.type = params.get('type') as VueQuery['type']
1818

1919
if (params.has('blockType'))
20-
ret.blockType = params.get('blockType')
20+
ret.blockType = params.get('blockType') ?? undefined
2121

2222
if (params.has('index'))
2323
ret.index = Number(params.get('index'))
2424

2525
if (params.has('locale'))
26-
ret.locale = params.get('locale')
26+
ret.locale = params.get('locale') ?? undefined
2727

2828
return {
2929
filename,

0 commit comments

Comments
 (0)