1
- const fs = require ( 'fs' ) ;
2
1
const path = require ( 'path' ) ;
3
2
const { bold} = require ( 'chalk' ) ;
4
3
5
4
const Logger = require ( './Logger' ) ;
6
5
const viewer = require ( './viewer' ) ;
7
6
const utils = require ( './utils' ) ;
8
7
const { writeStats} = require ( './statsUtils' ) ;
8
+ const PLUGIN_NAME = 'webpack-bundle-analyzer' ;
9
9
10
10
class BundleAnalyzerPlugin {
11
11
constructor ( opts = { } ) {
@@ -28,13 +28,18 @@ class BundleAnalyzerPlugin {
28
28
} ;
29
29
30
30
this . server = null ;
31
- this . logger = new Logger ( this . opts . logLevel ) ;
32
31
}
33
32
34
33
apply ( compiler ) {
34
+ const isFromWebpackDevServer = process . env . WEBPACK_SERVE === 'true' ;
35
+
35
36
this . compiler = compiler ;
36
37
38
+ this . logger = require ( 'webpack/lib/logging/runtime' ) ?. getLogger ( PLUGIN_NAME )
39
+ || new Logger ( this . opts . logLevel ) ;
40
+
37
41
const done = ( stats , callback ) => {
42
+ this . fs = isFromWebpackDevServer ? compiler . outputFileSystem : require ( 'fs' ) ;
38
43
callback = callback || ( ( ) => { } ) ;
39
44
40
45
const actions = [ ] ;
@@ -72,15 +77,15 @@ class BundleAnalyzerPlugin {
72
77
} ;
73
78
74
79
if ( compiler . hooks ) {
75
- compiler . hooks . done . tapAsync ( 'webpack-bundle-analyzer' , done ) ;
80
+ compiler . hooks . done . tapAsync ( PLUGIN_NAME , done ) ;
76
81
} else {
77
82
compiler . plugin ( 'done' , done ) ;
78
83
}
79
84
}
80
85
81
86
async generateStatsFile ( stats ) {
82
87
const statsFilepath = path . resolve ( this . compiler . outputPath , this . opts . statsFilename ) ;
83
- await fs . promises . mkdir ( path . dirname ( statsFilepath ) , { recursive : true } ) ;
88
+ await this . fs . promises . mkdir ( path . dirname ( statsFilepath ) , { recursive : true } ) ;
84
89
85
90
try {
86
91
await writeStats ( stats , statsFilepath ) ;
@@ -117,7 +122,8 @@ class BundleAnalyzerPlugin {
117
122
reportFilename : path . resolve ( this . compiler . outputPath , this . opts . reportFilename || 'report.json' ) ,
118
123
bundleDir : this . getBundleDirFromCompiler ( ) ,
119
124
logger : this . logger ,
120
- excludeAssets : this . opts . excludeAssets
125
+ excludeAssets : this . opts . excludeAssets ,
126
+ fs : this . fs
121
127
} ) ;
122
128
}
123
129
@@ -129,7 +135,8 @@ class BundleAnalyzerPlugin {
129
135
bundleDir : this . getBundleDirFromCompiler ( ) ,
130
136
logger : this . logger ,
131
137
defaultSizes : this . opts . defaultSizes ,
132
- excludeAssets : this . opts . excludeAssets
138
+ excludeAssets : this . opts . excludeAssets ,
139
+ fs : this . fs
133
140
} ) ;
134
141
}
135
142
0 commit comments