[57e58a3] | 1 | import { P as getDefaultExportFromCjs } from './dep-CfG9u7Cn.js';
|
---|
| 2 | import require$$0 from 'path';
|
---|
| 3 | import { l as lib } from './dep-3RmXg9uo.js';
|
---|
| 4 |
|
---|
| 5 | import { createRequire as __cjs_createRequire } from 'node:module';
|
---|
| 6 |
|
---|
| 7 | const __require = __cjs_createRequire(import.meta.url);
|
---|
| 8 | function _mergeNamespaces(n, m) {
|
---|
| 9 | for (var i = 0; i < m.length; i++) {
|
---|
| 10 | var e = m[i];
|
---|
| 11 | if (typeof e !== 'string' && !Array.isArray(e)) { for (var k in e) {
|
---|
| 12 | if (k !== 'default' && !(k in n)) {
|
---|
| 13 | n[k] = e[k];
|
---|
| 14 | }
|
---|
| 15 | } }
|
---|
| 16 | }
|
---|
| 17 | return n;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | var formatImportPrelude$2 = function formatImportPrelude(layer, media, supports) {
|
---|
| 21 | const parts = [];
|
---|
| 22 |
|
---|
| 23 | if (typeof layer !== "undefined") {
|
---|
| 24 | let layerParams = "layer";
|
---|
| 25 | if (layer) {
|
---|
| 26 | layerParams = `layer(${layer})`;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | parts.push(layerParams);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | if (typeof supports !== "undefined") {
|
---|
| 33 | parts.push(`supports(${supports})`);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | if (typeof media !== "undefined") {
|
---|
| 37 | parts.push(media);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | return parts.join(" ")
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | const formatImportPrelude$1 = formatImportPrelude$2;
|
---|
| 44 |
|
---|
| 45 | // Base64 encode an import with conditions
|
---|
| 46 | // The order of conditions is important and is interleaved with cascade layer declarations
|
---|
| 47 | // Each group of conditions and cascade layers needs to be interpreted in order
|
---|
| 48 | // To achieve this we create a list of base64 encoded imports, where each import contains a stylesheet with another import.
|
---|
| 49 | // Each import can define a single group of conditions and a single cascade layer.
|
---|
| 50 | var base64EncodedImport = function base64EncodedConditionalImport(prelude, conditions) {
|
---|
| 51 | conditions.reverse();
|
---|
| 52 | const first = conditions.pop();
|
---|
| 53 | let params = `${prelude} ${formatImportPrelude$1(
|
---|
| 54 | first.layer,
|
---|
| 55 | first.media,
|
---|
| 56 | first.supports,
|
---|
| 57 | )}`;
|
---|
| 58 |
|
---|
| 59 | for (const condition of conditions) {
|
---|
| 60 | params = `'data:text/css;base64,${Buffer.from(`@import ${params}`).toString(
|
---|
| 61 | "base64",
|
---|
| 62 | )}' ${formatImportPrelude$1(
|
---|
| 63 | condition.layer,
|
---|
| 64 | condition.media,
|
---|
| 65 | condition.supports,
|
---|
| 66 | )}`;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | return params
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | const base64EncodedConditionalImport = base64EncodedImport;
|
---|
| 73 |
|
---|
| 74 | var applyConditions$1 = function applyConditions(bundle, atRule) {
|
---|
| 75 | bundle.forEach(stmt => {
|
---|
| 76 | if (
|
---|
| 77 | stmt.type === "charset" ||
|
---|
| 78 | stmt.type === "warning" ||
|
---|
| 79 | !stmt.conditions?.length
|
---|
| 80 | ) {
|
---|
| 81 | return
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (stmt.type === "import") {
|
---|
| 85 | stmt.node.params = base64EncodedConditionalImport(
|
---|
| 86 | stmt.fullUri,
|
---|
| 87 | stmt.conditions,
|
---|
| 88 | );
|
---|
| 89 | return
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | const { nodes } = stmt;
|
---|
| 93 | const { parent } = nodes[0];
|
---|
| 94 |
|
---|
| 95 | const atRules = [];
|
---|
| 96 |
|
---|
| 97 | // Convert conditions to at-rules
|
---|
| 98 | for (const condition of stmt.conditions) {
|
---|
| 99 | if (typeof condition.media !== "undefined") {
|
---|
| 100 | const mediaNode = atRule({
|
---|
| 101 | name: "media",
|
---|
| 102 | params: condition.media,
|
---|
| 103 | source: parent.source,
|
---|
| 104 | });
|
---|
| 105 |
|
---|
| 106 | atRules.push(mediaNode);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | if (typeof condition.supports !== "undefined") {
|
---|
| 110 | const supportsNode = atRule({
|
---|
| 111 | name: "supports",
|
---|
| 112 | params: `(${condition.supports})`,
|
---|
| 113 | source: parent.source,
|
---|
| 114 | });
|
---|
| 115 |
|
---|
| 116 | atRules.push(supportsNode);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | if (typeof condition.layer !== "undefined") {
|
---|
| 120 | const layerNode = atRule({
|
---|
| 121 | name: "layer",
|
---|
| 122 | params: condition.layer,
|
---|
| 123 | source: parent.source,
|
---|
| 124 | });
|
---|
| 125 |
|
---|
| 126 | atRules.push(layerNode);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | // Add nodes to AST
|
---|
| 131 | const outerAtRule = atRules.shift();
|
---|
| 132 | const innerAtRule = atRules.reduce((previous, next) => {
|
---|
| 133 | previous.append(next);
|
---|
| 134 | return next
|
---|
| 135 | }, outerAtRule);
|
---|
| 136 |
|
---|
| 137 | parent.insertBefore(nodes[0], outerAtRule);
|
---|
| 138 |
|
---|
| 139 | // remove nodes
|
---|
| 140 | nodes.forEach(node => {
|
---|
| 141 | node.parent = undefined;
|
---|
| 142 | });
|
---|
| 143 |
|
---|
| 144 | // better output
|
---|
| 145 | nodes[0].raws.before = nodes[0].raws.before || "\n";
|
---|
| 146 |
|
---|
| 147 | // wrap new rules with media query and/or layer at rule
|
---|
| 148 | innerAtRule.append(nodes);
|
---|
| 149 |
|
---|
| 150 | stmt.type = "nodes";
|
---|
| 151 | stmt.nodes = [outerAtRule];
|
---|
| 152 | delete stmt.node;
|
---|
| 153 | });
|
---|
| 154 | };
|
---|
| 155 |
|
---|
| 156 | var applyRaws$1 = function applyRaws(bundle) {
|
---|
| 157 | bundle.forEach((stmt, index) => {
|
---|
| 158 | if (index === 0) return
|
---|
| 159 |
|
---|
| 160 | if (stmt.parent) {
|
---|
| 161 | const { before } = stmt.parent.node.raws;
|
---|
| 162 | if (stmt.type === "nodes") stmt.nodes[0].raws.before = before;
|
---|
| 163 | else stmt.node.raws.before = before;
|
---|
| 164 | } else if (stmt.type === "nodes") {
|
---|
| 165 | stmt.nodes[0].raws.before = stmt.nodes[0].raws.before || "\n";
|
---|
| 166 | }
|
---|
| 167 | });
|
---|
| 168 | };
|
---|
| 169 |
|
---|
| 170 | var applyStyles$1 = function applyStyles(bundle, styles) {
|
---|
| 171 | styles.nodes = [];
|
---|
| 172 |
|
---|
| 173 | // Strip additional statements.
|
---|
| 174 | bundle.forEach(stmt => {
|
---|
| 175 | if (["charset", "import"].includes(stmt.type)) {
|
---|
| 176 | stmt.node.parent = undefined;
|
---|
| 177 | styles.append(stmt.node);
|
---|
| 178 | } else if (stmt.type === "nodes") {
|
---|
| 179 | stmt.nodes.forEach(node => {
|
---|
| 180 | node.parent = undefined;
|
---|
| 181 | styles.append(node);
|
---|
| 182 | });
|
---|
| 183 | }
|
---|
| 184 | });
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 | const anyDataURLRegexp = /^data:text\/css(?:;(base64|plain))?,/i;
|
---|
| 188 | const base64DataURLRegexp = /^data:text\/css;base64,/i;
|
---|
| 189 | const plainDataURLRegexp = /^data:text\/css;plain,/i;
|
---|
| 190 |
|
---|
| 191 | function isValid(url) {
|
---|
| 192 | return anyDataURLRegexp.test(url)
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | function contents(url) {
|
---|
| 196 | if (base64DataURLRegexp.test(url)) {
|
---|
| 197 | // "data:text/css;base64,".length === 21
|
---|
| 198 | return Buffer.from(url.slice(21), "base64").toString()
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | if (plainDataURLRegexp.test(url)) {
|
---|
| 202 | // "data:text/css;plain,".length === 20
|
---|
| 203 | return decodeURIComponent(url.slice(20))
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | // "data:text/css,".length === 14
|
---|
| 207 | return decodeURIComponent(url.slice(14))
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | var dataUrl = {
|
---|
| 211 | isValid,
|
---|
| 212 | contents,
|
---|
| 213 | };
|
---|
| 214 |
|
---|
| 215 | // external tooling
|
---|
| 216 | const valueParser = lib;
|
---|
| 217 |
|
---|
| 218 | // extended tooling
|
---|
| 219 | const { stringify } = valueParser;
|
---|
| 220 |
|
---|
| 221 | var parseStatements$1 = function parseStatements(result, styles, conditions, from) {
|
---|
| 222 | const statements = [];
|
---|
| 223 | let nodes = [];
|
---|
| 224 |
|
---|
| 225 | styles.each(node => {
|
---|
| 226 | let stmt;
|
---|
| 227 | if (node.type === "atrule") {
|
---|
| 228 | if (node.name === "import")
|
---|
| 229 | stmt = parseImport(result, node, conditions, from);
|
---|
| 230 | else if (node.name === "charset")
|
---|
| 231 | stmt = parseCharset(result, node, conditions, from);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | if (stmt) {
|
---|
| 235 | if (nodes.length) {
|
---|
| 236 | statements.push({
|
---|
| 237 | type: "nodes",
|
---|
| 238 | nodes,
|
---|
| 239 | conditions: [...conditions],
|
---|
| 240 | from,
|
---|
| 241 | });
|
---|
| 242 | nodes = [];
|
---|
| 243 | }
|
---|
| 244 | statements.push(stmt);
|
---|
| 245 | } else nodes.push(node);
|
---|
| 246 | });
|
---|
| 247 |
|
---|
| 248 | if (nodes.length) {
|
---|
| 249 | statements.push({
|
---|
| 250 | type: "nodes",
|
---|
| 251 | nodes,
|
---|
| 252 | conditions: [...conditions],
|
---|
| 253 | from,
|
---|
| 254 | });
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | return statements
|
---|
| 258 | };
|
---|
| 259 |
|
---|
| 260 | function parseCharset(result, atRule, conditions, from) {
|
---|
| 261 | if (atRule.prev()) {
|
---|
| 262 | return result.warn("@charset must precede all other statements", {
|
---|
| 263 | node: atRule,
|
---|
| 264 | })
|
---|
| 265 | }
|
---|
| 266 | return {
|
---|
| 267 | type: "charset",
|
---|
| 268 | node: atRule,
|
---|
| 269 | conditions: [...conditions],
|
---|
| 270 | from,
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | function parseImport(result, atRule, conditions, from) {
|
---|
| 275 | let prev = atRule.prev();
|
---|
| 276 |
|
---|
| 277 | // `@import` statements may follow other `@import` statements.
|
---|
| 278 | if (prev) {
|
---|
| 279 | do {
|
---|
| 280 | if (
|
---|
| 281 | prev.type === "comment" ||
|
---|
| 282 | (prev.type === "atrule" && prev.name === "import")
|
---|
| 283 | ) {
|
---|
| 284 | prev = prev.prev();
|
---|
| 285 | continue
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | break
|
---|
| 289 | } while (prev)
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | // All `@import` statements may be preceded by `@charset` or `@layer` statements.
|
---|
| 293 | // But the `@import` statements must be consecutive.
|
---|
| 294 | if (prev) {
|
---|
| 295 | do {
|
---|
| 296 | if (
|
---|
| 297 | prev.type === "comment" ||
|
---|
| 298 | (prev.type === "atrule" &&
|
---|
| 299 | (prev.name === "charset" || (prev.name === "layer" && !prev.nodes)))
|
---|
| 300 | ) {
|
---|
| 301 | prev = prev.prev();
|
---|
| 302 | continue
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | return result.warn(
|
---|
| 306 | "@import must precede all other statements (besides @charset or empty @layer)",
|
---|
| 307 | { node: atRule },
|
---|
| 308 | )
|
---|
| 309 | } while (prev)
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | if (atRule.nodes) {
|
---|
| 313 | return result.warn(
|
---|
| 314 | "It looks like you didn't end your @import statement correctly. " +
|
---|
| 315 | "Child nodes are attached to it.",
|
---|
| 316 | { node: atRule },
|
---|
| 317 | )
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | const params = valueParser(atRule.params).nodes;
|
---|
| 321 | const stmt = {
|
---|
| 322 | type: "import",
|
---|
| 323 | uri: "",
|
---|
| 324 | fullUri: "",
|
---|
| 325 | node: atRule,
|
---|
| 326 | conditions: [...conditions],
|
---|
| 327 | from,
|
---|
| 328 | };
|
---|
| 329 |
|
---|
| 330 | let layer;
|
---|
| 331 | let media;
|
---|
| 332 | let supports;
|
---|
| 333 |
|
---|
| 334 | for (let i = 0; i < params.length; i++) {
|
---|
| 335 | const node = params[i];
|
---|
| 336 |
|
---|
| 337 | if (node.type === "space" || node.type === "comment") continue
|
---|
| 338 |
|
---|
| 339 | if (node.type === "string") {
|
---|
| 340 | if (stmt.uri) {
|
---|
| 341 | return result.warn(`Multiple url's in '${atRule.toString()}'`, {
|
---|
| 342 | node: atRule,
|
---|
| 343 | })
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | if (!node.value) {
|
---|
| 347 | return result.warn(`Unable to find uri in '${atRule.toString()}'`, {
|
---|
| 348 | node: atRule,
|
---|
| 349 | })
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | stmt.uri = node.value;
|
---|
| 353 | stmt.fullUri = stringify(node);
|
---|
| 354 | continue
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | if (node.type === "function" && /^url$/i.test(node.value)) {
|
---|
| 358 | if (stmt.uri) {
|
---|
| 359 | return result.warn(`Multiple url's in '${atRule.toString()}'`, {
|
---|
| 360 | node: atRule,
|
---|
| 361 | })
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | if (!node.nodes?.[0]?.value) {
|
---|
| 365 | return result.warn(`Unable to find uri in '${atRule.toString()}'`, {
|
---|
| 366 | node: atRule,
|
---|
| 367 | })
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | stmt.uri = node.nodes[0].value;
|
---|
| 371 | stmt.fullUri = stringify(node);
|
---|
| 372 | continue
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | if (!stmt.uri) {
|
---|
| 376 | return result.warn(`Unable to find uri in '${atRule.toString()}'`, {
|
---|
| 377 | node: atRule,
|
---|
| 378 | })
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | if (
|
---|
| 382 | (node.type === "word" || node.type === "function") &&
|
---|
| 383 | /^layer$/i.test(node.value)
|
---|
| 384 | ) {
|
---|
| 385 | if (typeof layer !== "undefined") {
|
---|
| 386 | return result.warn(`Multiple layers in '${atRule.toString()}'`, {
|
---|
| 387 | node: atRule,
|
---|
| 388 | })
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | if (typeof supports !== "undefined") {
|
---|
| 392 | return result.warn(
|
---|
| 393 | `layers must be defined before support conditions in '${atRule.toString()}'`,
|
---|
| 394 | {
|
---|
| 395 | node: atRule,
|
---|
| 396 | },
|
---|
| 397 | )
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | if (node.nodes) {
|
---|
| 401 | layer = stringify(node.nodes);
|
---|
| 402 | } else {
|
---|
| 403 | layer = "";
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | continue
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | if (node.type === "function" && /^supports$/i.test(node.value)) {
|
---|
| 410 | if (typeof supports !== "undefined") {
|
---|
| 411 | return result.warn(
|
---|
| 412 | `Multiple support conditions in '${atRule.toString()}'`,
|
---|
| 413 | {
|
---|
| 414 | node: atRule,
|
---|
| 415 | },
|
---|
| 416 | )
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | supports = stringify(node.nodes);
|
---|
| 420 |
|
---|
| 421 | continue
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | media = stringify(params.slice(i));
|
---|
| 425 | break
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | if (!stmt.uri) {
|
---|
| 429 | return result.warn(`Unable to find uri in '${atRule.toString()}'`, {
|
---|
| 430 | node: atRule,
|
---|
| 431 | })
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | if (
|
---|
| 435 | typeof media !== "undefined" ||
|
---|
| 436 | typeof layer !== "undefined" ||
|
---|
| 437 | typeof supports !== "undefined"
|
---|
| 438 | ) {
|
---|
| 439 | stmt.conditions.push({
|
---|
| 440 | layer,
|
---|
| 441 | media,
|
---|
| 442 | supports,
|
---|
| 443 | });
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | return stmt
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | // builtin tooling
|
---|
| 450 | const path$2 = require$$0;
|
---|
| 451 |
|
---|
| 452 | // placeholder tooling
|
---|
| 453 | let sugarss;
|
---|
| 454 |
|
---|
| 455 | var processContent$1 = function processContent(
|
---|
| 456 | result,
|
---|
| 457 | content,
|
---|
| 458 | filename,
|
---|
| 459 | options,
|
---|
| 460 | postcss,
|
---|
| 461 | ) {
|
---|
| 462 | const { plugins } = options;
|
---|
| 463 | const ext = path$2.extname(filename);
|
---|
| 464 |
|
---|
| 465 | const parserList = [];
|
---|
| 466 |
|
---|
| 467 | // SugarSS support:
|
---|
| 468 | if (ext === ".sss") {
|
---|
| 469 | if (!sugarss) {
|
---|
| 470 | /* c8 ignore next 3 */
|
---|
| 471 | try {
|
---|
| 472 | sugarss = __require('sugarss');
|
---|
| 473 | } catch {} // Ignore
|
---|
| 474 | }
|
---|
| 475 | if (sugarss)
|
---|
| 476 | return runPostcss(postcss, content, filename, plugins, [sugarss])
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | // Syntax support:
|
---|
| 480 | if (result.opts.syntax?.parse) {
|
---|
| 481 | parserList.push(result.opts.syntax.parse);
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | // Parser support:
|
---|
| 485 | if (result.opts.parser) parserList.push(result.opts.parser);
|
---|
| 486 | // Try the default as a last resort:
|
---|
| 487 | parserList.push(null);
|
---|
| 488 |
|
---|
| 489 | return runPostcss(postcss, content, filename, plugins, parserList)
|
---|
| 490 | };
|
---|
| 491 |
|
---|
| 492 | function runPostcss(postcss, content, filename, plugins, parsers, index) {
|
---|
| 493 | if (!index) index = 0;
|
---|
| 494 | return postcss(plugins)
|
---|
| 495 | .process(content, {
|
---|
| 496 | from: filename,
|
---|
| 497 | parser: parsers[index],
|
---|
| 498 | })
|
---|
| 499 | .catch(err => {
|
---|
| 500 | // If there's an error, try the next parser
|
---|
| 501 | index++;
|
---|
| 502 | // If there are no parsers left, throw it
|
---|
| 503 | if (index === parsers.length) throw err
|
---|
| 504 | return runPostcss(postcss, content, filename, plugins, parsers, index)
|
---|
| 505 | })
|
---|
| 506 | }
|
---|
| 507 |
|
---|
| 508 | const path$1 = require$$0;
|
---|
| 509 |
|
---|
| 510 | const dataURL = dataUrl;
|
---|
| 511 | const parseStatements = parseStatements$1;
|
---|
| 512 | const processContent = processContent$1;
|
---|
| 513 | const resolveId$1 = (id) => id;
|
---|
| 514 | const formatImportPrelude = formatImportPrelude$2;
|
---|
| 515 |
|
---|
| 516 | async function parseStyles$1(
|
---|
| 517 | result,
|
---|
| 518 | styles,
|
---|
| 519 | options,
|
---|
| 520 | state,
|
---|
| 521 | conditions,
|
---|
| 522 | from,
|
---|
| 523 | postcss,
|
---|
| 524 | ) {
|
---|
| 525 | const statements = parseStatements(result, styles, conditions, from);
|
---|
| 526 |
|
---|
| 527 | for (const stmt of statements) {
|
---|
| 528 | if (stmt.type !== "import" || !isProcessableURL(stmt.uri)) {
|
---|
| 529 | continue
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | if (options.filter && !options.filter(stmt.uri)) {
|
---|
| 533 | // rejected by filter
|
---|
| 534 | continue
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | await resolveImportId(result, stmt, options, state, postcss);
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | let charset;
|
---|
| 541 | const imports = [];
|
---|
| 542 | const bundle = [];
|
---|
| 543 |
|
---|
| 544 | function handleCharset(stmt) {
|
---|
| 545 | if (!charset) charset = stmt;
|
---|
| 546 | // charsets aren't case-sensitive, so convert to lower case to compare
|
---|
| 547 | else if (
|
---|
| 548 | stmt.node.params.toLowerCase() !== charset.node.params.toLowerCase()
|
---|
| 549 | ) {
|
---|
| 550 | throw stmt.node.error(
|
---|
| 551 | `Incompatible @charset statements:
|
---|
| 552 | ${stmt.node.params} specified in ${stmt.node.source.input.file}
|
---|
| 553 | ${charset.node.params} specified in ${charset.node.source.input.file}`,
|
---|
| 554 | )
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | // squash statements and their children
|
---|
| 559 | statements.forEach(stmt => {
|
---|
| 560 | if (stmt.type === "charset") handleCharset(stmt);
|
---|
| 561 | else if (stmt.type === "import") {
|
---|
| 562 | if (stmt.children) {
|
---|
| 563 | stmt.children.forEach((child, index) => {
|
---|
| 564 | if (child.type === "import") imports.push(child);
|
---|
| 565 | else if (child.type === "charset") handleCharset(child);
|
---|
| 566 | else bundle.push(child);
|
---|
| 567 | // For better output
|
---|
| 568 | if (index === 0) child.parent = stmt;
|
---|
| 569 | });
|
---|
| 570 | } else imports.push(stmt);
|
---|
| 571 | } else if (stmt.type === "nodes") {
|
---|
| 572 | bundle.push(stmt);
|
---|
| 573 | }
|
---|
| 574 | });
|
---|
| 575 |
|
---|
| 576 | return charset ? [charset, ...imports.concat(bundle)] : imports.concat(bundle)
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | async function resolveImportId(result, stmt, options, state, postcss) {
|
---|
| 580 | if (dataURL.isValid(stmt.uri)) {
|
---|
| 581 | // eslint-disable-next-line require-atomic-updates
|
---|
| 582 | stmt.children = await loadImportContent(
|
---|
| 583 | result,
|
---|
| 584 | stmt,
|
---|
| 585 | stmt.uri,
|
---|
| 586 | options,
|
---|
| 587 | state,
|
---|
| 588 | postcss,
|
---|
| 589 | );
|
---|
| 590 |
|
---|
| 591 | return
|
---|
| 592 | } else if (dataURL.isValid(stmt.from.slice(-1))) {
|
---|
| 593 | // Data urls can't be used as a base url to resolve imports.
|
---|
| 594 | throw stmt.node.error(
|
---|
| 595 | `Unable to import '${stmt.uri}' from a stylesheet that is embedded in a data url`,
|
---|
| 596 | )
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | const atRule = stmt.node;
|
---|
| 600 | let sourceFile;
|
---|
| 601 | if (atRule.source?.input?.file) {
|
---|
| 602 | sourceFile = atRule.source.input.file;
|
---|
| 603 | }
|
---|
| 604 | const base = sourceFile
|
---|
| 605 | ? path$1.dirname(atRule.source.input.file)
|
---|
| 606 | : options.root;
|
---|
| 607 |
|
---|
| 608 | const paths = [await options.resolve(stmt.uri, base, options, atRule)].flat();
|
---|
| 609 |
|
---|
| 610 | // Ensure that each path is absolute:
|
---|
| 611 | const resolved = await Promise.all(
|
---|
| 612 | paths.map(file => {
|
---|
| 613 | return !path$1.isAbsolute(file)
|
---|
| 614 | ? resolveId$1(file)
|
---|
| 615 | : file
|
---|
| 616 | }),
|
---|
| 617 | );
|
---|
| 618 |
|
---|
| 619 | // Add dependency messages:
|
---|
| 620 | resolved.forEach(file => {
|
---|
| 621 | result.messages.push({
|
---|
| 622 | type: "dependency",
|
---|
| 623 | plugin: "postcss-import",
|
---|
| 624 | file,
|
---|
| 625 | parent: sourceFile,
|
---|
| 626 | });
|
---|
| 627 | });
|
---|
| 628 |
|
---|
| 629 | const importedContent = await Promise.all(
|
---|
| 630 | resolved.map(file => {
|
---|
| 631 | return loadImportContent(result, stmt, file, options, state, postcss)
|
---|
| 632 | }),
|
---|
| 633 | );
|
---|
| 634 |
|
---|
| 635 | // Merge loaded statements
|
---|
| 636 | // eslint-disable-next-line require-atomic-updates
|
---|
| 637 | stmt.children = importedContent.flat().filter(x => !!x);
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | async function loadImportContent(
|
---|
| 641 | result,
|
---|
| 642 | stmt,
|
---|
| 643 | filename,
|
---|
| 644 | options,
|
---|
| 645 | state,
|
---|
| 646 | postcss,
|
---|
| 647 | ) {
|
---|
| 648 | const atRule = stmt.node;
|
---|
| 649 | const { conditions, from } = stmt;
|
---|
| 650 | const stmtDuplicateCheckKey = conditions
|
---|
| 651 | .map(condition =>
|
---|
| 652 | formatImportPrelude(condition.layer, condition.media, condition.supports),
|
---|
| 653 | )
|
---|
| 654 | .join(":");
|
---|
| 655 |
|
---|
| 656 | if (options.skipDuplicates) {
|
---|
| 657 | // skip files already imported at the same scope
|
---|
| 658 | if (state.importedFiles[filename]?.[stmtDuplicateCheckKey]) {
|
---|
| 659 | return
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | // save imported files to skip them next time
|
---|
| 663 | if (!state.importedFiles[filename]) {
|
---|
| 664 | state.importedFiles[filename] = {};
|
---|
| 665 | }
|
---|
| 666 | state.importedFiles[filename][stmtDuplicateCheckKey] = true;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | if (from.includes(filename)) {
|
---|
| 670 | return
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | const content = await options.load(filename, options);
|
---|
| 674 |
|
---|
| 675 | if (content.trim() === "" && options.warnOnEmpty) {
|
---|
| 676 | result.warn(`${filename} is empty`, { node: atRule });
|
---|
| 677 | return
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | // skip previous imported files not containing @import rules
|
---|
| 681 | if (
|
---|
| 682 | options.skipDuplicates &&
|
---|
| 683 | state.hashFiles[content]?.[stmtDuplicateCheckKey]
|
---|
| 684 | ) {
|
---|
| 685 | return
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | const importedResult = await processContent(
|
---|
| 689 | result,
|
---|
| 690 | content,
|
---|
| 691 | filename,
|
---|
| 692 | options,
|
---|
| 693 | postcss,
|
---|
| 694 | );
|
---|
| 695 |
|
---|
| 696 | const styles = importedResult.root;
|
---|
| 697 | result.messages = result.messages.concat(importedResult.messages);
|
---|
| 698 |
|
---|
| 699 | if (options.skipDuplicates) {
|
---|
| 700 | const hasImport = styles.some(child => {
|
---|
| 701 | return child.type === "atrule" && child.name === "import"
|
---|
| 702 | });
|
---|
| 703 | if (!hasImport) {
|
---|
| 704 | // save hash files to skip them next time
|
---|
| 705 | if (!state.hashFiles[content]) {
|
---|
| 706 | state.hashFiles[content] = {};
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | state.hashFiles[content][stmtDuplicateCheckKey] = true;
|
---|
| 710 | }
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | // recursion: import @import from imported file
|
---|
| 714 | return parseStyles$1(
|
---|
| 715 | result,
|
---|
| 716 | styles,
|
---|
| 717 | options,
|
---|
| 718 | state,
|
---|
| 719 | conditions,
|
---|
| 720 | [...from, filename],
|
---|
| 721 | postcss,
|
---|
| 722 | )
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | function isProcessableURL(uri) {
|
---|
| 726 | // skip protocol base uri (protocol://url) or protocol-relative
|
---|
| 727 | if (/^(?:[a-z]+:)?\/\//i.test(uri)) {
|
---|
| 728 | return false
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | // check for fragment or query
|
---|
| 732 | try {
|
---|
| 733 | // needs a base to parse properly
|
---|
| 734 | const url = new URL(uri, "https://example.com");
|
---|
| 735 | if (url.search) {
|
---|
| 736 | return false
|
---|
| 737 | }
|
---|
| 738 | } catch {} // Ignore
|
---|
| 739 |
|
---|
| 740 | return true
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | var parseStyles_1 = parseStyles$1;
|
---|
| 744 |
|
---|
| 745 | // builtin tooling
|
---|
| 746 | const path = require$$0;
|
---|
| 747 |
|
---|
| 748 | // internal tooling
|
---|
| 749 | const applyConditions = applyConditions$1;
|
---|
| 750 | const applyRaws = applyRaws$1;
|
---|
| 751 | const applyStyles = applyStyles$1;
|
---|
| 752 | const loadContent = () => "";
|
---|
| 753 | const parseStyles = parseStyles_1;
|
---|
| 754 | const resolveId = (id) => id;
|
---|
| 755 |
|
---|
| 756 | function AtImport(options) {
|
---|
| 757 | options = {
|
---|
| 758 | root: process.cwd(),
|
---|
| 759 | path: [],
|
---|
| 760 | skipDuplicates: true,
|
---|
| 761 | resolve: resolveId,
|
---|
| 762 | load: loadContent,
|
---|
| 763 | plugins: [],
|
---|
| 764 | addModulesDirectories: [],
|
---|
| 765 | warnOnEmpty: true,
|
---|
| 766 | ...options,
|
---|
| 767 | };
|
---|
| 768 |
|
---|
| 769 | options.root = path.resolve(options.root);
|
---|
| 770 |
|
---|
| 771 | // convert string to an array of a single element
|
---|
| 772 | if (typeof options.path === "string") options.path = [options.path];
|
---|
| 773 |
|
---|
| 774 | if (!Array.isArray(options.path)) options.path = [];
|
---|
| 775 |
|
---|
| 776 | options.path = options.path.map(p => path.resolve(options.root, p));
|
---|
| 777 |
|
---|
| 778 | return {
|
---|
| 779 | postcssPlugin: "postcss-import",
|
---|
| 780 | async Once(styles, { result, atRule, postcss }) {
|
---|
| 781 | const state = {
|
---|
| 782 | importedFiles: {},
|
---|
| 783 | hashFiles: {},
|
---|
| 784 | };
|
---|
| 785 |
|
---|
| 786 | if (styles.source?.input?.file) {
|
---|
| 787 | state.importedFiles[styles.source.input.file] = {};
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | if (options.plugins && !Array.isArray(options.plugins)) {
|
---|
| 791 | throw new Error("plugins option must be an array")
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | const bundle = await parseStyles(
|
---|
| 795 | result,
|
---|
| 796 | styles,
|
---|
| 797 | options,
|
---|
| 798 | state,
|
---|
| 799 | [],
|
---|
| 800 | [],
|
---|
| 801 | postcss,
|
---|
| 802 | );
|
---|
| 803 |
|
---|
| 804 | applyRaws(bundle);
|
---|
| 805 | applyConditions(bundle, atRule);
|
---|
| 806 | applyStyles(bundle, styles);
|
---|
| 807 | },
|
---|
| 808 | }
|
---|
| 809 | }
|
---|
| 810 |
|
---|
| 811 | AtImport.postcss = true;
|
---|
| 812 |
|
---|
| 813 | var postcssImport = AtImport;
|
---|
| 814 |
|
---|
| 815 | var index = /*@__PURE__*/getDefaultExportFromCjs(postcssImport);
|
---|
| 816 |
|
---|
| 817 | var index$1 = /*#__PURE__*/_mergeNamespaces({
|
---|
| 818 | __proto__: null,
|
---|
| 819 | default: index
|
---|
| 820 | }, [postcssImport]);
|
---|
| 821 |
|
---|
| 822 | export { index$1 as i };
|
---|