FlagDependencyExportsPlugin.js 13 KB

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