diff --git a/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap b/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap index a7c4cd2..1432cac 100644 Binary files a/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap and b/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap differ diff --git a/__tests__/frameworks/vite/__snapshots__/sfc.spec.ts.snap b/__tests__/frameworks/vite/__snapshots__/sfc.spec.ts.snap index 146341d..54c7a90 100644 Binary files a/__tests__/frameworks/vite/__snapshots__/sfc.spec.ts.snap and b/__tests__/frameworks/vite/__snapshots__/sfc.spec.ts.snap differ diff --git a/__tests__/frameworks/vite/util.ts b/__tests__/frameworks/vite/util.ts index 508a5f6..21bf4ed 100644 --- a/__tests__/frameworks/vite/util.ts +++ b/__tests__/frameworks/vite/util.ts @@ -55,6 +55,7 @@ export async function compile(options: InlineConfig, file: string): Promise module.transform) .filter(module => !module.module.url.includes('node_modules')) + .filter(module => !module.module.url.includes('virtual:empty:')) .map(module => `=== ${module.module.url} ===\n${module.transform.code}`).join('\n\n') // normalize paths diff --git a/src/plugins/external-plugin.ts b/src/plugins/external-plugin.ts index 224231f..4c2cf5a 100644 --- a/src/plugins/external-plugin.ts +++ b/src/plugins/external-plugin.ts @@ -1,4 +1,4 @@ -import { join, relative } from 'node:path' +import { dirname, join, relative } from 'node:path' import { stat as fsStat } from 'node:fs/promises' import { createUnplugin } from 'unplugin' @@ -55,6 +55,7 @@ const isFtl = createFilter(['**/*.ftl']) interface Dependency { locale: string ftlPath: string + relativeFtlPath: string importVariable: string } @@ -80,11 +81,13 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => for (const locale of options.locales) { const ftlPath = normalizePath(resolvedOptions.getFtlPath(locale, id)) const ftlExists = await fileExists(ftlPath) + const relativeFtlPath = normalizePath(relative(dirname(id), ftlPath)) if (ftlExists) { dependencies.push({ locale, ftlPath, + relativeFtlPath, importVariable: `${makeLegalIdentifier(locale)}_ftl`, }) } @@ -139,14 +142,16 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => this.addWatchFile(ftlPath) for (const dep of translations) - magic.prepend(`import ${dep.importVariable} from '${dep.ftlPath}';\n`) + magic.prepend(`import ${dep.importVariable} from '${dep.relativeFtlPath}';\n`) magic.appendLeft(insertPos, `${target}.fluent = ${target}.fluent || {};\n`) for (const dep of translations) magic.appendLeft(insertPos, `${target}.fluent['${dep.locale}'] = ${dep.importVariable}\n`) + + const __HOT_API__ = meta.framework === 'webpack' ? 'import.meta.webpackHot' : 'import.meta.hot' + magic.appendLeft(insertPos, ` -const __HOT_API__ = import.meta.hot || import.meta.webpackHot -if (__HOT_API__) { - __HOT_API__.accept([${translations.map(dep => `'${dep.ftlPath}'`).join(', ')}], () => { +if (${__HOT_API__}) { + ${__HOT_API__}.accept([${translations.map(dep => `'${dep.relativeFtlPath}'`).join(', ')}], () => { ${translations.map(({ locale, importVariable }) => `${target}.fluent['${locale}'] = ${importVariable}`).join('\n')} delete ${target}._fluent