@@ -61,6 +61,7 @@ interface Dependency {
61
61
export const unplugin = createUnplugin ( ( options : ExternalPluginOptions , meta ) => {
62
62
const resolvedOptions = {
63
63
checkSyntax : true ,
64
+ virtualModuleName : 'virtual:ftl-for-file' ,
64
65
getFtlPath : undefined as ( ( locale : string , vuePath : string ) => string ) | undefined ,
65
66
...options ,
66
67
}
@@ -74,9 +75,52 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
74
75
}
75
76
}
76
77
78
+ const getTranslationsForFile = async ( id : string ) => {
79
+ const dependencies : Dependency [ ] = [ ]
80
+ for ( const locale of options . locales ) {
81
+ const ftlPath = normalizePath ( resolvedOptions . getFtlPath ( locale , id ) )
82
+ const ftlExists = await fileExists ( ftlPath )
83
+
84
+ if ( ftlExists ) {
85
+ dependencies . push ( {
86
+ locale,
87
+ ftlPath,
88
+ importVariable : `${ makeLegalIdentifier ( locale ) } _ftl` ,
89
+ } )
90
+ }
91
+ }
92
+
93
+ return dependencies
94
+ }
95
+
77
96
return {
78
97
name : 'unplugin-fluent-vue-external' ,
79
98
enforce : meta . framework === 'webpack' ? 'post' : undefined ,
99
+ resolveId ( id , importer ) {
100
+ if ( id === resolvedOptions . virtualModuleName )
101
+ return `${ id } ?importer=${ importer } `
102
+ } ,
103
+ async load ( id ) {
104
+ if ( ! id . startsWith ( resolvedOptions . virtualModuleName ) )
105
+ return
106
+
107
+ const importer = id . split ( '?importer=' ) [ 1 ]
108
+
109
+ const translations = await getTranslationsForFile ( importer )
110
+
111
+ for ( const { ftlPath } of translations )
112
+ this . addWatchFile ( ftlPath )
113
+
114
+ let code = ''
115
+ for ( const { ftlPath, importVariable } of translations )
116
+ code += `import ${ importVariable } from '${ ftlPath } ';\n`
117
+
118
+ code += `export default { ${ translations
119
+ . map ( ( { locale, importVariable } ) => `'${ locale } ': ${ importVariable } ` )
120
+ . join ( ', ' ) } }\n`
121
+
122
+ return code
123
+ } ,
80
124
transformInclude ( id : string ) {
81
125
return isVue ( id ) || isFtl ( id )
82
126
} ,
@@ -86,32 +130,21 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
86
130
87
131
const { insertPos, target } = getInsertInfo ( source )
88
132
89
- const dependencies : Dependency [ ] = [ ]
90
- for ( const locale of options . locales ) {
91
- const ftlPath = normalizePath ( resolvedOptions . getFtlPath ( locale , id ) )
92
- const ftlExists = await fileExists ( ftlPath )
93
-
94
- if ( ftlExists ) {
95
- this . addWatchFile ( ftlPath )
133
+ const translations = await getTranslationsForFile ( id )
96
134
97
- dependencies . push ( {
98
- locale,
99
- ftlPath,
100
- importVariable : `${ makeLegalIdentifier ( locale ) } _ftl` ,
101
- } )
102
- }
103
- }
135
+ for ( const { ftlPath } of translations )
136
+ this . addWatchFile ( ftlPath )
104
137
105
- for ( const dep of dependencies )
138
+ for ( const dep of translations )
106
139
magic . prepend ( `import ${ dep . importVariable } from '${ dep . ftlPath } ';\n` )
107
140
magic . appendLeft ( insertPos , `${ target } .fluent = ${ target } .fluent || {};\n` )
108
- for ( const dep of dependencies )
141
+ for ( const dep of translations )
109
142
magic . appendLeft ( insertPos , `${ target } .fluent['${ dep . locale } '] = ${ dep . importVariable } \n` )
110
143
magic . appendLeft ( insertPos , `
111
144
const __HOT_API__ = import.meta.hot || import.meta.webpackHot
112
145
if (__HOT_API__) {
113
- __HOT_API__.accept([${ dependencies . map ( dep => `'${ dep . ftlPath } '` ) . join ( ', ' ) } ], () => {
114
- ${ dependencies . map ( ( { locale, importVariable } ) => `${ target } .fluent['${ locale } '] = ${ importVariable } ` ) . join ( '\n' ) }
146
+ __HOT_API__.accept([${ translations . map ( dep => `'${ dep . ftlPath } '` ) . join ( ', ' ) } ], () => {
147
+ ${ translations . map ( ( { locale, importVariable } ) => `${ target } .fluent['${ locale } '] = ${ importVariable } ` ) . join ( '\n' ) }
115
148
116
149
delete ${ target } ._fluent
117
150
if (typeof __VUE_HMR_RUNTIME__ !== 'undefined') {
0 commit comments