From fe07dbb98e22deb6bdc13d1f4ad053f2b62bfb2d Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Sat, 1 Oct 2022 09:37:55 +0300 Subject: [PATCH] Add getFtlPath option to better control path to ftl files --- .../vite/__snapshots__/external.spec.ts.snap | Bin 6470 -> 9710 bytes __tests__/frameworks/vite/external.spec.ts | 24 +++++++++++++++++- src/plugins/external-plugin.ts | 19 +++++++++++--- src/types.ts | 15 ++++++++--- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap b/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap index abedccf38a7df54db630723b681741a3c3f9d124..320f783216b7acca39de7b69d69d849bb93cf0f4 100644 GIT binary patch delta 29 lcmX?R^v-*N0atoziCaldKw?S8 { }, '/fixtures/components/external.vue') // Assert + expect(code).toContain('external.vue.ftl') + expect(code).toMatchSnapshot() + }) + + it('getFtlPath', async () => { + // Arrange + // Act + const code = await compile({ + plugins: [ + vue3(), + ExternalFluentPlugin({ + getFtlPath: (locale, vuePath) => { + return `${baseDir}/fixtures/ftl/${locale}/${relative(resolve(baseDir, 'fixtures'), vuePath)}.ftl` + }, + locales: ['en', 'da'], + }), + ], + }, '/fixtures/components/external.vue') + + // Assert + expect(code).toContain('external.vue.ftl') expect(code).toMatchSnapshot() }) @@ -47,6 +68,7 @@ describe('Vite external', () => { }, '/fixtures/components/external.setup.vue') // Assert + expect(code).toContain('external.setup.vue.ftl') expect(code).toMatchSnapshot() }) }) diff --git a/src/plugins/external-plugin.ts b/src/plugins/external-plugin.ts index 44422e4..8ef597d 100644 --- a/src/plugins/external-plugin.ts +++ b/src/plugins/external-plugin.ts @@ -59,7 +59,20 @@ interface Dependency { } export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => { - options.checkSyntax = options.checkSyntax ?? false + const resolvedOptions = { + checkSyntax: true, + getFtlPath: undefined as ((locale: string, vuePath: string) => string) | undefined, + ...options, + } + + if ('getFtlPath' in options) { + resolvedOptions.getFtlPath = options.getFtlPath + } + else { + resolvedOptions.getFtlPath = (locale: string, vuePath: string) => { + return join(options.ftlDir, locale, `${relative(options.baseDir, vuePath)}.ftl`) + } + } return { name: 'unplugin-fluent-vue-external', @@ -73,11 +86,9 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => const { insertPos, target } = getInsertInfo(source) - const relativePath = relative(options.baseDir, id) - const dependencies: Dependency[] = [] for (const locale of options.locales) { - const ftlPath = normalizePath(join(options.ftlDir, locale, `${relativePath}.ftl`)) + const ftlPath = normalizePath(resolvedOptions.getFtlPath(locale, id)) const ftlExists = await fileExists(ftlPath) if (ftlExists) { diff --git a/src/types.ts b/src/types.ts index 64091f3..83ec1bb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,10 +1,19 @@ -export interface ExternalPluginOptions { - baseDir: string - ftlDir: string +interface ExternalPluginOptionsBase { locales: string[] checkSyntax?: boolean } +interface ExternalPluginOptionsFolder extends ExternalPluginOptionsBase { + baseDir: string + ftlDir: string +} + +interface ExternalPluginOptionsFunction extends ExternalPluginOptionsBase { + getFtlPath: (locale: string, vuePath: string) => string +} + +export type ExternalPluginOptions = ExternalPluginOptionsFolder | ExternalPluginOptionsFunction + export interface SFCPluginOptions { blockType?: string checkSyntax?: boolean