Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make webpack's done hook wait until analyzer writes report / stat file. #247

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/BundleAnalyzerPlugin.js
Expand Up @@ -33,7 +33,7 @@ class BundleAnalyzerPlugin {
apply(compiler) {
this.compiler = compiler;

const done = stats => {
const done = (stats, callback) => {
stats = stats.toJson(this.opts.statsOptions);

const actions = [];
Expand All @@ -55,14 +55,19 @@ class BundleAnalyzerPlugin {

if (actions.length) {
// Making analyzer logs to be after all webpack logs in the console
setImmediate(() => {
actions.forEach(action => action());
setImmediate(async () => {
try {
for (let i = 0, len = actions.length; i < len; ++i) await actions[i]();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to pre-optimise the loop here. AFAIK V8's optimising compiler does this for us automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean write it as for (let i = 0; i < actions.length; ... instead?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, IMO for(let i = 0; i < actions.length; i++) more readable than for(let i = 0, len = actions.length; i < len; ++i ), plus it wouldn't make a huge difference in terms of perf.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, no problem.

if (typeof callback === 'function') callback();
mareolan marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
if (typeof callback === 'function') callback(e);
mareolan marked this conversation as resolved.
Show resolved Hide resolved
}
});
}
};

if (compiler.hooks) {
compiler.hooks.done.tap('webpack-bundle-analyzer', done);
compiler.hooks.done.tapAsync('webpack-bundle-analyzer', done);
} else {
compiler.plugin('done', done);
}
Expand Down Expand Up @@ -108,8 +113,8 @@ class BundleAnalyzerPlugin {
}
}

generateStaticReport(stats) {
viewer.generateReport(stats, {
async generateStaticReport(stats) {
await viewer.generateReport(stats, {
openBrowser: this.opts.openAnalyzer,
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename),
bundleDir: this.getBundleDirFromCompiler(),
Expand Down
63 changes: 37 additions & 26 deletions src/viewer.js
Expand Up @@ -111,7 +111,7 @@ async function startServer(bundleStats, opts) {
}
}

function generateReport(bundleStats, opts) {
async function generateReport(bundleStats, opts) {
const {
openBrowser = true,
reportFilename = 'report.html',
Expand All @@ -125,32 +125,43 @@ function generateReport(bundleStats, opts) {

if (!chartData) return;

ejs.renderFile(
`${projectRoot}/views/viewer.ejs`,
{
mode: 'static',
chartData: JSON.stringify(chartData),
assetContent: getAssetContent,
defaultSizes: JSON.stringify(defaultSizes),
enableWebSocket: false
},
(err, reportHtml) => {
if (err) return logger.error(err);

const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);

mkdir.sync(path.dirname(reportFilepath));
fs.writeFileSync(reportFilepath, reportHtml);

logger.info(
`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`
);

if (openBrowser) {
opener(`file://${reportFilepath}`);
await new Promise((resolve, reject) => {
ejs.renderFile(
`${projectRoot}/views/viewer.ejs`,
{
mode: 'static',
chartData: JSON.stringify(chartData),
assetContent: getAssetContent,
defaultSizes: JSON.stringify(defaultSizes),
enableWebSocket: false
},
(err, reportHtml) => {
try {
if (err) {
logger.error(err);
reject(err);
return;
}

const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);

mkdir.sync(path.dirname(reportFilepath));
fs.writeFileSync(reportFilepath, reportHtml);

logger.info(
`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`
);

if (openBrowser) {
opener(`file://${reportFilepath}`);
}
resolve();
} catch (e) {
reject(e);
}
}
}
);
);
});
}

function getAssetContent(filename) {
Expand Down