@@ -61,6 +61,7 @@ interface Dependency {
6161export const unplugin = createUnplugin ( ( options : ExternalPluginOptions , meta ) => {
6262 const resolvedOptions = {
6363 checkSyntax : true ,
64+ virtualModuleName : 'virtual:ftl-for-file' ,
6465 getFtlPath : undefined as ( ( locale : string , vuePath : string ) => string ) | undefined ,
6566 ...options ,
6667 }
@@ -74,9 +75,52 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
7475 }
7576 }
7677
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+
7796 return {
7897 name : 'unplugin-fluent-vue-external' ,
7998 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+ } ,
80124 transformInclude ( id : string ) {
81125 return isVue ( id ) || isFtl ( id )
82126 } ,
@@ -86,32 +130,21 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
86130
87131 const { insertPos, target } = getInsertInfo ( source )
88132
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 )
96134
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 )
104137
105- for ( const dep of dependencies )
138+ for ( const dep of translations )
106139 magic . prepend ( `import ${ dep . importVariable } from '${ dep . ftlPath } ';\n` )
107140 magic . appendLeft ( insertPos , `${ target } .fluent = ${ target } .fluent || {};\n` )
108- for ( const dep of dependencies )
141+ for ( const dep of translations )
109142 magic . appendLeft ( insertPos , `${ target } .fluent['${ dep . locale } '] = ${ dep . importVariable } \n` )
110143 magic . appendLeft ( insertPos , `
111144const __HOT_API__ = import.meta.hot || import.meta.webpackHot
112145if (__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' ) }
115148
116149 delete ${ target } ._fluent
117150 if (typeof __VUE_HMR_RUNTIME__ !== 'undefined') {
0 commit comments