CreateScriptUrlDependency.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /** @import { ReplaceSource } from "webpack-sources" */
  10. /** @import Dependency from "../Dependency" */
  11. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  12. /** @import { Range } from "../javascript/JavascriptParser" */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[Range]>} ObjectDeserializerContext */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[Range]>} 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. context.write(this.range);
  33. super.serialize(context);
  34. }
  35. /**
  36. * Restores this instance from the provided deserializer context.
  37. * @param {ObjectDeserializerContext} context context
  38. */
  39. deserialize(context) {
  40. this.range = context.read();
  41. super.deserialize(context.rest);
  42. }
  43. }
  44. CreateScriptUrlDependency.Template = class CreateScriptUrlDependencyTemplate extends (
  45. NullDependency.Template
  46. ) {
  47. /**
  48. * Applies the plugin by registering its hooks on the compiler.
  49. * @param {Dependency} dependency the dependency for which the template should be applied
  50. * @param {ReplaceSource} source the current replace source which can be modified
  51. * @param {DependencyTemplateContext} templateContext the context object
  52. * @returns {void}
  53. */
  54. apply(dependency, source, { runtimeRequirements }) {
  55. const dep = /** @type {CreateScriptUrlDependency} */ (dependency);
  56. runtimeRequirements.add(RuntimeGlobals.createScriptUrl);
  57. source.insert(dep.range[0], `${RuntimeGlobals.createScriptUrl}(`);
  58. source.insert(dep.range[1], ")");
  59. }
  60. };
  61. makeSerializable(
  62. CreateScriptUrlDependency,
  63. "webpack/lib/dependencies/CreateScriptUrlDependency"
  64. );
  65. module.exports = CreateScriptUrlDependency;