FlagDependencyExportsPlugin.js 13 KB

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