1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Ivan Kopeykin @vankop
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const RawDataUrlModule = require("../asset/RawDataUrlModule");
|
---|
9 | const makeSerializable = require("../util/makeSerializable");
|
---|
10 | const memoize = require("../util/memoize");
|
---|
11 | const ModuleDependency = require("./ModuleDependency");
|
---|
12 |
|
---|
13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
14 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
15 | /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
|
---|
16 | /** @typedef {import("../Dependency")} Dependency */
|
---|
17 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
18 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
19 | /** @typedef {import("../Module")} Module */
|
---|
20 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
---|
21 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
22 | /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
|
---|
23 | /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
|
---|
24 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
25 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
26 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
27 | /** @typedef {import("../util/Hash")} Hash */
|
---|
28 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
29 |
|
---|
30 | const getIgnoredRawDataUrlModule = memoize(
|
---|
31 | () => new RawDataUrlModule("data:,", "ignored-asset", "(ignored asset)")
|
---|
32 | );
|
---|
33 |
|
---|
34 | class CssUrlDependency extends ModuleDependency {
|
---|
35 | /**
|
---|
36 | * @param {string} request request
|
---|
37 | * @param {Range} range range of the argument
|
---|
38 | * @param {"string" | "url" | "src"} urlType dependency type e.g. url() or string
|
---|
39 | */
|
---|
40 | constructor(request, range, urlType) {
|
---|
41 | super(request);
|
---|
42 | this.range = range;
|
---|
43 | this.urlType = urlType;
|
---|
44 | }
|
---|
45 |
|
---|
46 | get type() {
|
---|
47 | return "css url()";
|
---|
48 | }
|
---|
49 |
|
---|
50 | get category() {
|
---|
51 | return "url";
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * @param {string} context context directory
|
---|
56 | * @returns {Module | null} a module
|
---|
57 | */
|
---|
58 | createIgnoredModule(context) {
|
---|
59 | return getIgnoredRawDataUrlModule();
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * @param {ObjectSerializerContext} context context
|
---|
64 | */
|
---|
65 | serialize(context) {
|
---|
66 | const { write } = context;
|
---|
67 | write(this.urlType);
|
---|
68 | super.serialize(context);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * @param {ObjectDeserializerContext} context context
|
---|
73 | */
|
---|
74 | deserialize(context) {
|
---|
75 | const { read } = context;
|
---|
76 | this.urlType = read();
|
---|
77 | super.deserialize(context);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * @param {string} str string
|
---|
83 | * @returns {string} string in quotes if needed
|
---|
84 | */
|
---|
85 | const cssEscapeString = str => {
|
---|
86 | let countWhiteOrBracket = 0;
|
---|
87 | let countQuotation = 0;
|
---|
88 | let countApostrophe = 0;
|
---|
89 | for (let i = 0; i < str.length; i++) {
|
---|
90 | const cc = str.charCodeAt(i);
|
---|
91 | switch (cc) {
|
---|
92 | case 9: // tab
|
---|
93 | case 10: // nl
|
---|
94 | case 32: // space
|
---|
95 | case 40: // (
|
---|
96 | case 41: // )
|
---|
97 | countWhiteOrBracket++;
|
---|
98 | break;
|
---|
99 | case 34:
|
---|
100 | countQuotation++;
|
---|
101 | break;
|
---|
102 | case 39:
|
---|
103 | countApostrophe++;
|
---|
104 | break;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | if (countWhiteOrBracket < 2) {
|
---|
108 | return str.replace(/[\n\t ()'"\\]/g, m => `\\${m}`);
|
---|
109 | } else if (countQuotation <= countApostrophe) {
|
---|
110 | return `"${str.replace(/[\n"\\]/g, m => `\\${m}`)}"`;
|
---|
111 | }
|
---|
112 | return `'${str.replace(/[\n'\\]/g, m => `\\${m}`)}'`;
|
---|
113 | };
|
---|
114 |
|
---|
115 | CssUrlDependency.Template = class CssUrlDependencyTemplate extends (
|
---|
116 | ModuleDependency.Template
|
---|
117 | ) {
|
---|
118 | /**
|
---|
119 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
120 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
121 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
122 | * @returns {void}
|
---|
123 | */
|
---|
124 | apply(
|
---|
125 | dependency,
|
---|
126 | source,
|
---|
127 | { moduleGraph, runtimeTemplate, codeGenerationResults }
|
---|
128 | ) {
|
---|
129 | const dep = /** @type {CssUrlDependency} */ (dependency);
|
---|
130 | const module = /** @type {Module} */ (moduleGraph.getModule(dep));
|
---|
131 |
|
---|
132 | /** @type {string | undefined} */
|
---|
133 | let newValue;
|
---|
134 |
|
---|
135 | switch (dep.urlType) {
|
---|
136 | case "string":
|
---|
137 | newValue = cssEscapeString(
|
---|
138 | this.assetUrl({
|
---|
139 | module,
|
---|
140 | codeGenerationResults
|
---|
141 | })
|
---|
142 | );
|
---|
143 | break;
|
---|
144 | case "url":
|
---|
145 | newValue = `url(${cssEscapeString(
|
---|
146 | this.assetUrl({
|
---|
147 | module,
|
---|
148 | codeGenerationResults
|
---|
149 | })
|
---|
150 | )})`;
|
---|
151 | break;
|
---|
152 | case "src":
|
---|
153 | newValue = `src(${cssEscapeString(
|
---|
154 | this.assetUrl({
|
---|
155 | module,
|
---|
156 | codeGenerationResults
|
---|
157 | })
|
---|
158 | )})`;
|
---|
159 | break;
|
---|
160 | }
|
---|
161 |
|
---|
162 | source.replace(
|
---|
163 | dep.range[0],
|
---|
164 | dep.range[1] - 1,
|
---|
165 | /** @type {string} */ (newValue)
|
---|
166 | );
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * @param {object} options options object
|
---|
171 | * @param {Module} options.module the module
|
---|
172 | * @param {RuntimeSpec=} options.runtime runtime
|
---|
173 | * @param {CodeGenerationResults} options.codeGenerationResults the code generation results
|
---|
174 | * @returns {string} the url of the asset
|
---|
175 | */
|
---|
176 | assetUrl({ runtime, module, codeGenerationResults }) {
|
---|
177 | if (!module) {
|
---|
178 | return "data:,";
|
---|
179 | }
|
---|
180 | const codeGen = codeGenerationResults.get(module, runtime);
|
---|
181 | const data =
|
---|
182 | /** @type {NonNullable<CodeGenerationResult["data"]>} */
|
---|
183 | (codeGen.data);
|
---|
184 | if (!data) return "data:,";
|
---|
185 | const url = data.get("url");
|
---|
186 | if (!url || !url["css-url"]) return "data:,";
|
---|
187 | return url["css-url"];
|
---|
188 | }
|
---|
189 | };
|
---|
190 |
|
---|
191 | makeSerializable(CssUrlDependency, "webpack/lib/dependencies/CssUrlDependency");
|
---|
192 |
|
---|
193 | CssUrlDependency.PUBLIC_PATH_AUTO = "__WEBPACK_CSS_PUBLIC_PATH_AUTO__";
|
---|
194 |
|
---|
195 | module.exports = CssUrlDependency;
|
---|