Skip to content

Commit

Permalink
Revert "Update webpack bundle analyzer (#263)"
Browse files Browse the repository at this point in the history
This reverts commit 599248b.
  • Loading branch information
matt-gadd authored Dec 2, 2020
1 parent ae2b864 commit 93b4e83
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 351 deletions.
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ export * from './external-loader-plugin/ExternalLoaderPlugin';

export { default as I18nPlugin } from './i18n-plugin/I18nPlugin';
export * from './i18n-plugin/I18nPlugin';

export { default as BundleAnalyzerPlugin } from './webpack-bundle-analyzer/BundleAnalyzerPlugin';
export * from './webpack-bundle-analyzer/BundleAnalyzerPlugin';
82 changes: 0 additions & 82 deletions src/webpack-bundle-analyzer/AnalyzeBundles.ts

This file was deleted.

95 changes: 95 additions & 0 deletions src/webpack-bundle-analyzer/BundleAnalyzerPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import * as path from 'path';
import * as fs from 'fs';
import * as mkdir from 'mkdirp';
import * as viewer from './viewer';
import { Compiler } from 'webpack';
const bfj = require('bfj');

export interface BundleAnalyzerOptions {
reportFilename: string;
generateStatsFile: boolean;
statsFilename: string;
statsOptions: string | null;
analyzerMode: string;
openAnalyzer: boolean;
excludeBundles?: string;
}

export default class BundleAnalyzerPlugin {
private opts: BundleAnalyzerOptions;
private compiler: any;
private _outputDirectory!: string;
private _outputPath!: string;

constructor(opts: Partial<BundleAnalyzerOptions>) {
this.opts = {
reportFilename: 'report.html',
generateStatsFile: false,
statsFilename: 'stats.json',
statsOptions: null,
analyzerMode: '',
openAnalyzer: false,
...opts
};
}

apply(compiler: Compiler) {
this.compiler = compiler;
const done = (stats: any) => {
this._outputPath = path.resolve(this.compiler.outputPath, this.opts.statsFilename);
this._outputDirectory = path.dirname(this._outputPath);
stats = stats.toJson(this.opts.statsOptions);
stats = this.updateStatsHash(stats);
if (this.opts.generateStatsFile) {
this.generateStatsFile(stats);
}
this.generateStaticReport(stats);
};

compiler.hooks.done.tap(this.constructor.name, done);
}

updateStatsHash(stats: any): any {
try {
const manifest = JSON.parse(fs.readFileSync(path.join(this.compiler.outputPath, 'manifest.json'), 'utf8'));
const originalManifest = JSON.parse(
fs.readFileSync(path.join(this._outputDirectory, 'manifest.original.json'), 'utf8')
);
let updatedStats = JSON.stringify(stats);
Object.keys(manifest).forEach((key) => {
if (originalManifest[key]) {
updatedStats = updatedStats.replace(new RegExp(originalManifest[key], 'g'), manifest[key]);
}
});
return JSON.parse(updatedStats);
} catch (e) {
return stats;
}
}

async generateStatsFile(stats: any) {
mkdir.sync(this._outputDirectory);

try {
await bfj.write(this._outputPath, stats, {
promises: 'ignore',
buffers: 'ignore',
maps: 'ignore',
iterables: 'ignore',
circular: 'ignore'
});
} catch {}
}

generateStaticReport(stats: any) {
viewer.generateReportData(stats, {
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename),
bundleDir: this.getBundleDirFromCompiler(),
excludeBundle: this.opts.excludeBundles
});
}

getBundleDirFromCompiler() {
return this.compiler.outputFileSystem.constructor.name === 'MemoryFileSystem' ? null : this.compiler.outputPath;
}
}
16 changes: 9 additions & 7 deletions src/webpack-bundle-analyzer/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ export function getViewerData(bundleStats: any, bundleDir?: string | null) {
bundleInfo = null;
}

if (bundleInfo) {
bundlesSources[statAsset.name] = bundleInfo.src;
_.assign(parsedModules, bundleInfo.modules);
if (!bundleInfo) {
parsedModules = null;
bundlesSources = null;
break;
}

bundlesSources[statAsset.name] = bundleInfo.src;
_.assign(parsedModules, bundleInfo.modules);
}
}

Expand All @@ -45,7 +49,7 @@ export function getViewerData(bundleStats: any, bundleDir?: string | null) {
(result: any, statAsset: any) => {
const asset: any = (result[statAsset.name] = _.pick(statAsset, 'size'));

if (bundlesSources && bundlesSources[statAsset.name]) {
if (bundlesSources) {
asset.parsedSize = bundlesSources[statAsset.name].length;
asset.gzipSize = gzipSize.sync(bundlesSources[statAsset.name]);
}
Expand All @@ -59,7 +63,6 @@ export function getViewerData(bundleStats: any, bundleDir?: string | null) {
});

asset.tree = createModulesTree(asset.modules);
asset.chunks = statAsset.chunkNames || statAsset.chunks;
},
{}
);
Expand All @@ -72,8 +75,7 @@ export function getViewerData(bundleStats: any, bundleDir?: string | null) {
statSize: asset.tree.size,
parsedSize: asset.parsedSize,
gzipSize: asset.gzipSize,
groups: _.invokeMap(asset.tree.children, 'toChartData'),
chunks: asset.chunks
groups: _.invokeMap(asset.tree.children, 'toChartData')
});
},
[]
Expand Down
Loading

0 comments on commit 93b4e83

Please sign in to comment.