FlagDependencyExportsPlugin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const asyncLib = require("neo-async");
  7. const Queue = require("./util/Queue");
  8. /** @typedef {import("./Compiler")} Compiler */
  9. /** @typedef {import("./DependenciesBlock")} DependenciesBlock */
  10. /** @typedef {import("./Dependency")} Dependency */
  11. /** @typedef {import("./Dependency").ExportSpec} ExportSpec */
  12. /** @typedef {import("./Dependency").ExportsSpec} ExportsSpec */
  13. /** @typedef {import("./ExportsInfo")} ExportsInfo */
  14. /** @typedef {import("./ExportsInfo").RestoreProvidedData} RestoreProvidedData */
  15. /** @typedef {import("./Module")} Module */
  16. /** @typedef {import("./Module").BuildInfo} BuildInfo */
  17. const PLUGIN_NAME = "FlagDependencyExportsPlugin";
  18. const PLUGIN_LOGGER_NAME = `webpack.${PLUGIN_NAME}`;
  19. class FlagDependencyExportsPlugin {
  20. /**
  21. * Apply the plugin
  22. * @param {Compiler} compiler the compiler instance
  23. * @returns {void}
  24. */
  25. apply(compiler) {
  26. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  27. const moduleGraph = compilation.moduleGraph;
  28. const cache = compilation.getCache(PLUGIN_NAME);
  29. compilation.hooks.finishModules.tapAsync(
  30. PLUGIN_NAME,
  31. (modules, callback) => {
  32. const logger = compilation.getLogger(PLUGIN_LOGGER_NAME);
  33. let statRestoredFromMemCache = 0;
  34. let statRestoredFromCache = 0;
  35. let statNoExports = 0;
  36. let statFlaggedUncached = 0;
  37. let statNotCached = 0;
  38. let statQueueItemsProcessed = 0;
  39. const { moduleMemCaches } = compilation;
  40. /** @type {Queue<Module>} */
  41. const queue = new Queue();
  42. // Step 1: Try to restore cached provided export info from cache
  43. logger.time("restore cached provided exports");
  44. asyncLib.each(
  45. modules,
  46. (module, callback) => {
  47. const exportsInfo = moduleGraph.getExportsInfo(module);
  48. // If the module doesn't have an exportsType, it's a module
  49. // without declared exports.
  50. if (
  51. (!module.buildMeta || !module.buildMeta.exportsType) &&
  52. exportsInfo.otherExportsInfo.provided !== null
  53. ) {
  54. // It's a module without declared exports
  55. statNoExports++;
  56. exportsInfo.setHasProvideInfo();
  57. exportsInfo.setUnknownExportsProvided();
  58. return callback();
  59. }
  60. // If the module has no hash, it's uncacheable
  61. if (
  62. typeof (/** @type {BuildInfo} */ (module.buildInfo).hash) !==
  63. "string"
  64. ) {
  65. statFlaggedUncached++;
  66. // Enqueue uncacheable module for determining the exports
  67. queue.enqueue(module);
  68. exportsInfo.setHasProvideInfo();
  69. return callback();
  70. }
  71. const memCache = moduleMemCaches && moduleMemCaches.get(module);
  72. const memCacheValue = memCache && memCache.get(this);
  73. if (memCacheValue !== undefined) {
  74. statRestoredFromMemCache++;
  75. exportsInfo.restoreProvided(memCacheValue);
  76. return callback();
  77. }
  78. cache.get(
  79. module.identifier(),
  80. /** @type {BuildInfo} */
  81. (module.buildInfo).hash,
  82. (err, result) => {
  83. if (err) return callback(err);
  84. if (result !== undefined) {
  85. statRestoredFromCache++;
  86. exportsInfo.restoreProvided(result);
  87. } else {
  88. statNotCached++;
  89. // Without cached info enqueue module for determining the exports
  90. queue.enqueue(module);
  91. exportsInfo.setHasProvideInfo();
  92. }
  93. callback();
  94. }
  95. );
  96. },
  97. err => {
  98. logger.timeEnd("restore cached provided exports");
  99. if (err) return callback(err);
  100. /** @type {Set<Module>} */
  101. const modulesToStore = new Set();
  102. /** @type {Map<Module, Set<Module>>} */
  103. const dependencies = new Map();
  104. /** @type {Module} */
  105. let module;
  106. /** @type {ExportsInfo} */
  107. let exportsInfo;
  108. /** @type {Map<Dependency, ExportsSpec>} */
  109. const exportsSpecsFromDependencies = new Map();
  110. let cacheable = true;
  111. let changed = false;
  112. /**
  113. * @param {DependenciesBlock} depBlock the dependencies block
  114. * @returns {void}
  115. */
  116. const processDependenciesBlock = depBlock => {
  117. for (const dep of depBlock.dependencies) {
  118. processDependency(dep);
  119. }
  120. for (const block of depBlock.blocks) {
  121. processDependenciesBlock(block);
  122. }
  123. };
  124. /**
  125. * @param {Dependency} dep the dependency
  126. * @returns {void}
  127. */
  128. const processDependency = dep => {
  129. const exportDesc = dep.getExports(moduleGraph);
  130. if (!exportDesc) return;
  131. exportsSpecsFromDependencies.set(dep, exportDesc);
  132. };
  133. /**
  134. * @param {Dependency} dep dependency
  135. * @param {ExportsSpec} exportDesc info
  136. * @returns {void}
  137. */
  138. const processExportsSpec = (dep, exportDesc) => {
  139. const exports = exportDesc.exports;
  140. const globalCanMangle = exportDesc.canMangle;
  141. const globalFrom = exportDesc.from;
  142. const globalPriority = exportDesc.priority;
  143. const globalTerminalBinding =
  144. exportDesc.terminalBinding || false;
  145. const exportDeps = exportDesc.dependencies;
  146. if (exportDesc.hideExports) {
  147. for (const name of exportDesc.hideExports) {
  148. const exportInfo = exportsInfo.getExportInfo(name);
  149. exportInfo.unsetTarget(dep);
  150. }
  151. }
  152. if (exports === true) {
  153. // unknown exports
  154. if (
  155. exportsInfo.setUnknownExportsProvided(
  156. globalCanMangle,
  157. exportDesc.excludeExports,
  158. globalFrom && dep,
  159. globalFrom,
  160. globalPriority
  161. )
  162. ) {
  163. changed = true;
  164. }
  165. } else if (Array.isArray(exports)) {
  166. /**
  167. * merge in new exports
  168. * @param {ExportsInfo} exportsInfo own exports info
  169. * @param {(ExportSpec | string)[]} exports list of exports
  170. */
  171. const mergeExports = (exportsInfo, exports) => {
  172. for (const exportNameOrSpec of exports) {
  173. let name;
  174. let canMangle = globalCanMangle;
  175. let terminalBinding = globalTerminalBinding;
  176. let exports;
  177. let from = globalFrom;
  178. let fromExport;
  179. let priority = globalPriority;
  180. let hidden = false;
  181. if (typeof exportNameOrSpec === "string") {
  182. name = exportNameOrSpec;
  183. } else {
  184. name = exportNameOrSpec.name;
  185. if (exportNameOrSpec.canMangle !== undefined)
  186. canMangle = exportNameOrSpec.canMangle;
  187. if (exportNameOrSpec.export !== undefined)
  188. fromExport = exportNameOrSpec.export;
  189. if (exportNameOrSpec.exports !== undefined)
  190. exports = exportNameOrSpec.exports;
  191. if (exportNameOrSpec.from !== undefined)
  192. from = exportNameOrSpec.from;
  193. if (exportNameOrSpec.priority !== undefined)
  194. priority = exportNameOrSpec.priority;
  195. if (exportNameOrSpec.terminalBinding !== undefined)
  196. terminalBinding = exportNameOrSpec.terminalBinding;
  197. if (exportNameOrSpec.hidden !== undefined)
  198. hidden = exportNameOrSpec.hidden;
  199. }
  200. const exportInfo = exportsInfo.getExportInfo(name);
  201. if (
  202. exportInfo.provided === false ||
  203. exportInfo.provided === null
  204. ) {
  205. exportInfo.provided = true;
  206. changed = true;
  207. }
  208. if (
  209. exportInfo.canMangleProvide !== false &&
  210. canMangle === false
  211. ) {
  212. exportInfo.canMangleProvide = false;
  213. changed = true;
  214. }
  215. if (terminalBinding && !exportInfo.terminalBinding) {
  216. exportInfo.terminalBinding = true;
  217. changed = true;
  218. }
  219. if (exports) {
  220. const nestedExportsInfo =
  221. exportInfo.createNestedExportsInfo();
  222. mergeExports(
  223. /** @type {ExportsInfo} */ (nestedExportsInfo),
  224. exports
  225. );
  226. }
  227. if (
  228. from &&
  229. (hidden
  230. ? exportInfo.unsetTarget(dep)
  231. : exportInfo.setTarget(
  232. dep,
  233. from,
  234. fromExport === undefined ? [name] : fromExport,
  235. priority
  236. ))
  237. ) {
  238. changed = true;
  239. }
  240. // Recalculate target exportsInfo
  241. const target = exportInfo.getTarget(moduleGraph);
  242. let targetExportsInfo;
  243. if (target) {
  244. const targetModuleExportsInfo =
  245. moduleGraph.getExportsInfo(target.module);
  246. targetExportsInfo =
  247. targetModuleExportsInfo.getNestedExportsInfo(
  248. target.export
  249. );
  250. // add dependency for this module
  251. const set = dependencies.get(target.module);
  252. if (set === undefined) {
  253. dependencies.set(target.module, new Set([module]));
  254. } else {
  255. set.add(module);
  256. }
  257. }
  258. if (exportInfo.exportsInfoOwned) {
  259. if (
  260. /** @type {ExportsInfo} */
  261. (exportInfo.exportsInfo).setRedirectNamedTo(
  262. targetExportsInfo
  263. )
  264. ) {
  265. changed = true;
  266. }
  267. } else if (exportInfo.exportsInfo !== targetExportsInfo) {
  268. exportInfo.exportsInfo = targetExportsInfo;
  269. changed = true;
  270. }
  271. }
  272. };
  273. mergeExports(exportsInfo, exports);
  274. }
  275. // store dependencies
  276. if (exportDeps) {
  277. cacheable = false;
  278. for (const exportDependency of exportDeps) {
  279. // add dependency for this module
  280. const set = dependencies.get(exportDependency);
  281. if (set === undefined) {
  282. dependencies.set(exportDependency, new Set([module]));
  283. } else {
  284. set.add(module);
  285. }
  286. }
  287. }
  288. };
  289. const notifyDependencies = () => {
  290. const deps = dependencies.get(module);
  291. if (deps !== undefined) {
  292. for (const dep of deps) {
  293. queue.enqueue(dep);
  294. }
  295. }
  296. };
  297. logger.time("figure out provided exports");
  298. while (queue.length > 0) {
  299. module = /** @type {Module} */ (queue.dequeue());
  300. statQueueItemsProcessed++;
  301. exportsInfo = moduleGraph.getExportsInfo(module);
  302. cacheable = true;
  303. changed = false;
  304. exportsSpecsFromDependencies.clear();
  305. moduleGraph.freeze();
  306. processDependenciesBlock(module);
  307. moduleGraph.unfreeze();
  308. for (const [dep, exportsSpec] of exportsSpecsFromDependencies) {
  309. processExportsSpec(dep, exportsSpec);
  310. }
  311. if (cacheable) {
  312. modulesToStore.add(module);
  313. }
  314. if (changed) {
  315. notifyDependencies();
  316. }
  317. }
  318. logger.timeEnd("figure out provided exports");
  319. logger.log(
  320. `${Math.round(
  321. (100 * (statFlaggedUncached + statNotCached)) /
  322. (statRestoredFromMemCache +
  323. statRestoredFromCache +
  324. statNotCached +
  325. statFlaggedUncached +
  326. statNoExports)
  327. )}% of exports of modules have been determined (${statNoExports} no declared exports, ${statNotCached} not cached, ${statFlaggedUncached} flagged uncacheable, ${statRestoredFromCache} from cache, ${statRestoredFromMemCache} from mem cache, ${
  328. statQueueItemsProcessed - statNotCached - statFlaggedUncached
  329. } additional calculations due to dependencies)`
  330. );
  331. logger.time("store provided exports into cache");
  332. asyncLib.each(
  333. modulesToStore,
  334. (module, callback) => {
  335. if (
  336. typeof (
  337. /** @type {BuildInfo} */
  338. (module.buildInfo).hash
  339. ) !== "string"
  340. ) {
  341. // not cacheable
  342. return callback();
  343. }
  344. const cachedData = moduleGraph
  345. .getExportsInfo(module)
  346. .getRestoreProvidedData();
  347. const memCache =
  348. moduleMemCaches && moduleMemCaches.get(module);
  349. if (memCache) {
  350. memCache.set(this, cachedData);
  351. }
  352. cache.store(
  353. module.identifier(),
  354. /** @type {BuildInfo} */
  355. (module.buildInfo).hash,
  356. cachedData,
  357. callback
  358. );
  359. },
  360. err => {
  361. logger.timeEnd("store provided exports into cache");
  362. callback(err);
  363. }
  364. );
  365. }
  366. );
  367. }
  368. );
  369. /** @type {WeakMap<Module, RestoreProvidedData>} */
  370. const providedExportsCache = new WeakMap();
  371. compilation.hooks.rebuildModule.tap(PLUGIN_NAME, module => {
  372. providedExportsCache.set(
  373. module,
  374. moduleGraph.getExportsInfo(module).getRestoreProvidedData()
  375. );
  376. });
  377. compilation.hooks.finishRebuildingModule.tap(PLUGIN_NAME, module => {
  378. moduleGraph.getExportsInfo(module).restoreProvided(
  379. /** @type {RestoreProvidedData} */
  380. (providedExportsCache.get(module))
  381. );
  382. });
  383. });
  384. }
  385. }
  386. module.exports = FlagDependencyExportsPlugin;