TransformConcatenationModulePlugin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const NormalModule = require('webpack/lib/NormalModule');
  2. const cachePrefix = require('./util').cachePrefix;
  3. const pluginCompat = require('./util/plugin-compat');
  4. function wrapSource(source, methods) {
  5. Object.keys(methods).forEach(key => {
  6. const _method = source[key];
  7. source[key] = function(...args) {
  8. methods[key].apply(this, args);
  9. _method && _method.apply(this, args);
  10. };
  11. });
  12. return source;
  13. }
  14. function spyMethod(name, mods) {
  15. return function(...args) {
  16. mods.push([name].concat([].slice.call(args)));
  17. };
  18. }
  19. function isEqual(a, b) {
  20. if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
  21. return a.reduce(
  22. (carry, value, index) => carry && isEqual(value, b[index]),
  23. true,
  24. );
  25. } else if (a === b) {
  26. return true;
  27. }
  28. return false;
  29. }
  30. class HardModuleConcatenationPlugin {
  31. apply(compiler) {
  32. let store;
  33. let freeze;
  34. pluginCompat.tap(
  35. compiler,
  36. '_hardSourceMethods',
  37. 'HardModuleConcatenationPlugin',
  38. methods => {
  39. store = methods.store;
  40. // fetch = methods.fetch;
  41. freeze = methods.freeze;
  42. // thaw = methods.thaw;
  43. // mapFreeze = methods.mapFreeze;
  44. // mapThaw = methods.mapThaw;
  45. },
  46. );
  47. pluginCompat.tap(
  48. compiler,
  49. '_hardSourceFreezeModule',
  50. 'HardModuleConcatenationPlugin',
  51. (frozen, { modules }, extra) => {
  52. if (modules) {
  53. const compilation = extra.compilation;
  54. modules.forEach(module => {
  55. if (
  56. (module.cacheable ||
  57. (module.buildInfo && module.buildInfo.cacheable)) &&
  58. module instanceof NormalModule
  59. ) {
  60. const identifierPrefix = cachePrefix(compilation);
  61. if (identifierPrefix === null) {
  62. return;
  63. }
  64. const identifier = identifierPrefix + module.identifier();
  65. store('Module', identifier, module, {
  66. id: identifier,
  67. compilation,
  68. });
  69. }
  70. });
  71. }
  72. return frozen;
  73. },
  74. );
  75. pluginCompat.tap(
  76. compiler,
  77. '_hardSourceAfterFreezeModule',
  78. 'HardModuleConcatenationPlugin',
  79. (frozen, module, { compilation }) => {
  80. return frozen;
  81. if (frozen && module.__hardSource_concatedSource) {
  82. const source = module.__hardSource_concatedSource;
  83. frozen.source = source.source();
  84. frozen.sourceMap = freeze('SourceMap', null, source, {
  85. module,
  86. compilation: compilation,
  87. });
  88. frozen.concatenatedSourceMods = module.__hardSource_sourceMods;
  89. }
  90. return frozen;
  91. },
  92. );
  93. }
  94. }
  95. module.exports = HardModuleConcatenationPlugin;