Skip to content

Commit

Permalink
fix: plugin uses infrastructureLogger and outputs the report.html to …
Browse files Browse the repository at this point in the history
…compiler.outputFileSystem
  • Loading branch information
wood1986 committed Oct 11, 2021
1 parent 8dce252 commit e4e9aa5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Logger = require('./Logger');
const viewer = require('./viewer');
const utils = require('./utils');
const {writeStats} = require('./statsUtils');
const PLUGIN_NAME = 'webpack-bundle-analyzer';

class BundleAnalyzerPlugin {
constructor(opts = {}) {
Expand All @@ -28,13 +29,15 @@ class BundleAnalyzerPlugin {
};

this.server = null;
this.logger = new Logger(this.opts.logLevel);
}

apply(compiler) {
this.compiler = compiler;

this.logger = compiler?.getInfrastructureLogger(PLUGIN_NAME) || require('webpack/logging/runtime').getLogger(PLUGIN_NAME);

const done = (stats, callback) => {
this.fs = compiler.outputFileSystem;
callback = callback || (() => {});

const actions = [];
Expand Down Expand Up @@ -72,15 +75,15 @@ class BundleAnalyzerPlugin {
};

if (compiler.hooks) {
compiler.hooks.done.tapAsync('webpack-bundle-analyzer', done);
compiler.hooks.done.tapAsync(PLUGIN_NAME, done);
} else {
compiler.plugin('done', done);
}
}

async generateStatsFile(stats) {
const statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename);
await fs.promises.mkdir(path.dirname(statsFilepath), {recursive: true});
await this.fs.promises.mkdir(path.dirname(statsFilepath), {recursive: true});

try {
await writeStats(stats, statsFilepath);
Expand Down Expand Up @@ -117,7 +120,8 @@ class BundleAnalyzerPlugin {
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.json'),
bundleDir: this.getBundleDirFromCompiler(),
logger: this.logger,
excludeAssets: this.opts.excludeAssets
excludeAssets: this.opts.excludeAssets,
fs: this.fs
});
}

Expand All @@ -129,7 +133,8 @@ class BundleAnalyzerPlugin {
bundleDir: this.getBundleDirFromCompiler(),
logger: this.logger,
defaultSizes: this.opts.defaultSizes,
excludeAssets: this.opts.excludeAssets
excludeAssets: this.opts.excludeAssets,
fs: this.fs
});
}

Expand Down
7 changes: 5 additions & 2 deletions src/bin/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {resolve, dirname} = require('path');

const commander = require('commander');
const {magenta} = require('chalk');
const fs = require('fs');

const analyzer = require('../analyzer');
const viewer = require('../viewer');
Expand Down Expand Up @@ -138,14 +139,16 @@ if (mode === 'server') {
defaultSizes,
bundleDir,
excludeAssets,
logger: new Logger(logLevel)
logger: new Logger(logLevel),
fs
});
} else if (mode === 'json') {
viewer.generateJSONReport(bundleStats, {
reportFilename: resolve(reportFilename || 'report.json'),
bundleDir,
excludeAssets,
logger: new Logger(logLevel)
logger: new Logger(logLevel),
fs
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/viewer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const path = require('path');
const fs = require('fs');
const http = require('http');

const WebSocket = require('ws');
Expand Down Expand Up @@ -129,7 +128,8 @@ async function generateReport(bundleStats, opts) {
bundleDir = null,
logger = new Logger(),
defaultSizes = 'parsed',
excludeAssets = null
excludeAssets = null,
fs
} = opts || {};

const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
Expand All @@ -156,7 +156,7 @@ async function generateReport(bundleStats, opts) {
}

async function generateJSONReport(bundleStats, opts) {
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null} = opts || {};
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null, fs} = opts || {};

const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);

Expand Down

0 comments on commit e4e9aa5

Please sign in to comment.