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