1 | const fs = require('fs');
|
2 | const _ = require('lodash');
|
3 | const acorn = require('acorn');
|
4 | const walk = require('acorn-walk');
|
5 |
|
6 | module.exports = {
|
7 | parseBundle
|
8 | };
|
9 |
|
10 | function parseBundle(bundlePath) {
|
11 | const content = fs.readFileSync(bundlePath, 'utf8');
|
12 | const ast = acorn.parse(content, {
|
13 | sourceType: 'script',
|
14 |
|
15 |
|
16 |
|
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 |
|
36 | state.expressionStatementDepth === 1 &&
|
37 | ast.body.includes(node) &&
|
38 | isIIFE(node)
|
39 | ) {
|
40 | const fn = getIIFECallExpression(node);
|
41 |
|
42 | if (
|
43 |
|
44 | fn.arguments.length === 0 &&
|
45 |
|
46 | fn.callee.params.length === 0
|
47 | ) {
|
48 |
|
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 |
|
76 |
|
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 |
|
95 |
|
96 |
|
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 |
|
108 |
|
109 |
|
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 |
|
120 |
|
121 |
|
122 | if (isAsyncChunkPushExpression(node)) {
|
123 | state.locations = getModulesLocations(args[0].elements[1]);
|
124 | return;
|
125 | }
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | if (isAsyncWebWorkerChunkExpression(node)) {
|
131 | state.locations = getModulesLocations(args[1]);
|
132 | return;
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
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 |
|
162 |
|
163 | function 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 |
|
178 | function 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 |
|
188 | function getIIFECallExpression(node) {
|
189 | if (node.expression.type === 'UnaryExpression') {
|
190 | return node.expression.argument;
|
191 | } else {
|
192 | return node.expression;
|
193 | }
|
194 | }
|
195 |
|
196 | function isModulesList(node) {
|
197 | return (
|
198 | isSimpleModulesList(node) ||
|
199 |
|
200 | isOptimizedModulesArray(node)
|
201 | );
|
202 | }
|
203 |
|
204 | function isSimpleModulesList(node) {
|
205 | return (
|
206 |
|
207 | isModulesHash(node) ||
|
208 |
|
209 | isModulesArray(node)
|
210 | );
|
211 | }
|
212 |
|
213 | function isModulesHash(node) {
|
214 | return (
|
215 | node.type === 'ObjectExpression' &&
|
216 | node.properties
|
217 | .map(node => node.value)
|
218 | .every(isModuleWrapper)
|
219 | );
|
220 | }
|
221 |
|
222 | function isModulesArray(node) {
|
223 | return (
|
224 | node.type === 'ArrayExpression' &&
|
225 | node.elements.every(elem =>
|
226 |
|
227 | !elem ||
|
228 | isModuleWrapper(elem)
|
229 | )
|
230 | );
|
231 | }
|
232 |
|
233 | function isOptimizedModulesArray(node) {
|
234 |
|
235 |
|
236 |
|
237 | return (
|
238 | node.type === 'CallExpression' &&
|
239 | node.callee.type === 'MemberExpression' &&
|
240 |
|
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 |
|
247 | node.callee.property.type === 'Identifier' &&
|
248 | node.callee.property.name === 'concat' &&
|
249 |
|
250 | node.arguments.length === 1 &&
|
251 | isModulesArray(node.arguments[0])
|
252 | );
|
253 | }
|
254 |
|
255 | function isModuleWrapper(node) {
|
256 | return (
|
257 |
|
258 | ((node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') && !node.id) ||
|
259 |
|
260 | isModuleId(node) ||
|
261 |
|
262 | (node.type === 'ArrayExpression' && node.elements.length > 1 && isModuleId(node.elements[0]))
|
263 | );
|
264 | }
|
265 |
|
266 | function isModuleId(node) {
|
267 | return (node.type === 'Literal' && (isNumericId(node) || typeof node.value === 'string'));
|
268 | }
|
269 |
|
270 | function isNumericId(node) {
|
271 | return (node.type === 'Literal' && Number.isInteger(node.value) && node.value >= 0);
|
272 | }
|
273 |
|
274 | function isChunkIds(node) {
|
275 |
|
276 | return (
|
277 | node.type === 'ArrayExpression' &&
|
278 | node.elements.every(isModuleId)
|
279 | );
|
280 | }
|
281 |
|
282 | function 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 |
|
299 | function mayBeAsyncChunkArguments(args) {
|
300 | return (
|
301 | args.length >= 2 &&
|
302 | isChunkIds(args[0])
|
303 | );
|
304 | }
|
305 |
|
306 | function 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 |
|
318 | function getModulesLocations(node) {
|
319 | if (node.type === 'ObjectExpression') {
|
320 |
|
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 |
|
335 | const minId = isOptimizedArray ?
|
336 |
|
337 | node.callee.object.arguments[0].value :
|
338 |
|
339 | 0;
|
340 | const modulesNodes = isOptimizedArray ?
|
341 |
|
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 |
|
356 | function getModuleLocation(node) {
|
357 | return {
|
358 | start: node.start,
|
359 | end: node.end
|
360 | };
|
361 | }
|