From a0199c29ade5672b87262c9889cc58f68cbf66ac Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 17 Feb 2025 12:58:15 -0500 Subject: [PATCH 1/2] Fixes #427, #556 adds options to set sourceType --- CHANGELOG.md | 3 +++ src/analyzer.js | 2 +- src/parseUtils.js | 8 ++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c7e81fb..ac2ddc5c 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 + ## 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. From 0d6e7063a0c44c7b1fa1c9dc0b496144944c7c48 Mon Sep 17 00:00:00 2001 From: Vesa Laakso <482561+valscion@users.noreply.github.com> Date: Tue, 18 Feb 2025 09:40:34 +0200 Subject: [PATCH 2/2] Add links to PR and author in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac2ddc5c..e34f5d26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ _Note: Gaps between patch versions are faulty, broken or test releases._ ## UNRELEASED * **Improvement** - * Parse bundles as ES modules based on stats JSON information + * 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