Skip to content

Commit 3e466ed

Browse files
committed
Parse FTL in ExternalPlugin
1 parent 84734ac commit 3e466ed

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed
Binary file not shown.

__tests__/frameworks/vite/external.spec.ts

Lines changed: 23 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

src/plugins/external-plugin.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions) => {
132132
}
133133

134134
if (isFtl(id)) {
135-
if (options.checkSyntax) {
135+
if (resolvedOptions.checkSyntax) {
136136
const errorsText = getSyntaxErrors(source)
137137
if (errorsText)
138138
this.error(errorsText)
@@ -158,19 +158,15 @@ export default /*#__PURE__*/ new FluentResource(${source})
158158
this.error(errorsText)
159159
}
160160

161-
const magic = new MagicString(source, { filename: id })
162-
if (source.length > 0)
163-
magic.update(0, source.length, JSON.stringify(source))
164-
else
165-
magic.append('""')
166-
magic.prepend(`
167-
import { FluentResource } from '@fluent/bundle'
161+
const injectFtl = getInjectFtl(resolvedOptions)
168162

163+
const magic = injectFtl`
169164
export default function (Component) {
170165
const target = Component.options || Component
171166
target.fluent = target.fluent || {}
172-
target.fluent['${query.locale}'] = new FluentResource(`)
173-
magic.append(')\n}')
167+
target.fluent['${query.locale}'] = ${source}
168+
}
169+
`
174170

175171
return {
176172
code: magic.toString(),

src/plugins/ftl/inject.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { SFCPluginOptions } from 'src/types'
2-
31
import { FluentResource } from '@fluent/bundle'
42
import MagicString from 'magic-string'
53

@@ -9,7 +7,7 @@ function normalize(str: string) {
97
return str.replace(/\r\n/g, '\n').trim()
108
}
119

12-
export function getInjectFtl(options: SFCPluginOptions): InjectFtlFn {
10+
export function getInjectFtl(options: { parseFtl?: boolean }): InjectFtlFn {
1311
return (template, locale, source) => {
1412
if (source == null) {
1513
source = locale

src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ export interface ExternalPluginOptionsFunction extends ExternalPluginOptionsBase
1313
getFtlPath: (locale: string, vuePath: string) => string
1414
}
1515

16-
export type ExternalPluginOptions = ExternalPluginOptionsFolder | ExternalPluginOptionsFunction
16+
export type ExternalPluginOptions = (ExternalPluginOptionsFolder | ExternalPluginOptionsFunction) & {
17+
/**
18+
* Whether to parse the ftl syntax before injecting it into component
19+
*/
20+
parseFtl?: boolean
21+
}
1722

1823
export interface SFCPluginOptions {
1924
/**

0 commit comments

Comments
 (0)