[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = void 0;
|
---|
| 7 |
|
---|
| 8 | var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
---|
| 9 |
|
---|
| 10 | var _utils = require("../utils");
|
---|
| 11 |
|
---|
| 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 13 |
|
---|
| 14 | const isUrlFunc = /url/i;
|
---|
| 15 | const isImageSetFunc = /^(?:-webkit-)?image-set$/i;
|
---|
| 16 | const needParseDeclaration = /(?:url|(?:-webkit-)?image-set)\(/i;
|
---|
| 17 |
|
---|
| 18 | function getNodeFromUrlFunc(node) {
|
---|
| 19 | return node.nodes && node.nodes[0];
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | function getWebpackIgnoreCommentValue(index, nodes, inBetween) {
|
---|
| 23 | if (index === 0 && typeof inBetween !== "undefined") {
|
---|
| 24 | return inBetween;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | let prevValueNode = nodes[index - 1];
|
---|
| 28 |
|
---|
| 29 | if (!prevValueNode) {
|
---|
| 30 | // eslint-disable-next-line consistent-return
|
---|
| 31 | return;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | if (prevValueNode.type === "space") {
|
---|
| 35 | if (!nodes[index - 2]) {
|
---|
| 36 | // eslint-disable-next-line consistent-return
|
---|
| 37 | return;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | prevValueNode = nodes[index - 2];
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | if (prevValueNode.type !== "comment") {
|
---|
| 44 | // eslint-disable-next-line consistent-return
|
---|
| 45 | return;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | const matched = prevValueNode.value.match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
|
---|
| 49 | return matched && matched[2] === "true";
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | function shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL) {
|
---|
| 53 | if (url.length === 0) {
|
---|
| 54 | result.warn(`Unable to find uri in '${declaration.toString()}'`, {
|
---|
| 55 | node: declaration
|
---|
| 56 | });
|
---|
| 57 | return false;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | if ((0, _utils.isDataUrl)(url) && isSupportDataURLInNewURL) {
|
---|
| 61 | try {
|
---|
| 62 | decodeURIComponent(url);
|
---|
| 63 | } catch (ignoreError) {
|
---|
| 64 | return false;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return true;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | if (!(0, _utils.isUrlRequestable)(url)) {
|
---|
| 71 | return false;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | return true;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | function parseDeclaration(declaration, key, result, isSupportDataURLInNewURL) {
|
---|
| 78 | if (!needParseDeclaration.test(declaration[key])) {
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | const parsed = (0, _postcssValueParser.default)(declaration.raws && declaration.raws.value && declaration.raws.value.raw ? declaration.raws.value.raw : declaration[key]);
|
---|
| 83 | let inBetween;
|
---|
| 84 |
|
---|
| 85 | if (declaration.raws && declaration.raws.between) {
|
---|
| 86 | const lastCommentIndex = declaration.raws.between.lastIndexOf("/*");
|
---|
| 87 | const matched = declaration.raws.between.slice(lastCommentIndex).match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
|
---|
| 88 |
|
---|
| 89 | if (matched) {
|
---|
| 90 | inBetween = matched[2] === "true";
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | let isIgnoreOnDeclaration = false;
|
---|
| 95 | const prevNode = declaration.prev();
|
---|
| 96 |
|
---|
| 97 | if (prevNode && prevNode.type === "comment") {
|
---|
| 98 | const matched = prevNode.text.match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
|
---|
| 99 |
|
---|
| 100 | if (matched) {
|
---|
| 101 | isIgnoreOnDeclaration = matched[2] === "true";
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | let needIgnore;
|
---|
| 106 | const parsedURLs = [];
|
---|
| 107 | parsed.walk((valueNode, index, valueNodes) => {
|
---|
| 108 | if (valueNode.type !== "function") {
|
---|
| 109 | return;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | if (isUrlFunc.test(valueNode.value)) {
|
---|
| 113 | needIgnore = getWebpackIgnoreCommentValue(index, valueNodes, inBetween);
|
---|
| 114 |
|
---|
| 115 | if (isIgnoreOnDeclaration && typeof needIgnore === "undefined" || needIgnore) {
|
---|
| 116 | if (needIgnore) {
|
---|
| 117 | // eslint-disable-next-line no-undefined
|
---|
| 118 | needIgnore = undefined;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | return;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | const {
|
---|
| 125 | nodes
|
---|
| 126 | } = valueNode;
|
---|
| 127 | const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
|
---|
| 128 | let url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
|
---|
| 129 | url = (0, _utils.normalizeUrl)(url, isStringValue); // Do not traverse inside `url`
|
---|
| 130 |
|
---|
| 131 | if (!shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL)) {
|
---|
| 132 | // eslint-disable-next-line consistent-return
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | const queryParts = url.split("!");
|
---|
| 137 | let prefix;
|
---|
| 138 |
|
---|
| 139 | if (queryParts.length > 1) {
|
---|
| 140 | url = queryParts.pop();
|
---|
| 141 | prefix = queryParts.join("!");
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | parsedURLs.push({
|
---|
| 145 | declaration,
|
---|
| 146 | parsed,
|
---|
| 147 | node: getNodeFromUrlFunc(valueNode),
|
---|
| 148 | prefix,
|
---|
| 149 | url,
|
---|
| 150 | needQuotes: false
|
---|
| 151 | }); // eslint-disable-next-line consistent-return
|
---|
| 152 |
|
---|
| 153 | return false;
|
---|
| 154 | } else if (isImageSetFunc.test(valueNode.value)) {
|
---|
| 155 | for (const [innerIndex, nNode] of valueNode.nodes.entries()) {
|
---|
| 156 | const {
|
---|
| 157 | type,
|
---|
| 158 | value
|
---|
| 159 | } = nNode;
|
---|
| 160 |
|
---|
| 161 | if (type === "function" && isUrlFunc.test(value)) {
|
---|
| 162 | needIgnore = getWebpackIgnoreCommentValue(innerIndex, valueNode.nodes);
|
---|
| 163 |
|
---|
| 164 | if (isIgnoreOnDeclaration && typeof needIgnore === "undefined" || needIgnore) {
|
---|
| 165 | if (needIgnore) {
|
---|
| 166 | // eslint-disable-next-line no-undefined
|
---|
| 167 | needIgnore = undefined;
|
---|
| 168 | } // eslint-disable-next-line no-continue
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | continue;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | const {
|
---|
| 175 | nodes
|
---|
| 176 | } = nNode;
|
---|
| 177 | const isStringValue = nodes.length !== 0 && nodes[0].type === "string";
|
---|
| 178 | let url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
|
---|
| 179 | url = (0, _utils.normalizeUrl)(url, isStringValue); // Do not traverse inside `url`
|
---|
| 180 |
|
---|
| 181 | if (!shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL)) {
|
---|
| 182 | // eslint-disable-next-line consistent-return
|
---|
| 183 | return false;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | const queryParts = url.split("!");
|
---|
| 187 | let prefix;
|
---|
| 188 |
|
---|
| 189 | if (queryParts.length > 1) {
|
---|
| 190 | url = queryParts.pop();
|
---|
| 191 | prefix = queryParts.join("!");
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | parsedURLs.push({
|
---|
| 195 | declaration,
|
---|
| 196 | parsed,
|
---|
| 197 | node: getNodeFromUrlFunc(nNode),
|
---|
| 198 | prefix,
|
---|
| 199 | url,
|
---|
| 200 | needQuotes: false
|
---|
| 201 | });
|
---|
| 202 | } else if (type === "string") {
|
---|
| 203 | needIgnore = getWebpackIgnoreCommentValue(innerIndex, valueNode.nodes);
|
---|
| 204 |
|
---|
| 205 | if (isIgnoreOnDeclaration && typeof needIgnore === "undefined" || needIgnore) {
|
---|
| 206 | if (needIgnore) {
|
---|
| 207 | // eslint-disable-next-line no-undefined
|
---|
| 208 | needIgnore = undefined;
|
---|
| 209 | } // eslint-disable-next-line no-continue
|
---|
| 210 |
|
---|
| 211 |
|
---|
| 212 | continue;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | let url = (0, _utils.normalizeUrl)(value, true); // Do not traverse inside `url`
|
---|
| 216 |
|
---|
| 217 | if (!shouldHandleURL(url, declaration, result, isSupportDataURLInNewURL)) {
|
---|
| 218 | // eslint-disable-next-line consistent-return
|
---|
| 219 | return false;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | const queryParts = url.split("!");
|
---|
| 223 | let prefix;
|
---|
| 224 |
|
---|
| 225 | if (queryParts.length > 1) {
|
---|
| 226 | url = queryParts.pop();
|
---|
| 227 | prefix = queryParts.join("!");
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | parsedURLs.push({
|
---|
| 231 | declaration,
|
---|
| 232 | parsed,
|
---|
| 233 | node: nNode,
|
---|
| 234 | prefix,
|
---|
| 235 | url,
|
---|
| 236 | needQuotes: true
|
---|
| 237 | });
|
---|
| 238 | }
|
---|
| 239 | } // Do not traverse inside `image-set`
|
---|
| 240 | // eslint-disable-next-line consistent-return
|
---|
| 241 |
|
---|
| 242 |
|
---|
| 243 | return false;
|
---|
| 244 | }
|
---|
| 245 | }); // eslint-disable-next-line consistent-return
|
---|
| 246 |
|
---|
| 247 | return parsedURLs;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | const plugin = (options = {}) => {
|
---|
| 251 | return {
|
---|
| 252 | postcssPlugin: "postcss-url-parser",
|
---|
| 253 |
|
---|
| 254 | prepare(result) {
|
---|
| 255 | const parsedDeclarations = [];
|
---|
| 256 | return {
|
---|
| 257 | Declaration(declaration) {
|
---|
| 258 | const {
|
---|
| 259 | isSupportDataURLInNewURL
|
---|
| 260 | } = options;
|
---|
| 261 | const parsedURL = parseDeclaration(declaration, "value", result, isSupportDataURLInNewURL);
|
---|
| 262 |
|
---|
| 263 | if (!parsedURL) {
|
---|
| 264 | return;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | parsedDeclarations.push(...parsedURL);
|
---|
| 268 | },
|
---|
| 269 |
|
---|
| 270 | async OnceExit() {
|
---|
| 271 | if (parsedDeclarations.length === 0) {
|
---|
| 272 | return;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | const resolvedDeclarations = await Promise.all(parsedDeclarations.map(async parsedDeclaration => {
|
---|
| 276 | const {
|
---|
| 277 | url
|
---|
| 278 | } = parsedDeclaration;
|
---|
| 279 |
|
---|
| 280 | if (options.filter) {
|
---|
| 281 | const needKeep = await options.filter(url);
|
---|
| 282 |
|
---|
| 283 | if (!needKeep) {
|
---|
| 284 | // eslint-disable-next-line consistent-return
|
---|
| 285 | return;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | if ((0, _utils.isDataUrl)(url)) {
|
---|
| 290 | // eslint-disable-next-line consistent-return
|
---|
| 291 | return parsedDeclaration;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | const splittedUrl = url.split(/(\?)?#/);
|
---|
| 295 | const [pathname, query, hashOrQuery] = splittedUrl;
|
---|
| 296 | let hash = query ? "?" : "";
|
---|
| 297 | hash += hashOrQuery ? `#${hashOrQuery}` : "";
|
---|
| 298 | const {
|
---|
| 299 | needToResolveURL,
|
---|
| 300 | rootContext
|
---|
| 301 | } = options;
|
---|
| 302 | const request = (0, _utils.requestify)(pathname, rootContext, needToResolveURL);
|
---|
| 303 |
|
---|
| 304 | if (!needToResolveURL) {
|
---|
| 305 | // eslint-disable-next-line consistent-return
|
---|
| 306 | return { ...parsedDeclaration,
|
---|
| 307 | url: request,
|
---|
| 308 | hash
|
---|
| 309 | };
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | const {
|
---|
| 313 | resolver,
|
---|
| 314 | context
|
---|
| 315 | } = options;
|
---|
| 316 | const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([request, url])]);
|
---|
| 317 |
|
---|
| 318 | if (!resolvedUrl) {
|
---|
| 319 | // eslint-disable-next-line consistent-return
|
---|
| 320 | return;
|
---|
| 321 | } // eslint-disable-next-line consistent-return
|
---|
| 322 |
|
---|
| 323 |
|
---|
| 324 | return { ...parsedDeclaration,
|
---|
| 325 | url: resolvedUrl,
|
---|
| 326 | hash
|
---|
| 327 | };
|
---|
| 328 | }));
|
---|
| 329 | const urlToNameMap = new Map();
|
---|
| 330 | const urlToReplacementMap = new Map();
|
---|
| 331 | let hasUrlImportHelper = false;
|
---|
| 332 |
|
---|
| 333 | for (let index = 0; index <= resolvedDeclarations.length - 1; index++) {
|
---|
| 334 | const item = resolvedDeclarations[index];
|
---|
| 335 |
|
---|
| 336 | if (!item) {
|
---|
| 337 | // eslint-disable-next-line no-continue
|
---|
| 338 | continue;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | if (!hasUrlImportHelper) {
|
---|
| 342 | options.imports.push({
|
---|
| 343 | type: "get_url_import",
|
---|
| 344 | importName: "___CSS_LOADER_GET_URL_IMPORT___",
|
---|
| 345 | url: options.urlHandler(require.resolve("../runtime/getUrl.js")),
|
---|
| 346 | index: -1
|
---|
| 347 | });
|
---|
| 348 | hasUrlImportHelper = true;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | const {
|
---|
| 352 | url,
|
---|
| 353 | prefix
|
---|
| 354 | } = item;
|
---|
| 355 | const newUrl = prefix ? `${prefix}!${url}` : url;
|
---|
| 356 | let importName = urlToNameMap.get(newUrl);
|
---|
| 357 |
|
---|
| 358 | if (!importName) {
|
---|
| 359 | importName = `___CSS_LOADER_URL_IMPORT_${urlToNameMap.size}___`;
|
---|
| 360 | urlToNameMap.set(newUrl, importName);
|
---|
| 361 | options.imports.push({
|
---|
| 362 | type: "url",
|
---|
| 363 | importName,
|
---|
| 364 | url: options.needToResolveURL ? options.urlHandler(newUrl) : JSON.stringify(newUrl),
|
---|
| 365 | index
|
---|
| 366 | });
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | const {
|
---|
| 370 | hash,
|
---|
| 371 | needQuotes
|
---|
| 372 | } = item;
|
---|
| 373 | const replacementKey = JSON.stringify({
|
---|
| 374 | newUrl,
|
---|
| 375 | hash,
|
---|
| 376 | needQuotes
|
---|
| 377 | });
|
---|
| 378 | let replacementName = urlToReplacementMap.get(replacementKey);
|
---|
| 379 |
|
---|
| 380 | if (!replacementName) {
|
---|
| 381 | replacementName = `___CSS_LOADER_URL_REPLACEMENT_${urlToReplacementMap.size}___`;
|
---|
| 382 | urlToReplacementMap.set(replacementKey, replacementName);
|
---|
| 383 | options.replacements.push({
|
---|
| 384 | replacementName,
|
---|
| 385 | importName,
|
---|
| 386 | hash,
|
---|
| 387 | needQuotes
|
---|
| 388 | });
|
---|
| 389 | } // eslint-disable-next-line no-param-reassign
|
---|
| 390 |
|
---|
| 391 |
|
---|
| 392 | item.node.type = "word"; // eslint-disable-next-line no-param-reassign
|
---|
| 393 |
|
---|
| 394 | item.node.value = replacementName; // eslint-disable-next-line no-param-reassign
|
---|
| 395 |
|
---|
| 396 | item.declaration.value = item.parsed.toString();
|
---|
| 397 | }
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | };
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | };
|
---|
| 404 | };
|
---|
| 405 |
|
---|
| 406 | plugin.postcss = true;
|
---|
| 407 | var _default = plugin;
|
---|
| 408 | exports.default = _default; |
---|