| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Alexander Akait @alexander-akait
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { CSS_TYPE } = require("../ModuleSourceTypeConstants");
|
|---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 10 | const CssIcssExportDependency = require("./CssIcssExportDependency");
|
|---|
| 11 | const NullDependency = require("./NullDependency");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 14 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 15 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
|---|
| 16 | /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
|
|---|
| 17 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 18 | /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 19 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 20 | /** @typedef {import("../css/CssParser").Range} Range */
|
|---|
| 21 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 23 | /** @typedef {import("../util/Hash")} Hash */
|
|---|
| 24 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 25 |
|
|---|
| 26 | class CssIcssSymbolDependency extends NullDependency {
|
|---|
| 27 | /**
|
|---|
| 28 | * Creates an instance of CssIcssSymbolDependency.
|
|---|
| 29 | * @param {string} localName local name
|
|---|
| 30 | * @param {Range} range range
|
|---|
| 31 | * @param {string=} value value when it was defined in this module
|
|---|
| 32 | * @param {string=} importName import name when it was imported from other module
|
|---|
| 33 | * @param {string=} request request of the `@value` import that was active when this reference was parsed — used to disambiguate when the same local name is imported from multiple modules
|
|---|
| 34 | */
|
|---|
| 35 | constructor(localName, range, value, importName, request) {
|
|---|
| 36 | super();
|
|---|
| 37 | this.localName = localName;
|
|---|
| 38 | this.range = range;
|
|---|
| 39 | this.value = value;
|
|---|
| 40 | this.importName = importName;
|
|---|
| 41 | this.request = request;
|
|---|
| 42 | /** @type {undefined | string} */
|
|---|
| 43 | this._hashUpdate = undefined;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | get type() {
|
|---|
| 47 | return "css symbol identifier";
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Updates the hash with the data contributed by this instance.
|
|---|
| 52 | * @param {Hash} hash hash to be updated
|
|---|
| 53 | * @param {UpdateHashContext} context context
|
|---|
| 54 | * @returns {void}
|
|---|
| 55 | */
|
|---|
| 56 | updateHash(hash, context) {
|
|---|
| 57 | if (this._hashUpdate === undefined) {
|
|---|
| 58 | // Concatenate with explicit field separators so adjacent fields
|
|---|
| 59 | // can't alias each other (e.g. range `[1,11]` + localName `"foo"`
|
|---|
| 60 | // vs range `[1,1]` + localName `"1foo"`).
|
|---|
| 61 | this._hashUpdate = `range|${JSON.stringify(this.range)}|localName|${this.localName}|value|${this.value || ""}|importName|${this.importName || ""}|request|${this.request || ""}`;
|
|---|
| 62 | }
|
|---|
| 63 | hash.update(this._hashUpdate);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Serializes this instance into the provided serializer context.
|
|---|
| 68 | * @param {ObjectSerializerContext} context context
|
|---|
| 69 | */
|
|---|
| 70 | serialize(context) {
|
|---|
| 71 | const { write } = context;
|
|---|
| 72 | write(this.localName);
|
|---|
| 73 | write(this.range);
|
|---|
| 74 | write(this.value);
|
|---|
| 75 | write(this.importName);
|
|---|
| 76 | write(this.request);
|
|---|
| 77 | super.serialize(context);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Restores this instance from the provided deserializer context.
|
|---|
| 82 | * @param {ObjectDeserializerContext} context context
|
|---|
| 83 | */
|
|---|
| 84 | deserialize(context) {
|
|---|
| 85 | const { read } = context;
|
|---|
| 86 | this.localName = read();
|
|---|
| 87 | this.range = read();
|
|---|
| 88 | this.value = read();
|
|---|
| 89 | this.importName = read();
|
|---|
| 90 | this.request = read();
|
|---|
| 91 | super.deserialize(context);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | CssIcssSymbolDependency.Template = class CssIcssSymbolDependencyTemplate extends (
|
|---|
| 96 | NullDependency.Template
|
|---|
| 97 | ) {
|
|---|
| 98 | /**
|
|---|
| 99 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 100 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 101 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 102 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 103 | * @returns {void}
|
|---|
| 104 | */
|
|---|
| 105 | apply(dependency, source, templateContext) {
|
|---|
| 106 | if (templateContext.type === CSS_TYPE) {
|
|---|
| 107 | const dep = /** @type {CssIcssSymbolDependency} */ (dependency);
|
|---|
| 108 | /** @type {string | undefined} */
|
|---|
| 109 | const value = dep.importName
|
|---|
| 110 | ? CssIcssExportDependency.Template.resolve(
|
|---|
| 111 | dep.localName,
|
|---|
| 112 | dep.importName,
|
|---|
| 113 | templateContext,
|
|---|
| 114 | dep.request
|
|---|
| 115 | )
|
|---|
| 116 | : dep.value;
|
|---|
| 117 |
|
|---|
| 118 | if (!value) {
|
|---|
| 119 | return;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | source.replace(dep.range[0], dep.range[1] - 1, value);
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | };
|
|---|
| 126 |
|
|---|
| 127 | makeSerializable(
|
|---|
| 128 | CssIcssSymbolDependency,
|
|---|
| 129 | "webpack/lib/dependencies/CssIcssSymbolDependency"
|
|---|
| 130 | );
|
|---|
| 131 |
|
|---|
| 132 | module.exports = CssIcssSymbolDependency;
|
|---|