FlagDependencyUsagePlugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Dependency = require("./Dependency");
  7. const { UsageState } = require("./ExportsInfo");
  8. const ModuleGraphConnection = require("./ModuleGraphConnection");
  9. const { STAGE_DEFAULT } = require("./OptimizationStages");
  10. const ArrayQueue = require("./util/ArrayQueue");
  11. const TupleQueue = require("./util/TupleQueue");
  12. const { getEntryRuntime, mergeRuntimeOwned } = require("./util/runtime");
  13. /** @typedef {import("./Chunk")} Chunk */
  14. /** @typedef {import("./ChunkGroup")} ChunkGroup */
  15. /** @typedef {import("./Compiler")} Compiler */
  16. /** @typedef {import("./DependenciesBlock")} DependenciesBlock */
  17. /** @typedef {import("./Dependency").ReferencedExport} ReferencedExport */
  18. /** @typedef {import("./ExportsInfo")} ExportsInfo */
  19. /** @typedef {import("./Module")} Module */
  20. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  21. const { NO_EXPORTS_REFERENCED, EXPORTS_OBJECT_REFERENCED } = Dependency;
  22. const PLUGIN_NAME = "FlagDependencyUsagePlugin";
  23. const PLUGIN_LOGGER_NAME = `webpack.${PLUGIN_NAME}`;
  24. class FlagDependencyUsagePlugin {
  25. /**
  26. * @param {boolean} global do a global analysis instead of per runtime
  27. */
  28. constructor(global) {
  29. this.global = global;
  30. }
  31. /**
  32. * Apply the plugin
  33. * @param {Compiler} compiler the compiler instance
  34. * @returns {void}
  35. */
  36. apply(compiler) {
  37. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  38. const moduleGraph = compilation.moduleGraph;
  39. compilation.hooks.optimizeDependencies.tap(
  40. { name: PLUGIN_NAME, stage: STAGE_DEFAULT },
  41. modules => {
  42. if (compilation.moduleMemCaches) {
  43. throw new Error(
  44. "optimization.usedExports can't be used with cacheUnaffected as export usage is a global effect"
  45. );
  46. }
  47. const logger = compilation.getLogger(PLUGIN_LOGGER_NAME);
  48. /** @type {Map<ExportsInfo, Module>} */
  49. const exportInfoToModuleMap = new Map();
  50. /** @type {TupleQueue<[Module, RuntimeSpec]>} */
  51. const queue = new TupleQueue();
  52. /**
  53. * @param {Module} module module to process
  54. * @param {(string[] | ReferencedExport)[]} usedExports list of used exports
  55. * @param {RuntimeSpec} runtime part of which runtime
  56. * @param {boolean} forceSideEffects always apply side effects
  57. * @returns {void}
  58. */
  59. const processReferencedModule = (
  60. module,
  61. usedExports,
  62. runtime,
  63. forceSideEffects
  64. ) => {
  65. const exportsInfo = moduleGraph.getExportsInfo(module);
  66. if (usedExports.length > 0) {
  67. if (!module.buildMeta || !module.buildMeta.exportsType) {
  68. if (exportsInfo.setUsedWithoutInfo(runtime)) {
  69. queue.enqueue(module, runtime);
  70. }
  71. return;
  72. }
  73. for (const usedExportInfo of usedExports) {
  74. let usedExport;
  75. let canMangle = true;
  76. if (Array.isArray(usedExportInfo)) {
  77. usedExport = usedExportInfo;
  78. } else {
  79. usedExport = usedExportInfo.name;
  80. canMangle = usedExportInfo.canMangle !== false;
  81. }
  82. if (usedExport.length === 0) {
  83. if (exportsInfo.setUsedInUnknownWay(runtime)) {
  84. queue.enqueue(module, runtime);
  85. }
  86. } else {
  87. let currentExportsInfo = exportsInfo;
  88. for (let i = 0; i < usedExport.length; i++) {
  89. const exportInfo = currentExportsInfo.getExportInfo(
  90. usedExport[i]
  91. );
  92. if (canMangle === false) {
  93. exportInfo.canMangleUse = false;
  94. }
  95. const lastOne = i === usedExport.length - 1;
  96. if (!lastOne) {
  97. const nestedInfo = exportInfo.getNestedExportsInfo();
  98. if (nestedInfo) {
  99. if (
  100. exportInfo.setUsedConditionally(
  101. used => used === UsageState.Unused,
  102. UsageState.OnlyPropertiesUsed,
  103. runtime
  104. )
  105. ) {
  106. const currentModule =
  107. currentExportsInfo === exportsInfo
  108. ? module
  109. : exportInfoToModuleMap.get(currentExportsInfo);
  110. if (currentModule) {
  111. queue.enqueue(currentModule, runtime);
  112. }
  113. }
  114. currentExportsInfo = nestedInfo;
  115. continue;
  116. }
  117. }
  118. if (
  119. exportInfo.setUsedConditionally(
  120. v => v !== UsageState.Used,
  121. UsageState.Used,
  122. runtime
  123. )
  124. ) {
  125. const currentModule =
  126. currentExportsInfo === exportsInfo
  127. ? module
  128. : exportInfoToModuleMap.get(currentExportsInfo);
  129. if (currentModule) {
  130. queue.enqueue(currentModule, runtime);
  131. }
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. } else {
  138. // for a module without side effects we stop tracking usage here when no export is used
  139. // This module won't be evaluated in this case
  140. // TODO webpack 6 remove this check
  141. if (
  142. !forceSideEffects &&
  143. module.factoryMeta !== undefined &&
  144. module.factoryMeta.sideEffectFree
  145. ) {
  146. return;
  147. }
  148. if (exportsInfo.setUsedForSideEffectsOnly(runtime)) {
  149. queue.enqueue(module, runtime);
  150. }
  151. }
  152. };
  153. /**
  154. * @param {DependenciesBlock} module the module
  155. * @param {RuntimeSpec} runtime part of which runtime
  156. * @param {boolean} forceSideEffects always apply side effects
  157. * @returns {void}
  158. */
  159. const processModule = (module, runtime, forceSideEffects) => {
  160. /** @type {Map<Module, (string[] | ReferencedExport)[] | Map<string, string[] | ReferencedExport>>} */
  161. const map = new Map();
  162. /** @type {ArrayQueue<DependenciesBlock>} */
  163. const queue = new ArrayQueue();
  164. queue.enqueue(module);
  165. for (;;) {
  166. const block = queue.dequeue();
  167. if (block === undefined) break;
  168. for (const b of block.blocks) {
  169. if (
  170. !this.global &&
  171. b.groupOptions &&
  172. b.groupOptions.entryOptions
  173. ) {
  174. processModule(
  175. b,
  176. b.groupOptions.entryOptions.runtime || undefined,
  177. true
  178. );
  179. } else {
  180. queue.enqueue(b);
  181. }
  182. }
  183. for (const dep of block.dependencies) {
  184. const connection = moduleGraph.getConnection(dep);
  185. if (!connection || !connection.module) {
  186. continue;
  187. }
  188. const activeState = connection.getActiveState(runtime);
  189. if (activeState === false) continue;
  190. const { module } = connection;
  191. if (activeState === ModuleGraphConnection.TRANSITIVE_ONLY) {
  192. processModule(module, runtime, false);
  193. continue;
  194. }
  195. const oldReferencedExports = map.get(module);
  196. if (oldReferencedExports === EXPORTS_OBJECT_REFERENCED) {
  197. continue;
  198. }
  199. const referencedExports =
  200. compilation.getDependencyReferencedExports(dep, runtime);
  201. if (
  202. oldReferencedExports === undefined ||
  203. oldReferencedExports === NO_EXPORTS_REFERENCED ||
  204. referencedExports === EXPORTS_OBJECT_REFERENCED
  205. ) {
  206. map.set(module, referencedExports);
  207. } else if (
  208. oldReferencedExports !== undefined &&
  209. referencedExports === NO_EXPORTS_REFERENCED
  210. ) {
  211. continue;
  212. } else {
  213. let exportsMap;
  214. if (Array.isArray(oldReferencedExports)) {
  215. exportsMap = new Map();
  216. for (const item of oldReferencedExports) {
  217. if (Array.isArray(item)) {
  218. exportsMap.set(item.join("\n"), item);
  219. } else {
  220. exportsMap.set(item.name.join("\n"), item);
  221. }
  222. }
  223. map.set(module, exportsMap);
  224. } else {
  225. exportsMap = oldReferencedExports;
  226. }
  227. for (const item of referencedExports) {
  228. if (Array.isArray(item)) {
  229. const key = item.join("\n");
  230. const oldItem = exportsMap.get(key);
  231. if (oldItem === undefined) {
  232. exportsMap.set(key, item);
  233. }
  234. // if oldItem is already an array we have to do nothing
  235. // if oldItem is an ReferencedExport object, we don't have to do anything
  236. // as canMangle defaults to true for arrays
  237. } else {
  238. const key = item.name.join("\n");
  239. const oldItem = exportsMap.get(key);
  240. if (oldItem === undefined || Array.isArray(oldItem)) {
  241. exportsMap.set(key, item);
  242. } else {
  243. exportsMap.set(key, {
  244. name: item.name,
  245. canMangle: item.canMangle && oldItem.canMangle
  246. });
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. for (const [module, referencedExports] of map) {
  254. if (Array.isArray(referencedExports)) {
  255. processReferencedModule(
  256. module,
  257. referencedExports,
  258. runtime,
  259. forceSideEffects
  260. );
  261. } else {
  262. processReferencedModule(
  263. module,
  264. Array.from(referencedExports.values()),
  265. runtime,
  266. forceSideEffects
  267. );
  268. }
  269. }
  270. };
  271. logger.time("initialize exports usage");
  272. for (const module of modules) {
  273. const exportsInfo = moduleGraph.getExportsInfo(module);
  274. exportInfoToModuleMap.set(exportsInfo, module);
  275. exportsInfo.setHasUseInfo();
  276. }
  277. logger.timeEnd("initialize exports usage");
  278. logger.time("trace exports usage in graph");
  279. /**
  280. * @param {Dependency} dep dependency
  281. * @param {RuntimeSpec} runtime runtime
  282. */
  283. const processEntryDependency = (dep, runtime) => {
  284. const module = moduleGraph.getModule(dep);
  285. if (module) {
  286. processReferencedModule(
  287. module,
  288. NO_EXPORTS_REFERENCED,
  289. runtime,
  290. true
  291. );
  292. }
  293. };
  294. /** @type {RuntimeSpec} */
  295. let globalRuntime = undefined;
  296. for (const [
  297. entryName,
  298. { dependencies: deps, includeDependencies: includeDeps, options }
  299. ] of compilation.entries) {
  300. const runtime = this.global
  301. ? undefined
  302. : getEntryRuntime(compilation, entryName, options);
  303. for (const dep of deps) {
  304. processEntryDependency(dep, runtime);
  305. }
  306. for (const dep of includeDeps) {
  307. processEntryDependency(dep, runtime);
  308. }
  309. globalRuntime = mergeRuntimeOwned(globalRuntime, runtime);
  310. }
  311. for (const dep of compilation.globalEntry.dependencies) {
  312. processEntryDependency(dep, globalRuntime);
  313. }
  314. for (const dep of compilation.globalEntry.includeDependencies) {
  315. processEntryDependency(dep, globalRuntime);
  316. }
  317. while (queue.length) {
  318. const [module, runtime] = /** @type {[Module, RuntimeSpec]} */ (
  319. queue.dequeue()
  320. );
  321. processModule(module, runtime, false);
  322. }
  323. logger.timeEnd("trace exports usage in graph");
  324. }
  325. );
  326. });
  327. }
  328. }
  329. module.exports = FlagDependencyUsagePlugin;