CreateScriptUrlDependency.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 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. * Creates an instance of CreateScriptUrlDependency.
  18. * @param {Range} range range
  19. */
  20. constructor(range) {
  21. super();
  22. this.range = range;
  23. }
  24. get type() {
  25. return "create script url";
  26. }
  27. /**
  28. * Serializes this instance into the provided serializer context.
  29. * @param {ObjectSerializerContext} context context
  30. */
  31. serialize(context) {
  32. const { write } = context;
  33. write(this.range);
  34. super.serialize(context);
  35. }
  36. /**
  37. * Restores this instance from the provided deserializer context.
  38. * @param {ObjectDeserializerContext} context context
  39. */
  40. deserialize(context) {
  41. const { read } = context;
  42. this.range = read();
  43. super.deserialize(context);
  44. }
  45. }
  46. CreateScriptUrlDependency.Template = class CreateScriptUrlDependencyTemplate extends (
  47. NullDependency.Template
  48. ) {
  49. /**
  50. * Applies the plugin by registering its hooks on the compiler.
  51. * @param {Dependency} dependency the dependency for which the template should be applied
  52. * @param {ReplaceSource} source the current replace source which can be modified
  53. * @param {DependencyTemplateContext} templateContext the context object
  54. * @returns {void}
  55. */
  56. apply(dependency, source, { runtimeRequirements }) {
  57. const dep = /** @type {CreateScriptUrlDependency} */ (dependency);
  58. runtimeRequirements.add(RuntimeGlobals.createScriptUrl);
  59. source.insert(dep.range[0], `${RuntimeGlobals.createScriptUrl}(`);
  60. source.insert(dep.range[1], ")");
  61. }
  62. };
  63. makeSerializable(
  64. CreateScriptUrlDependency,
  65. "webpack/lib/dependencies/CreateScriptUrlDependency"
  66. );
  67. module.exports = CreateScriptUrlDependency;