Skip to content

Commit 70a2f00

Browse files
authored
Fixes #427, #556 adds options to set sourceType
1 parent f8d4711 commit 70a2f00

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

src/BundleAnalyzerPlugin.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class BundleAnalyzerPlugin {
2424
// deprecated
2525
startAnalyzer: true,
2626
analyzerUrl: utils.defaultAnalyzerUrl,
27+
parserOptions: { sourceType: 'script' },
2728
...opts,
2829
analyzerPort: 'analyzerPort' in opts ? (opts.analyzerPort === 'auto' ? 0 : opts.analyzerPort) : 8888
2930
};
@@ -109,7 +110,8 @@ class BundleAnalyzerPlugin {
109110
logger: this.logger,
110111
defaultSizes: this.opts.defaultSizes,
111112
excludeAssets: this.opts.excludeAssets,
112-
analyzerUrl: this.opts.analyzerUrl
113+
analyzerUrl: this.opts.analyzerUrl,
114+
parserOptions: this.opts.parserOptions
113115
});
114116
}
115117
}
@@ -119,7 +121,8 @@ class BundleAnalyzerPlugin {
119121
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.json'),
120122
bundleDir: this.getBundleDirFromCompiler(),
121123
logger: this.logger,
122-
excludeAssets: this.opts.excludeAssets
124+
excludeAssets: this.opts.excludeAssets,
125+
parserOptions: this.opts.parserOptions
123126
});
124127
}
125128

@@ -131,7 +134,8 @@ class BundleAnalyzerPlugin {
131134
bundleDir: this.getBundleDirFromCompiler(),
132135
logger: this.logger,
133136
defaultSizes: this.opts.defaultSizes,
134-
excludeAssets: this.opts.excludeAssets
137+
excludeAssets: this.opts.excludeAssets,
138+
parserOptions: this.opts.parserOptions
135139
});
136140
}
137141

src/analyzer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module.exports = {
2020
function getViewerData(bundleStats, bundleDir, opts) {
2121
const {
2222
logger = new Logger(),
23-
excludeAssets = null
23+
excludeAssets = null,
24+
parserOptions = { sourceType: 'script' }
2425
} = opts || {};
2526

2627
const isAssetIncluded = createAssetsFilter(excludeAssets);
@@ -77,7 +78,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
7778
let bundleInfo;
7879

7980
try {
80-
bundleInfo = parseBundle(assetFile);
81+
bundleInfo = parseBundle(assetFile, parserOptions);
8182
} catch (err) {
8283
const msg = (err.code === 'ENOENT') ? 'no such file' : err.message;
8384
logger.warn(`Error parsing bundle asset "${assetFile}": ${msg}`);

src/parseUtils.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ module.exports = {
66
parseBundle
77
};
88

9-
function parseBundle(bundlePath) {
9+
function parseBundle(bundlePath, opts) {
10+
const {
11+
sourceType = "script"
12+
} = opts || {};
13+
1014
const content = fs.readFileSync(bundlePath, 'utf8');
1115
const ast = acorn.parse(content, {
12-
sourceType: 'script',
16+
sourceType: sourceType,
1317
// I believe in a bright future of ECMAScript!
1418
// Actually, it's set to `2050` to support the latest ECMAScript version that currently exists.
1519
// Seems like `acorn` supports such weird option value.

src/viewer.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ async function startServer(bundleStats, opts) {
4040
defaultSizes = 'parsed',
4141
excludeAssets = null,
4242
reportTitle,
43-
analyzerUrl
43+
analyzerUrl,
44+
parserOptions = { sourceType: 'script' }
4445
} = opts || {};
4546

46-
const analyzerOpts = {logger, excludeAssets};
47+
const analyzerOpts = {logger, excludeAssets, parserOptions};
4748

4849
let chartData = getChartData(analyzerOpts, bundleStats, bundleDir);
4950
const entrypoints = getEntrypoints(bundleStats);
@@ -136,10 +137,11 @@ async function generateReport(bundleStats, opts) {
136137
bundleDir = null,
137138
logger = new Logger(),
138139
defaultSizes = 'parsed',
139-
excludeAssets = null
140+
excludeAssets = null,
141+
parserOptions = { sourceType: 'script' }
140142
} = opts || {};
141143

142-
const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
144+
const chartData = getChartData({logger, excludeAssets, parserOptions}, bundleStats, bundleDir);
143145
const entrypoints = getEntrypoints(bundleStats);
144146

145147
if (!chartData) return;
@@ -165,9 +167,9 @@ async function generateReport(bundleStats, opts) {
165167
}
166168

167169
async function generateJSONReport(bundleStats, opts) {
168-
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null} = opts || {};
170+
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null, parserOptions = { sourceType: 'script' }} = opts || {};
169171

170-
const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
172+
const chartData = getChartData({logger, excludeAssets, parserOptions}, bundleStats, bundleDir);
171173

172174
if (!chartData) return;
173175

0 commit comments

Comments
 (0)