RequireIncludeDependency.js 2.4 KB

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