Skip to content

Commit fc9ed8d

Browse files
committed
Correctly handle importing external FTL files
1 parent dd77a09 commit fc9ed8d

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

__tests__/frameworks/vite/external.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,57 @@ describe('Vite external', () => {
129129
"
130130
`)
131131
})
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 /*#__PURE__*/ {"body":[{"id":"key","value":"Translations for js file","attributes":{}}]}
182+
"
183+
`)
184+
})
132185
})

src/plugins/external-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions) => {
134134
if (isFtl(id)) {
135135
const injectFtl = getInjectFtl(resolvedOptions)
136136
const result = injectFtl`
137-
export default /*#__PURE__*/ new FluentResource(${source})
137+
export default /*#__PURE__*/ ${source}
138138
`
139139

140140
if (result.error)

src/plugins/ftl/inject.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function getInjectFtl(options: { checkSyntax: boolean, parseFtl: boolean
1919
throw new Error('Missing source')
2020

2121
if (options.checkSyntax) {
22-
const errorsText = getSyntaxErrors(source.replace(/\r\n/g, '\n').trim())
22+
const errorsText = getSyntaxErrors(normalize(source))
2323

2424
if (errorsText) {
2525
return {
@@ -30,7 +30,6 @@ export function getInjectFtl(options: { checkSyntax: boolean, parseFtl: boolean
3030

3131
const magic = new MagicString(source)
3232
const importString = options.parseFtl === true ? '' : '\nimport { FluentResource } from \'@fluent/bundle\'\n'
33-
const localeString = locale == null ? '' : locale
3433

3534
if (source.length === 0) {
3635
magic.append('undefined')
@@ -43,9 +42,14 @@ export function getInjectFtl(options: { checkSyntax: boolean, parseFtl: boolean
4342
magic.overwrite(0, source.length, `new FluentResource(${JSON.stringify(normalize(source))})`)
4443
}
4544

46-
magic.prepend(importString + template[0] + localeString + template[1])
47-
if (template[2] != null)
45+
if (template.length === 2) {
46+
magic.prepend(importString + template[0])
47+
magic.append(template[1])
48+
}
49+
else if (template.length === 3) {
50+
magic.prepend(importString + template[0] + locale + template[1])
4851
magic.append(template[2])
52+
}
4953

5054
return {
5155
code: {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy