UNPKG

3.18 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Ivan Kopeykin @vankop
4*/
5
6"use strict";
7
8const Dependency = require("../Dependency");
9const makeSerializable = require("../util/makeSerializable");
10const CssLocalIdentifierDependency = require("./CssLocalIdentifierDependency");
11
12/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
13/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
14/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
15/** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
16/** @typedef {import("../ModuleGraph")} ModuleGraph */
17/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
18
19class CssSelfLocalIdentifierDependency extends CssLocalIdentifierDependency {
20 /**
21 * @param {string} name name
22 * @param {[number, number]} range range
23 * @param {string=} prefix prefix
24 * @param {Set<string>=} declaredSet set of declared names (will only be active when in declared set)
25 */
26 constructor(name, range, prefix = "", declaredSet = undefined) {
27 super(name, range, prefix);
28 this.declaredSet = declaredSet;
29 }
30
31 get type() {
32 return "css self local identifier";
33 }
34
35 get category() {
36 return "self";
37 }
38
39 /**
40 * @returns {string | null} an identifier to merge equal requests
41 */
42 getResourceIdentifier() {
43 return `self`;
44 }
45 /**
46 * Returns the exported names
47 * @param {ModuleGraph} moduleGraph module graph
48 * @returns {ExportsSpec | undefined} export names
49 */
50 getExports(moduleGraph) {
51 if (this.declaredSet && !this.declaredSet.has(this.name)) return;
52 return super.getExports(moduleGraph);
53 }
54
55 /**
56 * Returns list of exports referenced by this dependency
57 * @param {ModuleGraph} moduleGraph module graph
58 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
59 * @returns {(string[] | ReferencedExport)[]} referenced exports
60 */
61 getReferencedExports(moduleGraph, runtime) {
62 if (this.declaredSet && !this.declaredSet.has(this.name))
63 return Dependency.NO_EXPORTS_REFERENCED;
64 return [[this.name]];
65 }
66
67 serialize(context) {
68 const { write } = context;
69 write(this.declaredSet);
70 super.serialize(context);
71 }
72
73 deserialize(context) {
74 const { read } = context;
75 this.declaredSet = read();
76 super.deserialize(context);
77 }
78}
79
80CssSelfLocalIdentifierDependency.Template = class CssSelfLocalIdentifierDependencyTemplate extends (
81 CssLocalIdentifierDependency.Template
82) {
83 /**
84 * @param {Dependency} dependency the dependency for which the template should be applied
85 * @param {ReplaceSource} source the current replace source which can be modified
86 * @param {DependencyTemplateContext} templateContext the context object
87 * @returns {void}
88 */
89 apply(dependency, source, templateContext) {
90 const dep = /** @type {CssSelfLocalIdentifierDependency} */ (dependency);
91 if (dep.declaredSet && !dep.declaredSet.has(dep.name)) return;
92 super.apply(dependency, source, templateContext);
93 }
94};
95
96makeSerializable(
97 CssSelfLocalIdentifierDependency,
98 "webpack/lib/dependencies/CssSelfLocalIdentifierDependency"
99);
100
101module.exports = CssSelfLocalIdentifierDependency;