@@ -3,42 +3,32 @@ const path = require("path");
3
3
4
4
const settings = require ( "../settings" ) ;
5
5
6
+ async function ensureFile ( filename , reject , resolve ) {
7
+ fs . stat ( filename , ( stat_err , _stat ) => {
8
+ if ( stat_err === null ) {
9
+ resolve ( true ) ;
10
+ } else if ( stat_err . code === "ENOENT" ) {
11
+ fs . mkdir ( path . dirname ( filename ) , ( mkdir_err ) => {
12
+ if ( mkdir_err && mkdir_err . code !== "EEXIST" ) {
13
+ reject ( mkdir_err ) ;
14
+ } else {
15
+ resolve ( true ) ;
16
+ }
17
+ } ) ;
18
+ } else {
19
+ reject ( stat_err ) ;
20
+ }
21
+ } ) ;
22
+ }
23
+
6
24
class ModuleLocalesPlugin {
7
- constructor ( options ) {
8
- this . from = options . from ;
25
+ constructor ( ) {
9
26
this . paths_to_watch = [ ] ;
10
27
for ( const language of settings . SUPPORTED_LOCALES ) {
11
28
this . paths_to_watch . push ( `${ language } .json` ) ;
12
29
}
13
30
}
14
31
15
- async ensureAsset ( filename ) {
16
- try {
17
- return await fs . promises . readFile ( filename , "utf8" ) ;
18
- } catch ( _error ) {
19
- await new Promise ( ( resolve , reject ) => {
20
- const parent = path . dirname ( filename ) ;
21
- fs . stat ( parent , ( stat_err , _stat ) => {
22
- if ( stat_err === null ) {
23
- resolve ( true ) ;
24
- } else if ( stat_err . code === "ENOENT" ) {
25
- fs . mkdir ( parent , ( mkdir_err ) => {
26
- if ( mkdir_err && mkdir_err . code !== "EEXIST" ) {
27
- reject ( mkdir_err ) ;
28
- } else {
29
- resolve ( true ) ;
30
- }
31
- } ) ;
32
- } else {
33
- reject ( stat_err ) ;
34
- }
35
- } ) ;
36
- } ) ;
37
- await fs . promises . writeFile ( filename , "{}" ) ;
38
- return "{}" ;
39
- }
40
- }
41
-
42
32
apply ( compiler ) {
43
33
compiler . hooks . thisCompilation . tap ( ModuleLocalesPlugin . name , ( compilation ) => {
44
34
compilation . hooks . processAssets . tapPromise (
@@ -49,6 +39,8 @@ class ModuleLocalesPlugin {
49
39
async ( _assets ) => {
50
40
const assetOutputDirectory = path . dirname ( compilation . outputOptions . assetModuleFilename ) ;
51
41
42
+ const assetsToWatch = [ ] ;
43
+
52
44
for ( const entryPoint of compilation . entrypoints . values ( ) ) {
53
45
let entryPointOrigin = null ;
54
46
@@ -65,14 +57,17 @@ class ModuleLocalesPlugin {
65
57
if ( origin . request . indexOf ( "webpack" ) !== - 1 ) {
66
58
continue ;
67
59
}
60
+
68
61
entryPointOrigin = path . extname ( origin . request )
69
62
? path . resolve ( origin . request , ".." , ".." , "messages" )
70
63
: path . resolve ( origin . request , ".." , "messages" ) ;
71
64
break ;
72
65
}
73
66
67
+ const hasRuntimeChunk = entryPoint . chunks . length > 1 ;
68
+
74
69
for ( const chunk of entryPoint . chunks ) {
75
- if ( chunk . id === chunk . runtime ) {
70
+ if ( hasRuntimeChunk && chunk . id === chunk . runtime ) {
76
71
continue ;
77
72
}
78
73
@@ -81,18 +76,41 @@ class ModuleLocalesPlugin {
81
76
if ( compilation . getAsset ( outputPath ) ) {
82
77
continue ;
83
78
}
84
-
85
79
const inputPath = path . resolve ( entryPointOrigin , asset ) ;
86
- try {
87
- const content = await this . ensureAsset ( inputPath ) ;
88
- compilation . fileDependencies . add ( inputPath ) ;
89
- compilation . emitAsset ( outputPath , new compiler . webpack . sources . RawSource ( content ) ) ;
90
- } catch ( error ) {
91
- console . error ( "Failed to process locale asset" , inputPath , "with error" , error ) ;
92
- }
80
+ assetsToWatch . push ( [ inputPath , outputPath ] ) ;
93
81
}
94
82
}
95
83
}
84
+
85
+ for ( const [ inputPath , outputPath ] of assetsToWatch ) {
86
+ try {
87
+ const content = await new Promise ( ( resolve , reject ) => {
88
+ fs . readFile ( inputPath , "utf8" , ( read_err , read_ok ) => {
89
+ if ( ! read_err ) {
90
+ resolve ( read_ok ) ;
91
+ return ;
92
+ }
93
+ ensureFile ( inputPath , ( ensure_err , _ensure_ok ) => {
94
+ if ( ensure_err ) {
95
+ reject ( ensure_err ) ;
96
+ return ;
97
+ }
98
+ fs . writeFile ( inputPath , "{}" , ( write_err , _write_ok ) => {
99
+ if ( write_err ) {
100
+ reject ( write_err ) ;
101
+ return ;
102
+ }
103
+ resolve ( "{}" ) ;
104
+ } ) ;
105
+ } ) ;
106
+ } ) ;
107
+ } ) ;
108
+ compilation . fileDependencies . add ( inputPath ) ;
109
+ compilation . emitAsset ( outputPath , new compiler . webpack . sources . RawSource ( content ) ) ;
110
+ } catch ( error ) {
111
+ console . error ( "Failed to process locale asset" , inputPath , "with error" , error ) ;
112
+ }
113
+ }
96
114
} ,
97
115
) ;
98
116
} ) ;
0 commit comments