Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LoaderPlugin.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const LoaderDependency = require("./LoaderDependency");
  7. const NormalModule = require("../NormalModule");
  8. /** @typedef {import("../Module")} Module */
  9. /**
  10. * @callback LoadModuleCallback
  11. * @param {Error=} err error object
  12. * @param {string=} source source code
  13. * @param {object=} map source map
  14. * @param {Module=} module loaded module if successful
  15. */
  16. class LoaderPlugin {
  17. apply(compiler) {
  18. compiler.hooks.compilation.tap(
  19. "LoaderPlugin",
  20. (compilation, { normalModuleFactory }) => {
  21. compilation.dependencyFactories.set(
  22. LoaderDependency,
  23. normalModuleFactory
  24. );
  25. }
  26. );
  27. compiler.hooks.compilation.tap("LoaderPlugin", compilation => {
  28. compilation.hooks.normalModuleLoader.tap(
  29. "LoaderPlugin",
  30. (loaderContext, module) => {
  31. /**
  32. * @param {string} request the request string to load the module from
  33. * @param {LoadModuleCallback} callback callback returning the loaded module or error
  34. * @returns {void}
  35. */
  36. loaderContext.loadModule = (request, callback) => {
  37. const dep = new LoaderDependency(request);
  38. dep.loc = {
  39. name: request
  40. };
  41. const factory = compilation.dependencyFactories.get(
  42. dep.constructor
  43. );
  44. if (factory === undefined) {
  45. return callback(
  46. new Error(
  47. `No module factory available for dependency type: ${dep.constructor.name}`
  48. )
  49. );
  50. }
  51. compilation.semaphore.release();
  52. compilation.addModuleDependencies(
  53. module,
  54. [
  55. {
  56. factory,
  57. dependencies: [dep]
  58. }
  59. ],
  60. true,
  61. "lm",
  62. true,
  63. err => {
  64. compilation.semaphore.acquire(() => {
  65. if (err) {
  66. return callback(err);
  67. }
  68. if (!dep.module) {
  69. return callback(new Error("Cannot load the module"));
  70. }
  71. // TODO consider removing this in webpack 5
  72. if (dep.module instanceof NormalModule && dep.module.error) {
  73. return callback(dep.module.error);
  74. }
  75. if (!dep.module._source) {
  76. throw new Error(
  77. "The module created for a LoaderDependency must have a property _source"
  78. );
  79. }
  80. let source, map;
  81. const moduleSource = dep.module._source;
  82. if (moduleSource.sourceAndMap) {
  83. const sourceAndMap = moduleSource.sourceAndMap();
  84. map = sourceAndMap.map;
  85. source = sourceAndMap.source;
  86. } else {
  87. map = moduleSource.map();
  88. source = moduleSource.source();
  89. }
  90. if (dep.module.buildInfo.fileDependencies) {
  91. for (const d of dep.module.buildInfo.fileDependencies) {
  92. loaderContext.addDependency(d);
  93. }
  94. }
  95. if (dep.module.buildInfo.contextDependencies) {
  96. for (const d of dep.module.buildInfo.contextDependencies) {
  97. loaderContext.addContextDependency(d);
  98. }
  99. }
  100. return callback(null, source, map, dep.module);
  101. });
  102. }
  103. );
  104. };
  105. }
  106. );
  107. });
  108. }
  109. }
  110. module.exports = LoaderPlugin;