CreateScriptUrlDependency.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const NullDependency = require("./NullDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  12. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  15. class CreateScriptUrlDependency extends NullDependency {
  16. /**
  17. * @param {Range} range range
  18. */
  19. constructor(range) {
  20. super();
  21. this.range = range;
  22. }
  23. get type() {
  24. return "create script url";
  25. }
  26. /**
  27. * @param {ObjectSerializerContext} context context
  28. */
  29. serialize(context) {
  30. const { write } = context;
  31. write(this.range);
  32. super.serialize(context);
  33. }
  34. /**
  35. * @param {ObjectDeserializerContext} context context
  36. */
  37. deserialize(context) {
  38. const { read } = context;
  39. this.range = read();
  40. super.deserialize(context);
  41. }
  42. }
  43. CreateScriptUrlDependency.Template = class CreateScriptUrlDependencyTemplate extends (
  44. NullDependency.Template
  45. ) {
  46. /**
  47. * @param {Dependency} dependency the dependency for which the template should be applied
  48. * @param {ReplaceSource} source the current replace source which can be modified
  49. * @param {DependencyTemplateContext} templateContext the context object
  50. * @returns {void}
  51. */
  52. apply(dependency, source, { runtimeRequirements }) {
  53. const dep = /** @type {CreateScriptUrlDependency} */ (dependency);
  54. runtimeRequirements.add(RuntimeGlobals.createScriptUrl);
  55. source.insert(dep.range[0], `${RuntimeGlobals.createScriptUrl}(`);
  56. source.insert(dep.range[1], ")");
  57. }
  58. };
  59. makeSerializable(
  60. CreateScriptUrlDependency,
  61. "webpack/lib/dependencies/CreateScriptUrlDependency"
  62. );
  63. module.exports = CreateScriptUrlDependency;