1
+ import { readdir , readFile } from 'fs/promises' ;
1
2
import { dirname , relative } from 'path' ;
2
3
import { fileURLToPath , URL } from 'url' ;
3
4
@@ -33,6 +34,8 @@ const getInput = (hmr: boolean, _isWeb: boolean): InputOption => {
33
34
return inputs ;
34
35
} ;
35
36
37
+ const i18nRegex = / .* s r c \/ i 1 8 n \/ ( [ a - z A - Z ] + ) \/ .* \. j s o n / ;
38
+
36
39
const getPlugins = ( ) : PluginOption [ ] => [
37
40
dtsPlugin ( {
38
41
include : [ 'index.ts' , 'web/**' ] ,
@@ -43,6 +46,37 @@ const getPlugins = (): PluginOption[] => [
43
46
} ) ,
44
47
viteVueCE ( ) ,
45
48
49
+ {
50
+ name : 'i18n-hmr' ,
51
+ configureServer : server => {
52
+ console . info ( 'server start' ) ;
53
+ server . ws . on ( 'fetch:i18n' , async ( ) => {
54
+ const dir = await readdir ( 'dist/_locales' ) ;
55
+ const locales = dir . map ( _lang =>
56
+ readFile ( `dist/_locales/${ _lang } /messages.json` , { encoding : 'utf-8' } ) . then ( locale => ( { lang : _lang , locale : JSON . parse ( locale ) } ) ) ,
57
+ ) ;
58
+ server . ws . send ( {
59
+ type : 'custom' ,
60
+ event : 'update:i18n' ,
61
+ data : await Promise . all ( locales ) ,
62
+ } ) ;
63
+ } ) ;
64
+ } ,
65
+ handleHotUpdate : async ( { server, file, read } ) => {
66
+ const lang = file . match ( i18nRegex ) ?. [ 1 ] ;
67
+ if ( lang ) {
68
+ console . info ( 'Emit new i18n' , file ) ;
69
+ const locale = JSON . parse ( await read ( ) ) ;
70
+ server . ws . send ( {
71
+ type : 'custom' ,
72
+ event : 'update:i18n' ,
73
+ data : [ { lang, locale } ] ,
74
+ } ) ;
75
+ }
76
+ return [ ] ;
77
+ } ,
78
+ } ,
79
+
46
80
// replace document query selector to inject naive-ui within shadow root instead of document head
47
81
{
48
82
name : 'chunk-transform' ,
0 commit comments