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 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ new BundleAnalyzerPlugin(options?: object)
|**`statsOptions`**|`null` or `{Object}`|Default: `null`. Options for `stats.toJson()` method. For example you can exclude sources of your modules from stats file with `source: false` option. [See more options here](https://webpack.js.org/configuration/stats/). |
|**`excludeAssets`**|`{null\|pattern\|pattern[]}` where `pattern` equals to `{String\|RegExp\|function}`|Default: `null`. Patterns that will be used to match against asset names to exclude them from the report. If pattern is a string it will be converted to RegExp via `new RegExp(str)`. If pattern is a function it should have the following signature `(assetName: string) => boolean` and should return `true` to *exclude* matching asset. If multiple patterns are provided asset should match at least one of them to be excluded. |
|**`logLevel`**|One of: `info`, `warn`, `error`, `silent`|Default: `info`. Used to control how much details the plugin outputs.|
|**`decompressExtenstion`**|`null` or `{Object}`|Default: `null`. Used to show stats for a compressed file, object has compressed file's extension as key and it's value is decompression algorithm of [zlib](https://nodejs.org/api/zlib.html) to use, like: `{ gz: { algorithm: 'unzipSync' } }`|
Copy link
Collaborator

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 this option - decompression should be transparent to the end-user. It should just work without any configuration.

Copy link
Author

@xitter xitter Sep 1, 2020

Choose a reason for hiding this comment

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

I too agree on this, but then should it be basis extension itself, i.e. detect for gz/br and apply respective decompression?

Copy link
Author

Choose a reason for hiding this comment

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

@th0r does the above comment look good?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think so. We can think about customizable extensions if someone will raise an issue about it.


<h2 align="center">Usage (as a CLI utility)</h2>

Expand Down
10 changes: 7 additions & 3 deletions src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BundleAnalyzerPlugin {
statsOptions: null,
excludeAssets: null,
logLevel: 'info',
decompressExtenstion: {},
// deprecated
startAnalyzer: true,
...opts,
Expand Down Expand Up @@ -114,7 +115,8 @@ class BundleAnalyzerPlugin {
bundleDir: this.getBundleDirFromCompiler(),
logger: this.logger,
defaultSizes: this.opts.defaultSizes,
excludeAssets: this.opts.excludeAssets
excludeAssets: this.opts.excludeAssets,
decompressExtenstion: this.opts.decompressExtenstion
});
}
}
Expand All @@ -124,7 +126,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,
decompressExtenstion: this.opts.decompressExtenstion
});
}

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

Expand Down
14 changes: 11 additions & 3 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ module.exports = {
readStatsFromFile
};

function isExtensionAllowed(filename, {decompressExtenstion = {}}) {
if (FILENAME_EXTENSIONS.test(filename)) {
return true;
}
return !!decompressExtenstion[filename.split('.').pop()];
}

function getViewerData(bundleStats, bundleDir, opts) {
const {
logger = new Logger(),
excludeAssets = null
excludeAssets = null,
decompressExtenstion
} = opts || {};

const isAssetIncluded = createAssetsFilter(excludeAssets);
Expand Down Expand Up @@ -53,7 +61,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
// See #22
asset.name = asset.name.replace(FILENAME_QUERY_REGEXP, '');

return FILENAME_EXTENSIONS.test(asset.name) && !_.isEmpty(asset.chunks) && isAssetIncluded(asset.name);
return isExtensionAllowed(asset.name, opts) && !_.isEmpty(asset.chunks) && isAssetIncluded(asset.name);
});

// Trying to parse bundle assets and get real module sizes if `bundleDir` is provided
Expand All @@ -69,7 +77,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
let bundleInfo;

