123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _postcss = _interopRequireDefault(require("postcss"));
- var _icssUtils = require("icss-utils");
- var _utils = require("../utils");
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- var _default = _postcss.default.plugin('postcss-icss-parser', options => async css => {
- const importReplacements = Object.create(null);
- const {
- icssImports,
- icssExports
- } = (0, _icssUtils.extractICSS)(css);
- const imports = new Map();
- const tasks = []; // eslint-disable-next-line guard-for-in
- for (const url in icssImports) {
- const tokens = icssImports[url];
- if (Object.keys(tokens).length === 0) {
- // eslint-disable-next-line no-continue
- continue;
- }
- let normalizedUrl = url;
- let prefix = '';
- const queryParts = normalizedUrl.split('!');
- if (queryParts.length > 1) {
- normalizedUrl = queryParts.pop();
- prefix = queryParts.join('!');
- }
- const request = (0, _utils.requestify)((0, _utils.normalizeUrl)(normalizedUrl, true), options.rootContext);
- const doResolve = async () => {
- const {
- resolver,
- context
- } = options;
- const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([normalizedUrl, request])]);
- return {
- url: resolvedUrl,
- prefix,
- tokens
- };
- };
- tasks.push(doResolve());
- }
- const results = await Promise.all(tasks);
- for (let index = 0; index <= results.length - 1; index++) {
- const {
- url,
- prefix,
- tokens
- } = results[index];
- const newUrl = prefix ? `${prefix}!${url}` : url;
- const importKey = newUrl;
- let importName = imports.get(importKey);
- if (!importName) {
- importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
- imports.set(importKey, importName);
- options.imports.push({
- importName,
- url: options.urlHandler(newUrl),
- icss: true,
- index
- });
- options.api.push({
- importName,
- dedupe: true,
- index
- });
- }
- for (const [replacementIndex, token] of Object.keys(tokens).entries()) {
- const replacementName = `___CSS_LOADER_ICSS_IMPORT_${index}_REPLACEMENT_${replacementIndex}___`;
- const localName = tokens[token];
- importReplacements[token] = replacementName;
- options.replacements.push({
- replacementName,
- importName,
- localName
- });
- }
- }
- if (Object.keys(importReplacements).length > 0) {
- (0, _icssUtils.replaceSymbols)(css, importReplacements);
- }
- for (const name of Object.keys(icssExports)) {
- const value = (0, _icssUtils.replaceValueSymbols)(icssExports[name], importReplacements);
- options.exports.push({
- name,
- value
- });
- }
- });
- exports.default = _default;
|