UNPKG

3.41 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _icssUtils = require("icss-utils");
9
10var _utils = require("../utils");
11
12const plugin = (options = {}) => {
13 return {
14 postcssPlugin: "postcss-icss-parser",
15
16 async OnceExit(root) {
17 const importReplacements = Object.create(null);
18 const {
19 icssImports,
20 icssExports
21 } = (0, _icssUtils.extractICSS)(root);
22 const imports = new Map();
23 const tasks = []; // eslint-disable-next-line guard-for-in
24
25 for (const url in icssImports) {
26 const tokens = icssImports[url];
27
28 if (Object.keys(tokens).length === 0) {
29 // eslint-disable-next-line no-continue
30 continue;
31 }
32
33 let normalizedUrl = url;
34 let prefix = "";
35 const queryParts = normalizedUrl.split("!");
36
37 if (queryParts.length > 1) {
38 normalizedUrl = queryParts.pop();
39 prefix = queryParts.join("!");
40 }
41
42 const request = (0, _utils.requestify)((0, _utils.normalizeUrl)(normalizedUrl, true), options.rootContext);
43
44 const doResolve = async () => {
45 const {
46 resolver,
47 context
48 } = options;
49 const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([normalizedUrl, request])]);
50
51 if (!resolvedUrl) {
52 return;
53 } // eslint-disable-next-line consistent-return
54
55
56 return {
57 url: resolvedUrl,
58 prefix,
59 tokens
60 };
61 };
62
63 tasks.push(doResolve());
64 }
65
66 const results = await Promise.all(tasks);
67
68 for (let index = 0; index <= results.length - 1; index++) {
69 const item = results[index];
70
71 if (!item) {
72 // eslint-disable-next-line no-continue
73 continue;
74 }
75
76 const newUrl = item.prefix ? `${item.prefix}!${item.url}` : item.url;
77 const importKey = newUrl;
78 let importName = imports.get(importKey);
79
80 if (!importName) {
81 importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
82 imports.set(importKey, importName);
83 options.imports.push({
84 type: "icss_import",
85 importName,
86 url: options.urlHandler(newUrl),
87 icss: true,
88 index
89 });
90 options.api.push({
91 importName,
92 dedupe: true,
93 index
94 });
95 }
96
97 for (const [replacementIndex, token] of Object.keys(item.tokens).entries()) {
98 const replacementName = `___CSS_LOADER_ICSS_IMPORT_${index}_REPLACEMENT_${replacementIndex}___`;
99 const localName = item.tokens[token];
100 importReplacements[token] = replacementName;
101 options.replacements.push({
102 replacementName,
103 importName,
104 localName
105 });
106 }
107 }
108
109 if (Object.keys(importReplacements).length > 0) {
110 (0, _icssUtils.replaceSymbols)(root, importReplacements);
111 }
112
113 for (const name of Object.keys(icssExports)) {
114 const value = (0, _icssUtils.replaceValueSymbols)(icssExports[name], importReplacements);
115 options.exports.push({
116 name,
117 value
118 });
119 }
120 }
121
122 };
123};
124
125plugin.postcss = true;
126var _default = plugin;
127exports.default = _default;
\No newline at end of file