Skip to content

Commit e14a364

Browse files
committed
fix: fix comments
1 parent 7424724 commit e14a364

File tree

4 files changed

+62
-39
lines changed

4 files changed

+62
-39
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/samples
44
node_modules
55
npm-debug.log
6+
.DS_Store

src/BundleAnalyzerPlugin.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,15 @@ class BundleAnalyzerPlugin {
2828
};
2929

3030
this.server = null;
31+
this.logger = new Logger(this.opts.logLevel);
3132
}
3233

3334
apply(compiler) {
34-
const isFromWebpackDevServer = process.env.WEBPACK_SERVE === 'true';
35-
3635
this.compiler = compiler;
3736

38-
this.logger = require('webpack/lib/logging/runtime')?.getLogger(PLUGIN_NAME)
39-
|| new Logger(this.opts.logLevel);
40-
4137
const done = (stats, callback) => {
42-
this.fs = isFromWebpackDevServer ? compiler.outputFileSystem : require('fs');
38+
const isWebpack5 = compiler.webpack;
39+
this.fs = isWebpack5 ? compiler.outputFileSystem : require('fs');
4340
callback = callback || (() => {});
4441

4542
const actions = [];
@@ -77,6 +74,9 @@ class BundleAnalyzerPlugin {
7774
};
7875

7976
if (compiler.hooks) {
77+
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
78+
this.logger = compilation.getLogger(PLUGIN_NAME);
79+
});
8080
compiler.hooks.done.tapAsync(PLUGIN_NAME, done);
8181
} else {
8282
compiler.plugin('done', done);

src/viewer.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ async function generateReport(bundleStats, opts) {
145145
});
146146
const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);
147147

148-
fs.mkdirSync(path.dirname(reportFilepath), {recursive: true});
148+
// older version webpack uses memory-fs whose mkdirSync does not support {recursive: true}
149+
fs.mkdirpSync
150+
? fs.mkdirpSync(path.dirname(reportFilepath))
151+
: fs.mkdirSync(path.dirname(reportFilepath), {recursive: true});
149152
fs.writeFileSync(reportFilepath, reportHtml);
150153

151154
logger.info(`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`);
@@ -162,8 +165,11 @@ async function generateJSONReport(bundleStats, opts) {
162165

163166
if (!chartData) return;
164167

165-
await fs.promises.mkdir(path.dirname(reportFilename), {recursive: true});
166-
await fs.promises.writeFile(reportFilename, JSON.stringify(chartData));
168+
// older version webpack uses memory-fs whose mkdirSync does not support {recursive: true}
169+
fs.mkdirpSync
170+
? fs.mkdirpSync(path.dirname(reportFilename))
171+
: fs.mkdirSync(path.dirname(reportFilename), {recursive: true});
172+
fs.writeFileSync(reportFilename, JSON.stringify(chartData));
167173

168174
logger.info(`${bold('Webpack Bundle Analyzer')} saved JSON report to ${bold(reportFilename)}`);
169175
}

test/dev-server.js

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,58 @@
11
const fs = require('fs');
2-
const {spawn} = require('child_process');
3-
4-
const del = require('del');
5-
62
const ROOT = `${__dirname}/dev-server`;
73
const WEBPACK_CONFIG_PATH = `${ROOT}/webpack.config.js`;
84
const webpackConfig = require(WEBPACK_CONFIG_PATH);
5+
const DevServer = require('webpack-dev-server');
6+
const webpack = require('webpack');
97

108
describe('Webpack Dev Server', function () {
11-
beforeAll(deleteOutputDirectory);
12-
afterEach(deleteOutputDirectory);
13-
14-
const timeout = 15000;
15-
jest.setTimeout(timeout);
16-
17-
it('should save report file to the output directory', function (done) {
18-
const startedAt = Date.now();
19-
20-
const devServer = spawn(`${__dirname}/../node_modules/.bin/webpack-dev-server`, ['--config', WEBPACK_CONFIG_PATH], {
21-
cwd: ROOT
9+
it('should save report file to memory file system when writeToDisk is empty', async function () {
10+
expect.assertions(2);
11+
const compiler = webpack(webpackConfig);
12+
const devServer = await new Promise((resolve) => {
13+
const devServerOptions = {host: '127.0.0.1', port: 8080};
14+
const devServer = new DevServer(compiler, devServerOptions);
15+
devServer.listen(devServerOptions.port, devServerOptions.host, () => {
16+
resolve(devServer);
17+
});
18+
});
19+
await new Promise((resolve) => {
20+
compiler.hooks.afterDone.tap('webpack-bundle-analyzer', resolve);
21+
});
22+
const path = `${webpackConfig.output.path}/report.html`;
23+
expect(compiler.outputFileSystem.existsSync(path)).toBeTruthy();
24+
expect(fs.existsSync(path)).toBeFalsy();
25+
compiler.outputFileSystem.unlinkSync(path);
26+
await new Promise((resolve) => {
27+
devServer.close(() => {
28+
resolve();
29+
});
2230
});
2331

24-
const reportCheckIntervalId = setInterval(() => {
25-
if (fs.existsSync(`${webpackConfig.output.path}/report.html`)) {
26-
finish();
27-
} else if (Date.now() - startedAt > timeout - 1000) {
28-
finish(`report file wasn't found in "${webpackConfig.output.path}" directory`);
29-
}
30-
}, 300);
32+
});
3133

32-
function finish(errorMessage) {
33-
clearInterval(reportCheckIntervalId);
34-
devServer.kill();
35-
done(errorMessage ? new Error(errorMessage) : null);
36-
}
34+
it('should save report file to the output directory when writeToDisk is true', async function () {
35+
expect.assertions(2);
36+
const compiler = webpack(webpackConfig);
37+
const devServer = await new Promise((resolve) => {
38+
const devServerOptions = {host: '127.0.0.1', port: 8080, writeToDisk: true};
39+
const devServer = new DevServer(compiler, devServerOptions);
40+
devServer.listen(devServerOptions.port, devServerOptions.host, () => {
41+
resolve(devServer);
42+
});
43+
});
44+
await new Promise((resolve) => {
45+
compiler.hooks.afterDone.tap('webpack-bundle-analyzer', resolve);
46+
});
47+
const path = `${webpackConfig.output.path}/report.html`;
48+
expect(compiler.outputFileSystem.existsSync(path)).toBeTruthy();
49+
expect(fs.existsSync(path)).toBeTruthy();
50+
compiler.outputFileSystem.unlinkSync(path);
51+
return await new Promise((resolve) => {
52+
devServer.close(() => {
53+
resolve();
54+
});
55+
});
3756
});
3857
});
3958

40-
function deleteOutputDirectory() {
41-
del.sync(webpackConfig.output.path);
42-
}

0 commit comments

Comments
 (0)