1
- import { join , relative } from 'node:path'
2
1
import { stat as fsStat } from 'node:fs/promises'
2
+ import { dirname , join , relative } from 'pathe'
3
3
import { createUnplugin } from 'unplugin'
4
4
5
5
import MagicString from 'magic-string'
@@ -45,16 +45,13 @@ async function fileExists(filename: string): Promise<boolean> {
45
45
}
46
46
}
47
47
48
- function normalizePath ( path : string ) {
49
- return path . replace ( / \\ / g, '/' )
50
- }
51
-
52
48
const isVue = createFilter ( [ '**/*.vue' ] )
53
49
const isFtl = createFilter ( [ '**/*.ftl' ] )
54
50
55
51
interface Dependency {
56
52
locale : string
57
53
ftlPath : string
54
+ relativeFtlPath : string
58
55
importVariable : string
59
56
}
60
57
@@ -78,13 +75,15 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
78
75
const getTranslationsForFile = async ( id : string ) => {
79
76
const dependencies : Dependency [ ] = [ ]
80
77
for ( const locale of options . locales ) {
81
- const ftlPath = normalizePath ( resolvedOptions . getFtlPath ( locale , id ) )
78
+ const ftlPath = resolvedOptions . getFtlPath ( locale , id )
82
79
const ftlExists = await fileExists ( ftlPath )
80
+ const relativeFtlPath = relative ( dirname ( id ) , ftlPath )
83
81
84
82
if ( ftlExists ) {
85
83
dependencies . push ( {
86
84
locale,
87
85
ftlPath,
86
+ relativeFtlPath,
88
87
importVariable : `${ makeLegalIdentifier ( locale ) } _ftl` ,
89
88
} )
90
89
}
@@ -139,7 +138,7 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
139
138
this . addWatchFile ( ftlPath )
140
139
141
140
for ( const dep of translations )
142
- magic . prepend ( `import ${ dep . importVariable } from '${ dep . ftlPath } ';\n` )
141
+ magic . prepend ( `import ${ dep . importVariable } from '${ dep . relativeFtlPath } ';\n` )
143
142
magic . appendLeft ( insertPos , `${ target } .fluent = ${ target } .fluent || {};\n` )
144
143
for ( const dep of translations )
145
144
magic . appendLeft ( insertPos , `${ target } .fluent['${ dep . locale } '] = ${ dep . importVariable } \n` )
@@ -148,7 +147,7 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
148
147
149
148
magic . appendLeft ( insertPos , `
150
149
if (${ __HOT_API__ } ) {
151
- ${ __HOT_API__ } .accept([${ translations . map ( dep => `'${ dep . ftlPath } '` ) . join ( ', ' ) } ], () => {
150
+ ${ __HOT_API__ } .accept([${ translations . map ( dep => `'${ dep . relativeFtlPath } '` ) . join ( ', ' ) } ], () => {
152
151
${ translations . map ( ( { locale, importVariable } ) => `${ target } .fluent['${ locale } '] = ${ importVariable } ` ) . join ( '\n' ) }
153
152
154
153
delete ${ target } ._fluent
0 commit comments