Skip to content

Commit 224bc9f

Browse files
authored
Add getFtlPath option to better control path to ftl files (#26)
1 parent 4a40c7c commit 224bc9f

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed
3.16 KB
Binary file not shown.

__tests__/frameworks/vite/external.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolve } from 'path'
1+
import { relative, resolve } from 'path'
22
import { describe, expect, it } from 'vitest'
33

44
import vue3base from '@vitejs/plugin-vue'
@@ -29,6 +29,27 @@ describe('Vite external', () => {
2929
}, '/fixtures/components/external.vue')
3030

3131
// Assert
32+
expect(code).toContain('external.vue.ftl')
33+
expect(code).toMatchSnapshot()
34+
})
35+
36+
it('getFtlPath', async () => {
37+
// Arrange
38+
// Act
39+
const code = await compile({
40+
plugins: [
41+
vue3(),
42+
ExternalFluentPlugin({
43+
getFtlPath: (locale, vuePath) => {
44+
return `${baseDir}/fixtures/ftl/${locale}/${relative(resolve(baseDir, 'fixtures'), vuePath)}.ftl`
45+
},
46+
locales: ['en', 'da'],
47+
}),
48+
],
49+
}, '/fixtures/components/external.vue')
50+
51+
// Assert
52+
expect(code).toContain('external.vue.ftl')
3253
expect(code).toMatchSnapshot()
3354
})
3455

@@ -47,6 +68,7 @@ describe('Vite external', () => {
4768
}, '/fixtures/components/external.setup.vue')
4869

4970
// Assert
71+
expect(code).toContain('external.setup.vue.ftl')
5072
expect(code).toMatchSnapshot()
5173
})
5274
})

src/plugins/external-plugin.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,20 @@ interface Dependency {
5959
}
6060

6161
export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => {
62-
options.checkSyntax = options.checkSyntax ?? false
62+
const resolvedOptions = {
63+
checkSyntax: true,
64+
getFtlPath: undefined as ((locale: string, vuePath: string) => string) | undefined,
65+
...options,
66+
}
67+
68+
if ('getFtlPath' in options) {
69+
resolvedOptions.getFtlPath = options.getFtlPath
70+
}
71+
else {
72+
resolvedOptions.getFtlPath = (locale: string, vuePath: string) => {
73+
return join(options.ftlDir, locale, `${relative(options.baseDir, vuePath)}.ftl`)
74+
}
75+
}
6376

6477
return {
6578
name: 'unplugin-fluent-vue-external',
@@ -73,11 +86,9 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
7386

7487
const { insertPos, target } = getInsertInfo(source)
7588

76-
const relativePath = relative(options.baseDir, id)
77-
7889
const dependencies: Dependency[] = []
7990
for (const locale of options.locales) {
80-
const ftlPath = normalizePath(join(options.ftlDir, locale, `${relativePath}.ftl`))
91+
const ftlPath = normalizePath(resolvedOptions.getFtlPath(locale, id))
8192
const ftlExists = await fileExists(ftlPath)
8293

8394
if (ftlExists) {

src/types.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
export interface ExternalPluginOptions {
2-
baseDir: string
3-
ftlDir: string
1+
interface ExternalPluginOptionsBase {
42
locales: string[]
53
checkSyntax?: boolean
64
}
75

6+
interface ExternalPluginOptionsFolder extends ExternalPluginOptionsBase {
7+
baseDir: string
8+
ftlDir: string
9+
}
10+
11+
interface ExternalPluginOptionsFunction extends ExternalPluginOptionsBase {
12+
getFtlPath: (locale: string, vuePath: string) => string
13+
}
14+
15+
export type ExternalPluginOptions = ExternalPluginOptionsFolder | ExternalPluginOptionsFunction
16+
817
export interface SFCPluginOptions {
918
blockType?: string
1019
checkSyntax?: boolean

0 commit comments

Comments
 (0)