@@ -76,12 +76,46 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
76
76
}
77
77
}
78
78
79
+ const insertFtlImports = ( magic : MagicString , translations : Dependency [ ] ) => {
80
+ for ( const dep of translations )
81
+ magic . prepend ( `import ${ dep . importVariable } from '${ dep . relativeFtlPath } ';\n` )
82
+ }
83
+
84
+ const insertHotCode = ( magic : MagicString , translations : Dependency [ ] , target : string , insertPos : number ) => {
85
+ const __HOT_API__ = meta . framework === 'webpack' ? 'import.meta.webpackHot' : 'import.meta.hot'
86
+
87
+ magic . appendLeft ( insertPos , `
88
+ if (${ __HOT_API__ } ) {
89
+ ${ __HOT_API__ } .accept([${ translations . map ( dep => `'${ dep . relativeFtlPath } '` ) . join ( ', ' ) } ], (mods) => {
90
+ ${ translations . map ( ( { locale, importVariable } ) => `${ target } .fluent['${ locale } '] = ${ importVariable } ` ) . join ( '\n' ) }
91
+
92
+ if (mods) {
93
+ ${ translations . map ( ( { locale } , index ) => `if (mods['${ index } ']) ${ target } .fluent['${ locale } '] = mods['${ index } '].default` ) . join ( '\n' ) }
94
+ }
95
+
96
+ delete ${ target } ._fluent
97
+ if (typeof __VUE_HMR_RUNTIME__ !== 'undefined') {
98
+ // Vue 3
99
+ __VUE_HMR_RUNTIME__.reload(${ target } .__hmrId, ${ target } )
100
+ } else {
101
+ // Vue 2
102
+ // There is no proper api to access HMR for component from custom block
103
+ // so use this magic
104
+ delete ${ target } ._Ctor
105
+ }
106
+ })
107
+ }
108
+ ` )
109
+ }
110
+
79
111
const getTranslationsForFile = async ( id : string ) => {
80
112
const dependencies : Dependency [ ] = [ ]
81
113
for ( const locale of options . locales ) {
82
114
const ftlPath = normalizePath ( resolvedOptions . getFtlPath ( locale , id ) )
83
115
const ftlExists = await fileExists ( ftlPath )
84
- const relativeFtlPath = normalizePath ( relative ( dirname ( id ) , ftlPath ) )
116
+ let relativeFtlPath = normalizePath ( relative ( dirname ( id ) , ftlPath ) )
117
+ if ( ! relativeFtlPath . startsWith ( '.' ) )
118
+ relativeFtlPath = `./${ relativeFtlPath } `
85
119
86
120
if ( ftlExists ) {
87
121
dependencies . push ( {
@@ -141,32 +175,13 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
141
175
for ( const { ftlPath } of translations )
142
176
this . addWatchFile ( ftlPath )
143
177
144
- for ( const dep of translations )
145
- magic . prepend ( `import ${ dep . importVariable } from ' ${ dep . relativeFtlPath } ';\n` )
178
+ insertFtlImports ( magic , translations )
179
+
146
180
magic . appendLeft ( insertPos , `${ target } .fluent = ${ target } .fluent || {};\n` )
147
181
for ( const dep of translations )
148
182
magic . appendLeft ( insertPos , `${ target } .fluent['${ dep . locale } '] = ${ dep . importVariable } \n` )
149
183
150
- const __HOT_API__ = meta . framework === 'webpack' ? 'import.meta.webpackHot' : 'import.meta.hot'
151
-
152
- magic . appendLeft ( insertPos , `
153
- if (${ __HOT_API__ } ) {
154
- ${ __HOT_API__ } .accept([${ translations . map ( dep => `'${ dep . relativeFtlPath } '` ) . join ( ', ' ) } ], () => {
155
- ${ translations . map ( ( { locale, importVariable } ) => `${ target } .fluent['${ locale } '] = ${ importVariable } ` ) . join ( '\n' ) }
156
-
157
- delete ${ target } ._fluent
158
- if (typeof __VUE_HMR_RUNTIME__ !== 'undefined') {
159
- // Vue 3
160
- __VUE_HMR_RUNTIME__.reload(${ target } .__hmrId, ${ target } )
161
- } else {
162
- // Vue 2
163
- // There is no proper api to access HMR for component from custom block
164
- // so use this magic
165
- delete ${ target } ._Ctor
166
- }
167
- })
168
- }
169
- ` )
184
+ insertHotCode ( magic , translations , target , insertPos )
170
185
171
186
return {
172
187
code : magic . toString ( ) ,
0 commit comments