1- import { join , relative } from 'node:path'
1+ import { dirname , join , relative } from 'node:path'
22import { stat as fsStat } from 'node:fs/promises'
33import { createUnplugin } from 'unplugin'
44
@@ -55,6 +55,7 @@ const isFtl = createFilter(['**/*.ftl'])
5555interface Dependency {
5656 locale : string
5757 ftlPath : string
58+ relativeFtlPath : string
5859 importVariable : string
5960}
6061
@@ -80,11 +81,13 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
8081 for ( const locale of options . locales ) {
8182 const ftlPath = normalizePath ( resolvedOptions . getFtlPath ( locale , id ) )
8283 const ftlExists = await fileExists ( ftlPath )
84+ const relativeFtlPath = normalizePath ( relative ( dirname ( id ) , ftlPath ) )
8385
8486 if ( ftlExists ) {
8587 dependencies . push ( {
8688 locale,
8789 ftlPath,
90+ relativeFtlPath,
8891 importVariable : `${ makeLegalIdentifier ( locale ) } _ftl` ,
8992 } )
9093 }
@@ -139,14 +142,16 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
139142 this . addWatchFile ( ftlPath )
140143
141144 for ( const dep of translations )
142- magic . prepend ( `import ${ dep . importVariable } from '${ dep . ftlPath } ';\n` )
145+ magic . prepend ( `import ${ dep . importVariable } from '${ dep . relativeFtlPath } ';\n` )
143146 magic . appendLeft ( insertPos , `${ target } .fluent = ${ target } .fluent || {};\n` )
144147 for ( const dep of translations )
145148 magic . appendLeft ( insertPos , `${ target } .fluent['${ dep . locale } '] = ${ dep . importVariable } \n` )
149+
150+ const __HOT_API__ = meta . framework === 'webpack' ? 'import.meta.webpackHot' : 'import.meta.hot'
151+
146152 magic . appendLeft ( insertPos , `
147- const __HOT_API__ = import.meta.hot || import.meta.webpackHot
148- if (__HOT_API__) {
149- __HOT_API__.accept([${ translations . map ( dep => `'${ dep . ftlPath } '` ) . join ( ', ' ) } ], () => {
153+ if (${ __HOT_API__ } ) {
154+ ${ __HOT_API__ } .accept([${ translations . map ( dep => `'${ dep . relativeFtlPath } '` ) . join ( ', ' ) } ], () => {
150155 ${ translations . map ( ( { locale, importVariable } ) => `${ target } .fluent['${ locale } '] = ${ importVariable } ` ) . join ( '\n' ) }
151156
152157 delete ${ target } ._fluent
0 commit comments