1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const makeSerializable = require("../util/makeSerializable");
|
9 | const NullDependency = require("./NullDependency");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | class CssLocalIdentifierDependency extends NullDependency {
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 | constructor(name, range, prefix = "") {
|
24 | super();
|
25 | this.name = name;
|
26 | this.range = range;
|
27 | this.prefix = prefix;
|
28 | }
|
29 |
|
30 | get type() {
|
31 | return "css local identifier";
|
32 | }
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 | getExports(moduleGraph) {
|
40 | const name = this.name;
|
41 | return {
|
42 | exports: [
|
43 | {
|
44 | name,
|
45 | canMangle: true
|
46 | }
|
47 | ],
|
48 | dependencies: undefined
|
49 | };
|
50 | }
|
51 |
|
52 | serialize(context) {
|
53 | const { write } = context;
|
54 | write(this.name);
|
55 | write(this.range);
|
56 | write(this.prefix);
|
57 | super.serialize(context);
|
58 | }
|
59 |
|
60 | deserialize(context) {
|
61 | const { read } = context;
|
62 | this.name = read();
|
63 | this.range = read();
|
64 | this.prefix = read();
|
65 | super.deserialize(context);
|
66 | }
|
67 | }
|
68 |
|
69 | const escapeCssIdentifier = (str, omitUnderscore) => {
|
70 | const escaped = `${str}`.replace(
|
71 |
|
72 | /[^a-zA-Z0-9_\u0081-\uffff-]/g,
|
73 | s => `\\${s}`
|
74 | );
|
75 | return !omitUnderscore && /^(?!--)[0-9-]/.test(escaped)
|
76 | ? `_${escaped}`
|
77 | : escaped;
|
78 | };
|
79 |
|
80 | CssLocalIdentifierDependency.Template = class CssLocalIdentifierDependencyTemplate extends (
|
81 | NullDependency.Template
|
82 | ) {
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | apply(
|
90 | dependency,
|
91 | source,
|
92 | { module, moduleGraph, chunkGraph, runtime, runtimeTemplate, cssExports }
|
93 | ) {
|
94 | const dep = (dependency);
|
95 | const used = moduleGraph
|
96 | .getExportInfo(module, dep.name)
|
97 | .getUsedName(dep.name, runtime);
|
98 | const moduleId = chunkGraph.getModuleId(module);
|
99 | const identifier =
|
100 | dep.prefix +
|
101 | (runtimeTemplate.outputOptions.uniqueName
|
102 | ? runtimeTemplate.outputOptions.uniqueName + "-"
|
103 | : "") +
|
104 | (used ? moduleId + "-" + used : "-");
|
105 | source.replace(
|
106 | dep.range[0],
|
107 | dep.range[1] - 1,
|
108 | escapeCssIdentifier(identifier, dep.prefix)
|
109 | );
|
110 | if (used) cssExports.set(used, identifier);
|
111 | }
|
112 | };
|
113 |
|
114 | makeSerializable(
|
115 | CssLocalIdentifierDependency,
|
116 | "webpack/lib/dependencies/CssLocalIdentifierDependency"
|
117 | );
|
118 |
|
119 | module.exports = CssLocalIdentifierDependency;
|