UNPKG

10.4 kBJavaScriptView Raw
1const fs = require('fs');
2const _ = require('lodash');
3const acorn = require('acorn');
4const walk = require('acorn-walk');
5
6module.exports = {
7 parseBundle
8};
9
10function parseBundle(bundlePath) {
11 const content = fs.readFileSync(bundlePath, 'utf8');
12 const ast = acorn.parse(content, {
13 sourceType: 'script',
14 // I believe in a bright future of ECMAScript!
15 // Actually, it's set to `2050` to support the latest ECMAScript version that currently exists.
16 // Seems like `acorn` supports such weird option value.
17 ecmaVersion: 2050
18 });
19
20 const walkState = {
21 locations: null,
22 expressionStatementDepth: 0
23 };
24
25 walk.recursive(
26 ast,
27 walkState,
28 {
29 ExpressionStatement(node, state, c) {
30 if (state.locations) return;
31
32 state.expressionStatementDepth++;
33
34 if (
35 // Webpack 5 stores modules in the the top-level IIFE
36 state.expressionStatementDepth === 1 &&
37 ast.body.includes(node) &&
38 isIIFE(node)
39 ) {
40 const fn = getIIFECallExpression(node);
41
42 if (
43 // It should not contain neither arguments
44 fn.arguments.length === 0 &&
45 // ...nor parameters
46 fn.callee.params.length === 0
47 ) {
48 // Modules are stored in the very first variable declaration as hash
49 const firstVariableDeclaration = fn.callee.body.body.find(node => node.type === 'VariableDeclaration');
50
51 if (firstVariableDeclaration) {
52 for (const declaration of firstVariableDeclaration.declarations) {
53 if (declaration.init) {
54 state.locations = getModulesLocations(declaration.init);
55
56 if (state.locations) {
57 break;
58 }
59 }
60 }
61 }
62 }
63 }
64
65 if (!state.locations) {
66 c(node.expression, state);
67 }
68
69 state.expressionStatementDepth--;
70 },
71
72 AssignmentExpression(node, state) {
73 if (state.locations) return;
74
75 // Modules are stored in exports.modules:
76 // exports.modules = {};
77 const {left, right} = node;
78
79 if (
80 left &&
81 left.object && left.object.name === 'exports' &&
82 left.property && left.property.name === 'modules' &&
83 isModulesHash(right)
84 ) {
85 state.locations = getModulesLocations(right);
86 }
87 },
88
89 CallExpression(node, state, c) {
90 if (state.locations) return;
91
92 const args = node.arguments;
93
94 // Main chunk with webpack loader.
95 // Modules are stored in first argument:
96 // (function (...) {...})(<modules>)
97 if (
98 node.callee.type === 'FunctionExpression' &&
99 !node.callee.id &&
100 args.length === 1 &&
101 isSimpleModulesList(args[0])
102 ) {
103 state.locations = getModulesLocations(args[0]);
104 return;
105 }
106
107 // Async Webpack < v4 chunk without webpack loader.
108 // webpackJsonp([<chunks>], <modules>, ...)
109 // As function name may be changed with `output.jsonpFunction` option we can't rely on it's default name.
110 if (
111 node.callee.type === 'Identifier' &&
112 mayBeAsyncChunkArguments(args) &&
113 isModulesList(args[1])
114 ) {
115 state.locations = getModulesLocations(args[1]);
116 return;
117 }
118
119 // Async Webpack v4 chunk without webpack loader.
120 // (window.webpackJsonp=window.webpackJsonp||[]).push([[<chunks>], <modules>, ...]);
121 // As function name may be changed with `output.jsonpFunction` option we can't rely on it's default name.
122 if (isAsyncChunkPushExpression(node)) {
123 state.locations = getModulesLocations(args[0].elements[1]);
124 return;
125 }
126
127 // Webpack v4 WebWorkerChunkTemplatePlugin
128 // globalObject.chunkCallbackName([<chunks>],<modules>, ...);
129 // Both globalObject and chunkCallbackName can be changed through the config, so we can't check them.
130 if (isAsyncWebWorkerChunkExpression(node)) {
131 state.locations = getModulesLocations(args[1]);
132 return;
133 }
134
135
136 // Walking into arguments because some of plugins (e.g. `DedupePlugin`) or some Webpack
137 // features (e.g. `umd` library output) can wrap modules list into additional IIFE.
138 args.forEach(arg => c(arg, state));
139 }
140 }
141 );
142
143 let modules;
144
145 if (walkState.locations) {
146 modules = _.mapValues(walkState.locations,
147 loc => content.slice(loc.start, loc.end)
148 );
149 } else {
150 modules = {};
151 }
152
153 return {
154 modules,
155 src: content,
156 runtimeSrc: getBundleRuntime(content, walkState.locations)
157 };
158}
159
160/**
161 * Returns bundle source except modules
162 */
163function getBundleRuntime(content, modulesLocations) {
164 const sortedLocations = Object.values(modulesLocations || {})
165 .sort((a, b) => a.start - b.start);
166
167 let result = '';
168 let lastIndex = 0;
169
170 for (const {start, end} of sortedLocations) {
171 result += content.slice(lastIndex, start);
172 lastIndex = end;
173 }
174
175 return result + content.slice(lastIndex, content.length);
176}
177
178function isIIFE(node) {
179 return (
180 node.type === 'ExpressionStatement' &&
181 (
182 node.expression.type === 'CallExpression' ||
183 (node.expression.type === 'UnaryExpression' && node.expression.argument.type === 'CallExpression')
184 )
185 );
186}
187
188function getIIFECallExpression(node) {
189 if (node.expression.type === 'UnaryExpression') {
190 return node.expression.argument;
191 } else {
192 return node.expression;
193 }
194}
195
196function isModulesList(node) {
197 return (
198 isSimpleModulesList(node) ||
199 // Modules are contained in expression `Array([minimum ID]).concat([<module>, <module>, ...])`
200 isOptimizedModulesArray(node)
201 );
202}
203
204function isSimpleModulesList(node) {
205 return (
206 // Modules are contained in hash. Keys are module ids.
207 isModulesHash(node) ||
208 // Modules are contained in array. Indexes are module ids.
209 isModulesArray(node)
210 );
211}
212
213function isModulesHash(node) {
214 return (
215 node.type === 'ObjectExpression' &&
216 node.properties
217 .map(node => node.value)
218 .every(isModuleWrapper)
219 );
220}
221
222function isModulesArray(node) {
223 return (
224 node.type === 'ArrayExpression' &&
225 node.elements.every(elem =>
226 // Some of array items may be skipped because there is no module with such id
227 !elem ||
228 isModuleWrapper(elem)
229 )
230 );
231}
232
233function isOptimizedModulesArray(node) {
234 // Checking whether modules are contained in `Array(<minimum ID>).concat(...modules)` array:
235 // https://github.com/webpack/webpack/blob/v1.14.0/lib/Template.js#L91
236 // The `<minimum ID>` + array indexes are module ids
237 return (
238 node.type === 'CallExpression' &&
239 node.callee.type === 'MemberExpression' &&
240 // Make sure the object called is `Array(<some number>)`
241 node.callee.object.type === 'CallExpression' &&
242 node.callee.object.callee.type === 'Identifier' &&
243 node.callee.object.callee.name === 'Array' &&
244 node.callee.object.arguments.length === 1 &&
245 isNumericId(node.callee.object.arguments[0]) &&
246 // Make sure the property X called for `Array(<some number>).X` is `concat`
247 node.callee.property.type === 'Identifier' &&
248 node.callee.property.name === 'concat' &&
249 // Make sure exactly one array is passed in to `concat`
250 node.arguments.length === 1 &&
251 isModulesArray(node.arguments[0])
252 );
253}
254
255function isModuleWrapper(node) {
256 return (
257 // It's an anonymous function expression that wraps module
258 ((node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') && !node.id) ||
259 // If `DedupePlugin` is used it can be an ID of duplicated module...
260 isModuleId(node) ||
261 // or an array of shape [<module_id>, ...args]
262 (node.type === 'ArrayExpression' && node.elements.length > 1 && isModuleId(node.elements[0]))
263 );
264}
265
266function isModuleId(node) {
267 return (node.type === 'Literal' && (isNumericId(node) || typeof node.value === 'string'));
268}
269
270function isNumericId(node) {
271 return (node.type === 'Literal' && Number.isInteger(node.value) && node.value >= 0);
272}
273
274function isChunkIds(node) {
275 // Array of numeric or string ids. Chunk IDs are strings when NamedChunksPlugin is used
276 return (
277 node.type === 'ArrayExpression' &&
278 node.elements.every(isModuleId)
279 );
280}
281
282function isAsyncChunkPushExpression(node) {
283 const {
284 callee,
285 arguments: args
286 } = node;
287
288 return (
289 callee.type === 'MemberExpression' &&
290 callee.property.name === 'push' &&
291 callee.object.type === 'AssignmentExpression' &&
292 args.length === 1 &&
293 args[0].type === 'ArrayExpression' &&
294 mayBeAsyncChunkArguments(args[0].elements) &&
295 isModulesList(args[0].elements[1])
296 );
297}
298
299function mayBeAsyncChunkArguments(args) {
300 return (
301 args.length >= 2 &&
302 isChunkIds(args[0])
303 );
304}
305
306function isAsyncWebWorkerChunkExpression(node) {
307 const {callee, type, arguments: args} = node;
308
309 return (
310 type === 'CallExpression' &&
311 callee.type === 'MemberExpression' &&
312 args.length === 2 &&
313 isChunkIds(args[0]) &&
314 isModulesList(args[1])
315 );
316}
317
318function getModulesLocations(node) {
319 if (node.type === 'ObjectExpression') {
320 // Modules hash
321 const modulesNodes = node.properties;
322
323 return modulesNodes.reduce((result, moduleNode) => {
324 const moduleId = moduleNode.key.name || moduleNode.key.value;
325
326 result[moduleId] = getModuleLocation(moduleNode.value);
327 return result;
328 }, {});
329 }
330
331 const isOptimizedArray = (node.type === 'CallExpression');
332
333 if (node.type === 'ArrayExpression' || isOptimizedArray) {
334 // Modules array or optimized array
335 const minId = isOptimizedArray ?
336 // Get the [minId] value from the Array() call first argument literal value
337 node.callee.object.arguments[0].value :
338 // `0` for simple array
339 0;
340 const modulesNodes = isOptimizedArray ?
341 // The modules reside in the `concat()` function call arguments
342 node.arguments[0].elements :
343 node.elements;
344
345 return modulesNodes.reduce((result, moduleNode, i) => {
346 if (moduleNode) {
347 result[i + minId] = getModuleLocation(moduleNode);
348 }
349 return result;
350 }, {});
351 }
352
353 return {};
354}
355
356function getModuleLocation(node) {
357 return {
358 start: node.start,
359 end: node.end
360 };
361}