Skip to content

Add getFtlPath option to better control path to ftl files #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __tests__/frameworks/vite/__snapshots__/external.spec.ts.snap
Binary file not shown.
24 changes: 23 additions & 1 deletion __tests__/frameworks/vite/external.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { relative, resolve } from 'path'
import { describe, expect, it } from 'vitest'

import vue3base from '@vitejs/plugin-vue'
Expand Down Expand Up @@ -29,6 +29,27 @@ describe('Vite external', () => {
}, '/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()
})

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

// Assert
expect(code).toContain('external.setup.vue.ftl')
expect(code).toMatchSnapshot()
})
})
19 changes: 15 additions & 4 deletions src/plugins/external-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) {
Expand Down
15 changes: 12 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down