1
- import { join , relative } from 'node:path'
1
+ import { dirname , join , relative } from 'node:path'
2
2
import { stat as fsStat } from 'node:fs/promises'
3
3
import { createUnplugin } from 'unplugin'
4
4
@@ -55,6 +55,7 @@ const isFtl = createFilter(['**/*.ftl'])
55
55
interface Dependency {
56
56
locale : string
57
57
ftlPath : string
58
+ relativeFtlPath : string
58
59
importVariable : string
59
60
}
60
61
@@ -80,11 +81,13 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
80
81
for ( const locale of options . locales ) {
81
82
const ftlPath = normalizePath ( resolvedOptions . getFtlPath ( locale , id ) )
82
83
const ftlExists = await fileExists ( ftlPath )
84
+ const relativeFtlPath = normalizePath ( relative ( dirname ( id ) , ftlPath ) )
83
85
84
86
if ( ftlExists ) {
85
87
dependencies . push ( {
86
88
locale,
87
89
ftlPath,
90
+ relativeFtlPath,
88
91
importVariable : `${ makeLegalIdentifier ( locale ) } _ftl` ,
89
92
} )
90
93
}
@@ -139,14 +142,16 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
139
142
this . addWatchFile ( ftlPath )
140
143
141
144
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` )
143
146
magic . appendLeft ( insertPos , `${ target } .fluent = ${ target } .fluent || {};\n` )
144
147
for ( const dep of translations )
145
148
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
+
146
152
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 ( ', ' ) } ], () => {
150
155
${ translations . map ( ( { locale, importVariable } ) => `${ target } .fluent['${ locale } '] = ${ importVariable } ` ) . join ( '\n' ) }
151
156
152
157
delete ${ target } ._fluent
0 commit comments