diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c7e81fb..e34f5d26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ ## UNRELEASED +* **Improvement** + * Parse bundles as ES modules based on stats JSON information ([#649](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/649) by [@eamodio](https://github.com/eamodio)) + ## 4.10.2 * **Bug Fix** diff --git a/src/analyzer.js b/src/analyzer.js index 81eba27b..2d0436b9 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -77,7 +77,7 @@ function getViewerData(bundleStats, bundleDir, opts) { let bundleInfo; try { - bundleInfo = parseBundle(assetFile); + bundleInfo = parseBundle(assetFile, {sourceType: statAsset.info.javascriptModule ? 'module' : 'script'}); } catch (err) { const msg = (err.code === 'ENOENT') ? 'no such file' : err.message; logger.warn(`Error parsing bundle asset "${assetFile}": ${msg}`); diff --git a/src/parseUtils.js b/src/parseUtils.js index cca8c0ae..bdfd71bb 100644 --- a/src/parseUtils.js +++ b/src/parseUtils.js @@ -6,10 +6,14 @@ module.exports = { parseBundle }; -function parseBundle(bundlePath) { +function parseBundle(bundlePath, opts) { + const { + sourceType = 'script' + } = opts || {}; + const content = fs.readFileSync(bundlePath, 'utf8'); const ast = acorn.parse(content, { - sourceType: 'script', + sourceType, // I believe in a bright future of ECMAScript! // Actually, it's set to `2050` to support the latest ECMAScript version that currently exists. // Seems like `acorn` supports such weird option value.