[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
| 10 | if (k2 === undefined) k2 = k;
|
---|
| 11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
| 12 | }) : (function(o, m, k, k2) {
|
---|
| 13 | if (k2 === undefined) k2 = k;
|
---|
| 14 | o[k2] = m[k];
|
---|
| 15 | }));
|
---|
| 16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
| 17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
| 18 | }) : function(o, v) {
|
---|
| 19 | o["default"] = v;
|
---|
| 20 | });
|
---|
| 21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
| 22 | if (mod && mod.__esModule) return mod;
|
---|
| 23 | var result = {};
|
---|
| 24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
| 25 | __setModuleDefault(result, mod);
|
---|
| 26 | return result;
|
---|
| 27 | };
|
---|
| 28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 29 | exports.postcss = void 0;
|
---|
| 30 | const loader_utils_1 = require("loader-utils");
|
---|
| 31 | const path = __importStar(require("path"));
|
---|
| 32 | const url = __importStar(require("url"));
|
---|
| 33 | function wrapUrl(url) {
|
---|
| 34 | let wrappedUrl;
|
---|
| 35 | const hasSingleQuotes = url.indexOf("'") >= 0;
|
---|
| 36 | if (hasSingleQuotes) {
|
---|
| 37 | wrappedUrl = `"${url}"`;
|
---|
| 38 | }
|
---|
| 39 | else {
|
---|
| 40 | wrappedUrl = `'${url}'`;
|
---|
| 41 | }
|
---|
| 42 | return `url(${wrappedUrl})`;
|
---|
| 43 | }
|
---|
| 44 | async function resolve(file, base, resolver) {
|
---|
| 45 | try {
|
---|
| 46 | return await resolver('./' + file, base);
|
---|
| 47 | }
|
---|
| 48 | catch {
|
---|
| 49 | return resolver(file, base);
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | exports.postcss = true;
|
---|
| 53 | function default_1(options) {
|
---|
| 54 | if (!options) {
|
---|
| 55 | throw new Error('No options were specified to "postcss-cli-resources".');
|
---|
| 56 | }
|
---|
| 57 | const { deployUrl = '', resourcesOutputPath = '', filename, loader, emitFile, extracted, } = options;
|
---|
| 58 | const process = async (inputUrl, context, resourceCache) => {
|
---|
| 59 | // If root-relative, absolute or protocol relative url, leave as is
|
---|
| 60 | if (/^((?:\w+:)?\/\/|data:|chrome:|#)/.test(inputUrl)) {
|
---|
| 61 | return inputUrl;
|
---|
| 62 | }
|
---|
| 63 | if (/^\//.test(inputUrl)) {
|
---|
| 64 | return inputUrl;
|
---|
| 65 | }
|
---|
| 66 | // If starts with a caret, remove and return remainder
|
---|
| 67 | // this supports bypassing asset processing
|
---|
| 68 | if (inputUrl.startsWith('^')) {
|
---|
| 69 | return inputUrl.substr(1);
|
---|
| 70 | }
|
---|
| 71 | const cacheKey = path.resolve(context, inputUrl);
|
---|
| 72 | const cachedUrl = resourceCache.get(cacheKey);
|
---|
| 73 | if (cachedUrl) {
|
---|
| 74 | return cachedUrl;
|
---|
| 75 | }
|
---|
| 76 | if (inputUrl.startsWith('~')) {
|
---|
| 77 | inputUrl = inputUrl.substr(1);
|
---|
| 78 | }
|
---|
| 79 | const { pathname, hash, search } = url.parse(inputUrl.replace(/\\/g, '/'));
|
---|
| 80 | const resolver = (file, base) => new Promise((resolve, reject) => {
|
---|
| 81 | loader.resolve(base, decodeURI(file), (err, result) => {
|
---|
| 82 | if (err) {
|
---|
| 83 | reject(err);
|
---|
| 84 | return;
|
---|
| 85 | }
|
---|
| 86 | resolve(result);
|
---|
| 87 | });
|
---|
| 88 | });
|
---|
| 89 | const result = await resolve(pathname, context, resolver);
|
---|
| 90 | return new Promise((resolve, reject) => {
|
---|
| 91 | loader.fs.readFile(result, (err, content) => {
|
---|
| 92 | if (err) {
|
---|
| 93 | reject(err);
|
---|
| 94 | return;
|
---|
| 95 | }
|
---|
| 96 | let outputPath = loader_utils_1.interpolateName({ resourcePath: result }, filename(result), {
|
---|
| 97 | content,
|
---|
| 98 | context: loader.context || loader.rootContext,
|
---|
| 99 | }).replace(/\\|\//g, '-');
|
---|
| 100 | if (resourcesOutputPath) {
|
---|
| 101 | outputPath = path.posix.join(resourcesOutputPath, outputPath);
|
---|
| 102 | }
|
---|
| 103 | loader.addDependency(result);
|
---|
| 104 | if (emitFile) {
|
---|
| 105 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
---|
| 106 | loader.emitFile(outputPath, content, undefined, { sourceFilename: result });
|
---|
| 107 | }
|
---|
| 108 | let outputUrl = outputPath.replace(/\\/g, '/');
|
---|
| 109 | if (hash || search) {
|
---|
| 110 | outputUrl = url.format({ pathname: outputUrl, hash, search });
|
---|
| 111 | }
|
---|
| 112 | if (deployUrl && !extracted) {
|
---|
| 113 | outputUrl = url.resolve(deployUrl, outputUrl);
|
---|
| 114 | }
|
---|
| 115 | resourceCache.set(cacheKey, outputUrl);
|
---|
| 116 | resolve(outputUrl);
|
---|
| 117 | });
|
---|
| 118 | });
|
---|
| 119 | };
|
---|
| 120 | const resourceCache = new Map();
|
---|
| 121 | const processed = Symbol('postcss-cli-resources');
|
---|
| 122 | return {
|
---|
| 123 | postcssPlugin: 'postcss-cli-resources',
|
---|
| 124 | async Declaration(decl) {
|
---|
| 125 | if (!decl.value.includes('url') || processed in decl) {
|
---|
| 126 | return;
|
---|
| 127 | }
|
---|
| 128 | const value = decl.value;
|
---|
| 129 | const urlRegex = /url\(\s*(?:"([^"]+)"|'([^']+)'|(.+?))\s*\)/g;
|
---|
| 130 | const segments = [];
|
---|
| 131 | let match;
|
---|
| 132 | let lastIndex = 0;
|
---|
| 133 | let modified = false;
|
---|
| 134 | // We want to load it relative to the file that imports
|
---|
| 135 | const inputFile = decl.source && decl.source.input.file;
|
---|
| 136 | const context = (inputFile && path.dirname(inputFile)) || loader.context;
|
---|
| 137 | // eslint-disable-next-line no-cond-assign
|
---|
| 138 | while ((match = urlRegex.exec(value))) {
|
---|
| 139 | const originalUrl = match[1] || match[2] || match[3];
|
---|
| 140 | let processedUrl;
|
---|
| 141 | try {
|
---|
| 142 | processedUrl = await process(originalUrl, context, resourceCache);
|
---|
| 143 | }
|
---|
| 144 | catch (err) {
|
---|
| 145 | loader.emitError(decl.error(err.message, { word: originalUrl }));
|
---|
| 146 | continue;
|
---|
| 147 | }
|
---|
| 148 | if (lastIndex < match.index) {
|
---|
| 149 | segments.push(value.slice(lastIndex, match.index));
|
---|
| 150 | }
|
---|
| 151 | if (!processedUrl || originalUrl === processedUrl) {
|
---|
| 152 | segments.push(match[0]);
|
---|
| 153 | }
|
---|
| 154 | else {
|
---|
| 155 | segments.push(wrapUrl(processedUrl));
|
---|
| 156 | modified = true;
|
---|
| 157 | }
|
---|
| 158 | lastIndex = match.index + match[0].length;
|
---|
| 159 | }
|
---|
| 160 | if (lastIndex < value.length) {
|
---|
| 161 | segments.push(value.slice(lastIndex));
|
---|
| 162 | }
|
---|
| 163 | if (modified) {
|
---|
| 164 | decl.value = segments.join('');
|
---|
| 165 | }
|
---|
| 166 | decl[processed] = true;
|
---|
| 167 | },
|
---|
| 168 | };
|
---|
| 169 | }
|
---|
| 170 | exports.default = default_1;
|
---|