FlagDependencyExportsPlugin.js 14 KB

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