You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.9 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Module = require("./Module");
  7. const Template = require("./Template");
  8. const { RawSource } = require("webpack-sources");
  9. /** @typedef {import("./util/createHash").Hash} Hash */
  10. class MultiModule extends Module {
  11. constructor(context, dependencies, name) {
  12. super("javascript/dynamic", context);
  13. // Info from Factory
  14. this.dependencies = dependencies;
  15. this.name = name;
  16. this._identifier = `multi ${this.dependencies
  17. .map(d => d.request)
  18. .join(" ")}`;
  19. }
  20. identifier() {
  21. return this._identifier;
  22. }
  23. readableIdentifier(requestShortener) {
  24. return `multi ${this.dependencies
  25. .map(d => requestShortener.shorten(d.request))
  26. .join(" ")}`;
  27. }
  28. build(options, compilation, resolver, fs, callback) {
  29. this.built = true;
  30. this.buildMeta = {};
  31. this.buildInfo = {};
  32. return callback();
  33. }
  34. needRebuild() {
  35. return false;
  36. }
  37. size() {
  38. return 16 + this.dependencies.length * 12;
  39. }
  40. /**
  41. * @param {Hash} hash the hash used to track dependencies
  42. * @returns {void}
  43. */
  44. updateHash(hash) {
  45. hash.update("multi module");
  46. hash.update(this.name || "");
  47. super.updateHash(hash);
  48. }
  49. source(dependencyTemplates, runtimeTemplate) {
  50. const str = [];
  51. let idx = 0;
  52. for (const dep of this.dependencies) {
  53. if (dep.module) {
  54. if (idx === this.dependencies.length - 1) {
  55. str.push("module.exports = ");
  56. }
  57. str.push("__webpack_require__(");
  58. if (runtimeTemplate.outputOptions.pathinfo) {
  59. str.push(Template.toComment(dep.request));
  60. }
  61. str.push(`${JSON.stringify(dep.module.id)}`);
  62. str.push(")");
  63. } else {
  64. const content = require("./dependencies/WebpackMissingModule").module(
  65. dep.request
  66. );
  67. str.push(content);
  68. }
  69. str.push(";\n");
  70. idx++;
  71. }
  72. return new RawSource(str.join(""));
  73. }
  74. }
  75. module.exports = MultiModule;