UNPKG

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