RequireIncludeDependency.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Template = require("../Template");
  8. const makeSerializable = require("../util/makeSerializable");
  9. const ModuleDependency = require("./ModuleDependency");
  10. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  11. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  15. class RequireIncludeDependency extends ModuleDependency {
  16. constructor(request, range) {
  17. super(request);
  18. this.range = range;
  19. }
  20. /**
  21. * Returns list of exports referenced by this dependency
  22. * @param {ModuleGraph} moduleGraph module graph
  23. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  24. * @returns {(string[] | ReferencedExport)[]} referenced exports
  25. */
  26. getReferencedExports(moduleGraph, runtime) {
  27. // This doesn't use any export
  28. return Dependency.NO_EXPORTS_REFERENCED;
  29. }
  30. get type() {
  31. return "require.include";
  32. }
  33. get category() {
  34. return "commonjs";
  35. }
  36. }
  37. makeSerializable(
  38. RequireIncludeDependency,
  39. "webpack/lib/dependencies/RequireIncludeDependency"
  40. );
  41. RequireIncludeDependency.Template = class RequireIncludeDependencyTemplate extends (
  42. ModuleDependency.Template
  43. ) {
  44. /**
  45. * @param {Dependency} dependency the dependency for which the template should be applied
  46. * @param {ReplaceSource} source the current replace source which can be modified
  47. * @param {DependencyTemplateContext} templateContext the context object
  48. * @returns {void}
  49. */
  50. apply(dependency, source, { runtimeTemplate }) {
  51. const dep = /** @type {RequireIncludeDependency} */ (dependency);
  52. const comment = runtimeTemplate.outputOptions.pathinfo
  53. ? Template.toComment(
  54. `require.include ${runtimeTemplate.requestShortener.shorten(
  55. dep.request
  56. )}`
  57. )
  58. : "";
  59. source.replace(dep.range[0], dep.range[1] - 1, `undefined${comment}`);
  60. }
  61. };
  62. module.exports = RequireIncludeDependency;