CreateScriptUrlDependency.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. class CreateScriptUrlDependency extends NullDependency {
  13. /**
  14. * @param {[number, number]} range range
  15. */
  16. constructor(range) {
  17. super();
  18. this.range = range;
  19. }
  20. get type() {
  21. return "create script url";
  22. }
  23. serialize(context) {
  24. const { write } = context;
  25. write(this.range);
  26. super.serialize(context);
  27. }
  28. deserialize(context) {
  29. const { read } = context;
  30. this.range = read();
  31. super.deserialize(context);
  32. }
  33. }
  34. CreateScriptUrlDependency.Template = class CreateScriptUrlDependencyTemplate extends (
  35. NullDependency.Template
  36. ) {
  37. /**
  38. * @param {Dependency} dependency the dependency for which the template should be applied
  39. * @param {ReplaceSource} source the current replace source which can be modified
  40. * @param {DependencyTemplateContext} templateContext the context object
  41. * @returns {void}
  42. */
  43. apply(dependency, source, { runtimeRequirements }) {
  44. const dep = /** @type {CreateScriptUrlDependency} */ (dependency);
  45. runtimeRequirements.add(RuntimeGlobals.createScriptUrl);
  46. source.insert(dep.range[0], `${RuntimeGlobals.createScriptUrl}(`);
  47. source.insert(dep.range[1], ")");
  48. }
  49. };
  50. makeSerializable(
  51. CreateScriptUrlDependency,
  52. "webpack/lib/dependencies/CreateScriptUrlDependency"
  53. );
  54. module.exports = CreateScriptUrlDependency;