CssExportDependency.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const NullDependency = require("./NullDependency");
  8. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  9. /** @typedef {import("../Dependency")} Dependency */
  10. /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
  11. /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
  12. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  13. class CssExportDependency extends NullDependency {
  14. /**
  15. * @param {string} name name
  16. * @param {string} value value
  17. */
  18. constructor(name, value) {
  19. super();
  20. this.name = name;
  21. this.value = value;
  22. }
  23. get type() {
  24. return "css :export";
  25. }
  26. /**
  27. * Returns the exported names
  28. * @param {ModuleGraph} moduleGraph module graph
  29. * @returns {ExportsSpec | undefined} export names
  30. */
  31. getExports(moduleGraph) {
  32. const name = this.name;
  33. return {
  34. exports: [
  35. {
  36. name,
  37. canMangle: true
  38. }
  39. ],
  40. dependencies: undefined
  41. };
  42. }
  43. serialize(context) {
  44. const { write } = context;
  45. write(this.name);
  46. write(this.value);
  47. super.serialize(context);
  48. }
  49. deserialize(context) {
  50. const { read } = context;
  51. this.name = read();
  52. this.value = read();
  53. super.deserialize(context);
  54. }
  55. }
  56. CssExportDependency.Template = class CssExportDependencyTemplate extends (
  57. NullDependency.Template
  58. ) {
  59. /**
  60. * @param {Dependency} dependency the dependency for which the template should be applied
  61. * @param {ReplaceSource} source the current replace source which can be modified
  62. * @param {DependencyTemplateContext} templateContext the context object
  63. * @returns {void}
  64. */
  65. apply(dependency, source, { cssExports }) {
  66. const dep = /** @type {CssExportDependency} */ (dependency);
  67. cssExports.set(dep.name, dep.value);
  68. }
  69. };
  70. makeSerializable(
  71. CssExportDependency,
  72. "webpack/lib/dependencies/CssExportDependency"
  73. );
  74. module.exports = CssExportDependency;