parseUtils.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. "use strict";
  2. const fs = require('fs');
  3. const _ = require('lodash');
  4. const acorn = require('acorn');
  5. const walk = require('acorn-walk');
  6. module.exports = {
  7. parseBundle
  8. };
  9. function parseBundle(bundlePath) {
  10. const content = fs.readFileSync(bundlePath, 'utf8');
  11. const ast = acorn.parse(content, {
  12. sourceType: 'script',
  13. // I believe in a bright future of ECMAScript!
  14. // Actually, it's set to `2050` to support the latest ECMAScript version that currently exists.
  15. // Seems like `acorn` supports such weird option value.
  16. ecmaVersion: 2050
  17. });
  18. const walkState = {
  19. locations: null
  20. };
  21. walk.recursive(ast, walkState, {
  22. AssignmentExpression(node, state) {
  23. if (state.locations) return; // Modules are stored in exports.modules:
  24. // exports.modules = {};
  25. const {
  26. left,
  27. right
  28. } = node;
  29. if (left && left.object && left.object.name === 'exports' && left.property && left.property.name === 'modules' && isModulesHash(right)) {
  30. state.locations = getModulesLocations(right);
  31. }
  32. },
  33. CallExpression(node, state, c) {
  34. if (state.locations) return;
  35. const args = node.arguments; // Main chunk with webpack loader.
  36. // Modules are stored in first argument:
  37. // (function (...) {...})(<modules>)
  38. if (node.callee.type === 'FunctionExpression' && !node.callee.id && args.length === 1 && isSimpleModulesList(args[0])) {
  39. state.locations = getModulesLocations(args[0]);
  40. return;
  41. } // Async Webpack < v4 chunk without webpack loader.
  42. // webpackJsonp([<chunks>], <modules>, ...)
  43. // As function name may be changed with `output.jsonpFunction` option we can't rely on it's default name.
  44. if (node.callee.type === 'Identifier' && mayBeAsyncChunkArguments(args) && isModulesList(args[1])) {
  45. state.locations = getModulesLocations(args[1]);
  46. return;
  47. } // Async Webpack v4 chunk without webpack loader.
  48. // (window.webpackJsonp=window.webpackJsonp||[]).push([[<chunks>], <modules>, ...]);
  49. // As function name may be changed with `output.jsonpFunction` option we can't rely on it's default name.
  50. if (isAsyncChunkPushExpression(node)) {
  51. state.locations = getModulesLocations(args[0].elements[1]);
  52. return;
  53. } // Webpack v4 WebWorkerChunkTemplatePlugin
  54. // globalObject.chunkCallbackName([<chunks>],<modules>, ...);
  55. // Both globalObject and chunkCallbackName can be changed through the config, so we can't check them.
  56. if (isAsyncWebWorkerChunkExpression(node)) {
  57. state.locations = getModulesLocations(args[1]);
  58. return;
  59. } // Walking into arguments because some of plugins (e.g. `DedupePlugin`) or some Webpack
  60. // features (e.g. `umd` library output) can wrap modules list into additional IIFE.
  61. _.each(args, arg => c(arg, state));
  62. }
  63. });
  64. let modules;
  65. if (walkState.locations) {
  66. modules = _.mapValues(walkState.locations, loc => content.slice(loc.start, loc.end));
  67. } else {
  68. modules = {};
  69. }
  70. return {
  71. src: content,
  72. modules
  73. };
  74. }
  75. function isModulesList(node) {
  76. return isSimpleModulesList(node) || // Modules are contained in expression `Array([minimum ID]).concat([<module>, <module>, ...])`
  77. isOptimizedModulesArray(node);
  78. }
  79. function isSimpleModulesList(node) {
  80. return (// Modules are contained in hash. Keys are module ids.
  81. isModulesHash(node) || // Modules are contained in array. Indexes are module ids.
  82. isModulesArray(node)
  83. );
  84. }
  85. function isModulesHash(node) {
  86. return node.type === 'ObjectExpression' && _(node.properties).map('value').every(isModuleWrapper);
  87. }
  88. function isModulesArray(node) {
  89. return node.type === 'ArrayExpression' && _.every(node.elements, elem => // Some of array items may be skipped because there is no module with such id
  90. !elem || isModuleWrapper(elem));
  91. }
  92. function isOptimizedModulesArray(node) {
  93. // Checking whether modules are contained in `Array(<minimum ID>).concat(...modules)` array:
  94. // https://github.com/webpack/webpack/blob/v1.14.0/lib/Template.js#L91
  95. // The `<minimum ID>` + array indexes are module ids
  96. return node.type === 'CallExpression' && node.callee.type === 'MemberExpression' && // Make sure the object called is `Array(<some number>)`
  97. node.callee.object.type === 'CallExpression' && node.callee.object.callee.type === 'Identifier' && node.callee.object.callee.name === 'Array' && node.callee.object.arguments.length === 1 && isNumericId(node.callee.object.arguments[0]) && // Make sure the property X called for `Array(<some number>).X` is `concat`
  98. node.callee.property.type === 'Identifier' && node.callee.property.name === 'concat' && // Make sure exactly one array is passed in to `concat`
  99. node.arguments.length === 1 && isModulesArray(node.arguments[0]);
  100. }
  101. function isModuleWrapper(node) {
  102. return (// It's an anonymous function expression that wraps module
  103. (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') && !node.id || // If `DedupePlugin` is used it can be an ID of duplicated module...
  104. isModuleId(node) || // or an array of shape [<module_id>, ...args]
  105. node.type === 'ArrayExpression' && node.elements.length > 1 && isModuleId(node.elements[0])
  106. );
  107. }
  108. function isModuleId(node) {
  109. return node.type === 'Literal' && (isNumericId(node) || typeof node.value === 'string');
  110. }
  111. function isNumericId(node) {
  112. return node.type === 'Literal' && Number.isInteger(node.value) && node.value >= 0;
  113. }
  114. function isChunkIds(node) {
  115. // Array of numeric or string ids. Chunk IDs are strings when NamedChunksPlugin is used
  116. return node.type === 'ArrayExpression' && _.every(node.elements, isModuleId);
  117. }
  118. function isAsyncChunkPushExpression(node) {
  119. const {
  120. callee,
  121. arguments: args
  122. } = node;
  123. return callee.type === 'MemberExpression' && callee.property.name === 'push' && callee.object.type === 'AssignmentExpression' && args.length === 1 && args[0].type === 'ArrayExpression' && mayBeAsyncChunkArguments(args[0].elements) && isModulesList(args[0].elements[1]);
  124. }
  125. function mayBeAsyncChunkArguments(args) {
  126. return args.length >= 2 && isChunkIds(args[0]);
  127. }
  128. function isAsyncWebWorkerChunkExpression(node) {
  129. const {
  130. callee,
  131. type,
  132. arguments: args
  133. } = node;
  134. return type === 'CallExpression' && callee.type === 'MemberExpression' && args.length === 2 && isChunkIds(args[0]) && isModulesList(args[1]);
  135. }
  136. function getModulesLocations(node) {
  137. if (node.type === 'ObjectExpression') {
  138. // Modules hash
  139. const modulesNodes = node.properties;
  140. return _.transform(modulesNodes, (result, moduleNode) => {
  141. const moduleId = moduleNode.key.name || moduleNode.key.value;
  142. result[moduleId] = getModuleLocation(moduleNode.value);
  143. }, {});
  144. }
  145. const isOptimizedArray = node.type === 'CallExpression';
  146. if (node.type === 'ArrayExpression' || isOptimizedArray) {
  147. // Modules array or optimized array
  148. const minId = isOptimizedArray ? // Get the [minId] value from the Array() call first argument literal value
  149. node.callee.object.arguments[0].value : // `0` for simple array
  150. 0;
  151. const modulesNodes = isOptimizedArray ? // The modules reside in the `concat()` function call arguments
  152. node.arguments[0].elements : node.elements;
  153. return _.transform(modulesNodes, (result, moduleNode, i) => {
  154. if (!moduleNode) return;
  155. result[i + minId] = getModuleLocation(moduleNode);
  156. }, {});
  157. }
  158. return {};
  159. }
  160. function getModuleLocation(node) {
  161. return {
  162. start: node.start,
  163. end: node.end
  164. };
  165. }