try {
bundleInfo = parseBundle(assetFile);
bundleInfo = parseBundle(assetFile, {decompressExtenstion, logger});
} catch (err) {
const msg = (err.code === 'ENOENT') ? 'no such file' : err.message;
logger.warn(`Error parsing bundle asset "${assetFile}": ${msg}`);
Expand Down
18 changes: 16 additions & 2 deletions src/parseUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ 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');
function parseBundle(bundlePath, {decompressExtenstion = {}, logger = new Logger()}) {
let content;
const decompressAlgorithm = (decompressExtenstion[bundlePath.split('.').pop()] || {}).algorithm;
if (decompressAlgorithm) {
if (zlib[decompressAlgorithm]) {
const compressedBuffer = fs.readFileSync(bundlePath);
const decompressedBuffer = zlib[decompressAlgorithm](compressedBuffer);
content = decompressedBuffer.toString();
} else {
logger.error(`Algorithm "${decompressAlgorithm}" not available in zlib, consider upgrading node version`);
}
} else {
content = fs.readFileSync(bundlePath, 'utf8');
}
const ast = acorn.parse(content, {
sourceType: 'script',
// I believe in a bright future of ECMAScript!
Expand Down
11 changes: 7 additions & 4 deletions src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ async function startServer(bundleStats, opts) {
logger = new Logger(),
defaultSizes = 'parsed',
excludeAssets = null,
reportTitle
reportTitle,
decompressExtenstion
} = opts || {};

const analyzerOpts = {logger, excludeAssets};
const analyzerOpts = {logger, excludeAssets, decompressExtenstion};

let chartData = getChartData(analyzerOpts, bundleStats, bundleDir);

Expand Down Expand Up @@ -181,9 +182,11 @@ 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, decompressExtenstion
} = opts || {};

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

if (!chartData) return;

Expand Down
Binary file added test/bundles/validBundleWithArrowFunction.js.br
Binary file not shown.
Binary file added test/bundles/validBundleWithArrowFunction.js.gz
Binary file not shown.
30 changes: 28 additions & 2 deletions test/parseUtils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const fs = require('fs');
const zlib = require('zlib');

const _ = require('lodash');
const {parseBundle} = require('../lib/parseUtils');

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

describe('parseBundle', function () {
const bundles = fs
Expand All @@ -16,7 +21,7 @@ describe('parseBundle', function () {
.forEach(bundleName => {
it(`should parse ${_.lowerCase(bundleName)}`, function () {
const bundleFile = `${BUNDLES_DIR}/${bundleName}.js`;
const bundle = parseBundle(bundleFile);
const bundle = parseBundle(bundleFile, {});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Options parameter should be optional.

const expectedModules = JSON.parse(fs.readFileSync(`${BUNDLES_DIR}/${bundleName}.modules.json`));

expect(bundle.src).to.equal(fs.readFileSync(bundleFile, 'utf8'));
Expand All @@ -26,8 +31,29 @@ describe('parseBundle', function () {

it("should parse invalid bundle and return it's content and empty modules hash", function () {
const bundleFile = `${BUNDLES_DIR}/invalidBundle.js`;
const bundle = parseBundle(bundleFile);
const bundle = parseBundle(bundleFile, {});
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, algorithm} = COMPRESSIONS[compressionType];
const bundleFile = `${BUNDLES_DIR}/validBundleWithArrowFunction.js`;
const compressedBundleFile = `${bundleFile}.${extension}`;
const expectedModules = JSON.parse(fs.readFileSync(`${BUNDLES_DIR}/validBundleWithArrowFunction.modules.json`));
if (!zlib[algorithm]) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need this condition - it swallows valid error and we won't notice it.

Copy link
Author

@xitter xitter Sep 1, 2020

Choose a reason for hiding this comment

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

For brotli, node version needs to be >= v11.7.0. Thus I put it to pass ci-checks which run on node 8. How do you suggest handling it?

Copy link
Collaborator

@th0r th0r Sep 1, 2020

Choose a reason for hiding this comment

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

For brotli, node version needs to be >= v11.7.0

We need to mention it in the docs and properly handle in the code.

Copy link
Collaborator

@th0r th0r Sep 1, 2020

Choose a reason for hiding this comment

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

How do you suggest handling it?

Check in tests, whether current version of Node supports it and expect successful decoding in one case and throwing of expected error in the other.

Copy link
Author

Choose a reason for hiding this comment

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

We need to mention it in the docs and properly handle in the code.

Brotli compression works for version >= v11.7.0, so it is kind of an already set expectation for brotli assets. Would you prefer it including in docs anyway?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes. Maybe compression is done with some external tool instead of Node.js.

return;
}
const bundle = parseBundle(compressedBundleFile, {
decompressExtenstion: {
[extension]: {algorithm}
}
});
expect(bundle.src).to.equal(fs.readFileSync(bundleFile, 'utf8'));
expect(bundle.modules).to.deep.equal(expectedModules.modules);
});
});

});