TransformNormalModuleFactoryPlugin.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. const fs = require('graceful-fs');
  2. const path = require('path');
  3. const cachePrefix = require('./util').cachePrefix;
  4. const pluginCompat = require('./util/plugin-compat');
  5. const relateContext = require('./util/relate-context');
  6. let NS;
  7. NS = path.dirname(fs.realpathSync(__dirname));
  8. class NormalModuleFactoryPlugin {
  9. apply(compiler) {
  10. const compilerHooks = pluginCompat.hooks(compiler);
  11. let fetch;
  12. compilerHooks._hardSourceMethods.tap(
  13. 'HardSource - TransformNormalModuleFactoryPlugin',
  14. methods => {
  15. fetch = methods.fetch;
  16. },
  17. );
  18. compilerHooks.compilation.tap(
  19. 'HardSource - TransformNormalModuleFactoryPlugin',
  20. (compilation, { normalModuleFactory, contextModuleFactory }) => {
  21. // if (!active) {return;}
  22. // compilation.fileTimestamps = fileTimestamps;
  23. // compilation.contextTimestamps = contextTimestamps;
  24. // compilation.__hardSourceFileMd5s = fileMd5s;
  25. // compilation.__hardSourceCachedMd5s = cachedMd5s;
  26. const compilationHooks = pluginCompat.hooks(compilation);
  27. compilationHooks.buildModule.tap(
  28. 'HardSource - TransformNormalModuleFactoryPlugin',
  29. module => {
  30. if (module.constructor.name === 'NormalModule') {
  31. module.__originalCreateLoaderContext =
  32. module.__originalCreateLoaderContext ||
  33. module.createLoaderContext;
  34. module.__hardSource_resolved = {};
  35. module.createLoaderContext = (...args) => {
  36. const loaderContext = module.__originalCreateLoaderContext(
  37. ...args,
  38. );
  39. const _resolve = loaderContext.resolve;
  40. loaderContext.resolve = (context, request, callback) => {
  41. _resolve.call(
  42. loaderContext,
  43. context,
  44. request,
  45. (err, result) => {
  46. if (err) {
  47. callback(err, result);
  48. } else {
  49. module.__hardSource_resolved[
  50. JSON.stringify({ context, request })
  51. ] = {
  52. resource: result,
  53. resolveOptions: module.resolveOptions,
  54. };
  55. callback(err, result);
  56. }
  57. },
  58. );
  59. };
  60. return loaderContext;
  61. };
  62. }
  63. },
  64. );
  65. const normalModuleFactoryHooks = pluginCompat.hooks(
  66. normalModuleFactory,
  67. );
  68. // Webpack 2 can use different parsers based on config rule sets.
  69. normalModuleFactoryHooks.parser
  70. .for('javascript/auto')
  71. .tap(
  72. 'HardSource - TransformNormalModuleFactoryPlugin',
  73. (parser, options) => {
  74. // Store the options somewhere that can not conflict with another plugin
  75. // on the parser so we can look it up and store those options with a
  76. // cached module resolution.
  77. parser[`${NS}/parser-options`] = options;
  78. },
  79. );
  80. normalModuleFactoryHooks.resolver.tap(
  81. 'HardSource - TransformNormalModuleFactoryPlugin',
  82. fn => (request, cb) => {
  83. const identifierPrefix = cachePrefix(compilation);
  84. if (identifierPrefix === null) {
  85. return fn.call(null, request, cb);
  86. }
  87. const cacheId = JSON.stringify([
  88. identifierPrefix,
  89. request.context,
  90. request.request,
  91. ]);
  92. const absCacheId = JSON.stringify([
  93. identifierPrefix,
  94. request.context,
  95. relateContext.relateAbsoluteRequest(
  96. request.context,
  97. request.request,
  98. ),
  99. ]);
  100. request.contextInfo.resolveOptions = request.resolveOptions;
  101. const next = () => {
  102. const originalRequest = request;
  103. return fn.call(null, request, function(err, request) {
  104. if (err) {
  105. return cb(err);
  106. }
  107. if (!request.source) {
  108. compilation.__hardSourceModuleResolveCacheChange.push(
  109. cacheId,
  110. );
  111. compilation.__hardSourceModuleResolveCache[
  112. cacheId
  113. ] = Object.assign({}, request, {
  114. parser: null,
  115. generator: null,
  116. parserOptions: request.parser[`${NS}/parser-options`],
  117. type: request.settings && request.settings.type,
  118. settings: request.settings,
  119. resourceResolveData: request.resourceResolveData,
  120. dependencies: null,
  121. });
  122. }
  123. cb(...arguments);
  124. });
  125. };
  126. const fromCache = () => {
  127. const result = Object.assign(
  128. {},
  129. compilation.__hardSourceModuleResolveCache[cacheId] ||
  130. compilation.__hardSourceModuleResolveCache[absCacheId],
  131. );
  132. result.dependencies = request.dependencies;
  133. if (!result.parser || !result.parser.parse) {
  134. result.parser = result.settings
  135. ? normalModuleFactory.getParser(
  136. result.type,
  137. result.settings.parser,
  138. )
  139. : normalModuleFactory.getParser(result.parserOptions);
  140. }
  141. if (!result.generator && normalModuleFactory.getGenerator) {
  142. result.generator = normalModuleFactory.getGenerator(
  143. result.type,
  144. result.settings.generator,
  145. );
  146. }
  147. result.loaders = result.loaders.map(loader => {
  148. if (typeof loader === 'object' && loader.ident) {
  149. const ruleSet = normalModuleFactory.ruleSet;
  150. return {
  151. loader: loader.loader,
  152. ident: loader.ident,
  153. options: ruleSet.references[loader.ident],
  154. };
  155. }
  156. return loader;
  157. });
  158. return cb(null, result);
  159. };
  160. if (
  161. (compilation.__hardSourceModuleResolveCache[cacheId] &&
  162. !compilation.__hardSourceModuleResolveCache[cacheId].invalid) ||
  163. (compilation.__hardSourceModuleResolveCache[absCacheId] &&
  164. !compilation.__hardSourceModuleResolveCache[absCacheId].invalid)
  165. ) {
  166. return fromCache();
  167. }
  168. next();
  169. },
  170. );
  171. normalModuleFactoryHooks.createModule.tap(
  172. 'HardSourceWebpackPlugin',
  173. ({ request }) => {
  174. if (
  175. compilation.cache &&
  176. compilation.cache[`m${request}`] &&
  177. compilation.cache[`m${request}`].cacheItem &&
  178. !compilation.cache[`m${request}`].cacheItem.invalid
  179. ) {
  180. return compilation.cache[`m${request}`];
  181. }
  182. const identifierPrefix = cachePrefix(compilation);
  183. if (identifierPrefix === null) {
  184. return;
  185. }
  186. const identifier = identifierPrefix + request;
  187. const module = fetch('Module', identifier, {
  188. compilation,
  189. normalModuleFactory: normalModuleFactory,
  190. contextModuleFactory: contextModuleFactory,
  191. });
  192. if (module) {
  193. return module;
  194. }
  195. },
  196. );
  197. },
  198. );
  199. }
  200. }
  201. module.exports = NormalModuleFactoryPlugin;