Skip to content

Commit

Permalink
Fixes #427, #556 adds options to set sourceType
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio authored Jan 8, 2025
1 parent f8d4711 commit 70a2f00
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
10 changes: 7 additions & 3 deletions src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class BundleAnalyzerPlugin {
// deprecated
startAnalyzer: true,
analyzerUrl: utils.defaultAnalyzerUrl,
parserOptions: { sourceType: 'script' },

Check failure on line 27 in src/BundleAnalyzerPlugin.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space after '{'

Check failure on line 27 in src/BundleAnalyzerPlugin.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space before '}'
...opts,
analyzerPort: 'analyzerPort' in opts ? (opts.analyzerPort === 'auto' ? 0 : opts.analyzerPort) : 8888
};
Expand Down Expand Up @@ -109,7 +110,8 @@ class BundleAnalyzerPlugin {
logger: this.logger,
defaultSizes: this.opts.defaultSizes,
excludeAssets: this.opts.excludeAssets,
analyzerUrl: this.opts.analyzerUrl
analyzerUrl: this.opts.analyzerUrl,
parserOptions: this.opts.parserOptions
});
}
}
Expand All @@ -119,7 +121,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,
parserOptions: this.opts.parserOptions
});
}

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

Expand Down
5 changes: 3 additions & 2 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = {
function getViewerData(bundleStats, bundleDir, opts) {
const {
logger = new Logger(),
excludeAssets = null
excludeAssets = null,
parserOptions = { sourceType: 'script' }

Check failure on line 24 in src/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space after '{'

Check failure on line 24 in src/analyzer.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space before '}'
} = opts || {};

const isAssetIncluded = createAssetsFilter(excludeAssets);
Expand Down Expand Up @@ -77,7 +78,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
let bundleInfo;

try {
bundleInfo = parseBundle(assetFile);
bundleInfo = parseBundle(assetFile, parserOptions);
} catch (err) {
const msg = (err.code === 'ENOENT') ? 'no such file' : err.message;
logger.warn(`Error parsing bundle asset "${assetFile}": ${msg}`);
Expand Down
8 changes: 6 additions & 2 deletions src/parseUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ module.exports = {
parseBundle
};

function parseBundle(bundlePath) {
function parseBundle(bundlePath, opts) {
const {
sourceType = "script"

Check failure on line 11 in src/parseUtils.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
} = opts || {};

const content = fs.readFileSync(bundlePath, 'utf8');
const ast = acorn.parse(content, {
sourceType: 'script',
sourceType: sourceType,

Check failure on line 16 in src/parseUtils.js

View workflow job for this annotation

GitHub Actions / lint

Expected property shorthand
// 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.
Expand Down
14 changes: 8 additions & 6 deletions src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ async function startServer(bundleStats, opts) {
defaultSizes = 'parsed',
excludeAssets = null,
reportTitle,
analyzerUrl
analyzerUrl,
parserOptions = { sourceType: 'script' }

Check failure on line 44 in src/viewer.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space after '{'

Check failure on line 44 in src/viewer.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space before '}'
} = opts || {};

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

let chartData = getChartData(analyzerOpts, bundleStats, bundleDir);
const entrypoints = getEntrypoints(bundleStats);
Expand Down Expand Up @@ -136,10 +137,11 @@ async function generateReport(bundleStats, opts) {
bundleDir = null,
logger = new Logger(),
defaultSizes = 'parsed',
excludeAssets = null
excludeAssets = null,
parserOptions = { sourceType: 'script' }

Check failure on line 141 in src/viewer.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space after '{'

Check failure on line 141 in src/viewer.js

View workflow job for this annotation

GitHub Actions / lint

There should be no space before '}'
} = opts || {};

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

if (!chartData) return;
Expand All @@ -165,9 +167,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, parserOptions = { sourceType: 'script' }} = opts || {};

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

if (!chartData) return;

Expand Down

0 comments on commit 70a2f00

Please sign in to comment.