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

feat: add support to analyze gzipped bundles #379

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ Analyzer will use module sizes from stats file.
```
To get more information about it you can read [issue #147](https://github.com/webpack-contrib/webpack-bundle-analyzer/issues/147).

### I don't see any stats for compressed files

`webpack-bundle-analyzer` automatically detects files compressed with gzip(.gz) or brotli(.br), and decompresses them to generate stats. If using `compression-webpack-plugin` with `deleteOriginalAssets: true`, make sure to provide same name to output and compressed files so that correct chunk can be referred, see [issue #377](https://github.com/webpack-contrib/webpack-bundle-analyzer/issues/377#issuecomment-682347389). For brotli assets, make sure your node version is `>=10.22.0`.

<h2 align="center">Maintainers</h2>

Expand Down
6 changes: 3 additions & 3 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {parseBundle} = require('./parseUtils');
const {createAssetsFilter} = require('./utils');

const FILENAME_QUERY_REGEXP = /\?.*$/u;
const FILENAME_EXTENSIONS = /\.(js|mjs)$/iu;
const FILENAME_EXTENSIONS = /\.(js|mjs|gz|br)$/iu;

module.exports = {
getViewerData,
Expand Down Expand Up @@ -47,7 +47,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
});
}

// Picking only `*.js or *.mjs` assets from bundle that has non-empty `chunks` array
// Picking only `*.js or *.mjs or *.gz or *.br` assets from bundle that has non-empty `chunks` array
bundleStats.assets = _.filter(bundleStats.assets, asset => {
// Removing query part from filename (yes, somebody uses it for some reason and Webpack supports it)
// See #22
Expand All @@ -69,7 +69,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
let bundleInfo;

try {
bundleInfo = parseBundle(assetFile);
bundleInfo = parseBundle(assetFile, {logger});
} catch (err) {
const msg = (err.code === 'ENOENT') ? 'no such file' : err.message;
logger.warn(`Error parsing bundle asset "${assetFile}": ${msg}`);
Expand Down
29 changes: 27 additions & 2 deletions src/parseUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,38 @@ const fs = require('fs');
const _ = require('lodash');
const acorn = require('acorn');
const walk = require('acorn-walk');
const zlib = require('zlib');
const Logger = require('./Logger');

module.exports = {
parseBundle
};

function parseBundle(bundlePath) {
const content = fs.readFileSync(bundlePath, 'utf8');
const COMPRESSED_EXTENSIONS = /\.(gz|br)$/iu;
const DECOMPRESSION_ALGORITHMS = {
gz: 'unzipSync',
br: 'brotliDecompressSync'
};

function decompressBundle(bundlePath, {logger = new Logger()}) {
const decompressAlgorithm = DECOMPRESSION_ALGORITHMS[bundlePath.split('.').pop()];
if (zlib[decompressAlgorithm]) {
const compressedBuffer = fs.readFileSync(bundlePath);
const decompressedBuffer = zlib[decompressAlgorithm](compressedBuffer);
return decompressedBuffer.toString();
} else {
logger.warn(`Bundle "${bundlePath}" could be compressed, consider upgrading node version`);
return '';
}
}

function parseBundle(bundlePath, opts = {}) {
let content;
if (COMPRESSED_EXTENSIONS.test(bundlePath)) {
content = decompressBundle(bundlePath, opts);
} else {
content = fs.readFileSync(bundlePath, 'utf8');
}
const ast = acorn.parse(content, {
sourceType: 'script',
// I believe in a bright future of ECMAScript!
Expand Down
4 changes: 3 additions & 1 deletion src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ 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
} = opts || {};

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

Expand Down
3 changes: 2 additions & 1 deletion test/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"globals": {
"expect": true,
"makeWebpackConfig": true,
"webpackCompile": true
"webpackCompile": true,
"hasNodeVersion": true
}
}
Binary file added test/bundles/validBundleWithArrowFunction.js.br
Binary file not shown.
Binary file added test/bundles/validBundleWithArrowFunction.js.gz
Binary file not shown.
11 changes: 11 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ chai.use(require('chai-subset'));
global.expect = chai.expect;
global.webpackCompile = webpackCompile;
global.makeWebpackConfig = makeWebpackConfig;
global.hasNodeVersion = hasNodeVersion;

const BundleAnalyzerPlugin = require('../lib/BundleAnalyzerPlugin');

Expand Down Expand Up @@ -80,3 +81,13 @@ function makeWebpackConfig(opts) {
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

function hasNodeVersion(version) {
const currentVersion = process.version.split('v')[1].split('.');
const versions = version.split('.');
for (let i = 0; i < versions.length; i++) {
if (Number(currentVersion[i]) > Number(versions[i])) return true;
else if (Number(currentVersion[i]) < Number(versions[i])) return false;
}
return true;
}
23 changes: 23 additions & 0 deletions test/parseUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const _ = require('lodash');
const {parseBundle} = require('../lib/parseUtils');

const BUNDLES_DIR = `${__dirname}/bundles`;
const COMPRESSIONS = {
brotli: {extension: 'br', minVersion: '10.22.0'},
gzip: {extension: 'gz'}
};

describe('parseBundle', function () {
const bundles = fs
Expand All @@ -30,4 +34,23 @@ describe('parseBundle', function () {
expect(bundle.src).to.equal(fs.readFileSync(bundleFile, 'utf8'));
expect(bundle.modules).to.deep.equal({});
});

Object.keys(COMPRESSIONS)
.forEach(compressionType => {
it(`should parse compressed ${compressionType} bundle`, function () {
const {extension, minVersion} = COMPRESSIONS[compressionType];
const bundleFile = `${BUNDLES_DIR}/validBundleWithArrowFunction.js`;
const compressedBundleFile = `${bundleFile}.${extension}`;
const expectedModules = JSON.parse(fs.readFileSync(`${BUNDLES_DIR}/validBundleWithArrowFunction.modules.json`));
const bundle = parseBundle(compressedBundleFile);
if (minVersion && !hasNodeVersion(minVersion)) {
expect(bundle.src).to.not.equal(fs.readFileSync(bundleFile, 'utf8'));
expect(bundle.modules).to.deep.not.equal(expectedModules.modules);
} else {
expect(bundle.src).to.equal(fs.readFileSync(bundleFile, 'utf8'));
expect(bundle.modules).to.deep.equal(expectedModules.modules);
}
});
});

});