@unocss/vite 0.58.9 → 0.59.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +23 -16
- package/dist/client.cjs +0 -54
- package/dist/client.d.cts +0 -2
- package/dist/index.cjs +0 -1270
- package/dist/index.d.cts +0 -78
package/dist/index.cjs
DELETED
|
@@ -1,1270 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const process$1 = require('node:process');
|
|
6
|
-
const UnocssInspector = require('@unocss/inspector');
|
|
7
|
-
const node_path = require('node:path');
|
|
8
|
-
const fs = require('node:fs/promises');
|
|
9
|
-
const fg = require('fast-glob');
|
|
10
|
-
const MagicString = require('magic-string');
|
|
11
|
-
const remapping = require('@ampproject/remapping');
|
|
12
|
-
const node_crypto = require('node:crypto');
|
|
13
|
-
const core = require('@unocss/core');
|
|
14
|
-
const node_buffer = require('node:buffer');
|
|
15
|
-
const pluginutils = require('@rollup/pluginutils');
|
|
16
|
-
const fs$1 = require('node:fs');
|
|
17
|
-
const node_url = require('node:url');
|
|
18
|
-
const config = require('@unocss/config');
|
|
19
|
-
|
|
20
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
21
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
22
|
-
|
|
23
|
-
const process__default = /*#__PURE__*/_interopDefaultCompat(process$1);
|
|
24
|
-
const UnocssInspector__default = /*#__PURE__*/_interopDefaultCompat(UnocssInspector);
|
|
25
|
-
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
26
|
-
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
27
|
-
const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
|
|
28
|
-
const remapping__default = /*#__PURE__*/_interopDefaultCompat(remapping);
|
|
29
|
-
const fs__default$1 = /*#__PURE__*/_interopDefaultCompat(fs$1);
|
|
30
|
-
|
|
31
|
-
const defaultPipelineExclude = [core.cssIdRE];
|
|
32
|
-
const defaultPipelineInclude = [/\.(vue|svelte|[jt]sx|mdx?|astro|elm|php|phtml|html)($|\?)/];
|
|
33
|
-
|
|
34
|
-
const VIRTUAL_ENTRY_ALIAS = [
|
|
35
|
-
/^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
|
|
36
|
-
];
|
|
37
|
-
const LAYER_MARK_ALL = "__ALL__";
|
|
38
|
-
const RESOLVED_ID_WITH_QUERY_RE = /[\/\\]__uno(?:(_.*?))?\.css(\?.*)?$/;
|
|
39
|
-
const RESOLVED_ID_RE = /[\/\\]__uno(?:_(.*?))?\.css$/;
|
|
40
|
-
function resolveId(id) {
|
|
41
|
-
if (id.match(RESOLVED_ID_WITH_QUERY_RE))
|
|
42
|
-
return id;
|
|
43
|
-
for (const alias of VIRTUAL_ENTRY_ALIAS) {
|
|
44
|
-
const match = id.match(alias);
|
|
45
|
-
if (match) {
|
|
46
|
-
return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
function resolveLayer(id) {
|
|
51
|
-
const match = id.match(RESOLVED_ID_RE);
|
|
52
|
-
if (match)
|
|
53
|
-
return match[1] || LAYER_MARK_ALL;
|
|
54
|
-
}
|
|
55
|
-
const LAYER_PLACEHOLDER_RE = /#--unocss--\s*{\s*layer\s*:\s*(.+?)\s*(?:;\s*escape-view\s*:\s*(.+?)\s*)?;?\s*}/g;
|
|
56
|
-
function getLayerPlaceholder(layer) {
|
|
57
|
-
return `#--unocss--{layer:${layer};escape-view:\\"\\'\\\`\\\\}`;
|
|
58
|
-
}
|
|
59
|
-
const HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*\\*"(.+?)\\*";?\s*}/g;
|
|
60
|
-
function getHashPlaceholder(hash) {
|
|
61
|
-
return `#--unocss-hash--{content:"${hash}"}`;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const INCLUDE_COMMENT = "@unocss-include";
|
|
65
|
-
const IGNORE_COMMENT = "@unocss-ignore";
|
|
66
|
-
const CSS_PLACEHOLDER = "@unocss-placeholder";
|
|
67
|
-
const SKIP_START_COMMENT = "@unocss-skip-start";
|
|
68
|
-
const SKIP_END_COMMENT = "@unocss-skip-end";
|
|
69
|
-
const SKIP_COMMENT_RE = new RegExp(`(//\\s*?${SKIP_START_COMMENT}\\s*?|\\/\\*\\s*?${SKIP_START_COMMENT}\\s*?\\*\\/|<!--\\s*?${SKIP_START_COMMENT}\\s*?-->)[\\s\\S]*?(//\\s*?${SKIP_END_COMMENT}\\s*?|\\/\\*\\s*?${SKIP_END_COMMENT}\\s*?\\*\\/|<!--\\s*?${SKIP_END_COMMENT}\\s*?-->)`, "g");
|
|
70
|
-
|
|
71
|
-
function deprecationCheck(config) {
|
|
72
|
-
let warned = false;
|
|
73
|
-
function warn(msg) {
|
|
74
|
-
warned = true;
|
|
75
|
-
console.warn(`[unocss] ${msg}`);
|
|
76
|
-
}
|
|
77
|
-
if (config.include)
|
|
78
|
-
warn("`include` option is deprecated, use `content.pipeline.include` instead.");
|
|
79
|
-
if (config.exclude)
|
|
80
|
-
warn("`exclude` option is deprecated, use `content.pipeline.exclude` instead.");
|
|
81
|
-
if (config.extraContent)
|
|
82
|
-
warn("`extraContent` option is deprecated, use `content` instead.");
|
|
83
|
-
if (config.content?.plain)
|
|
84
|
-
warn("`content.plain` option is renamed to `content.inline`.");
|
|
85
|
-
if (warned && typeof process !== "undefined" && process.env.CI)
|
|
86
|
-
throw new Error("deprecation warning");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function createContext(configOrPath, defaults = {}, extraConfigSources = [], resolveConfigResult = () => {
|
|
90
|
-
}) {
|
|
91
|
-
let root = process__default.cwd();
|
|
92
|
-
let rawConfig = {};
|
|
93
|
-
let configFileList = [];
|
|
94
|
-
const uno = core.createGenerator(rawConfig, defaults);
|
|
95
|
-
let rollupFilter = pluginutils.createFilter(
|
|
96
|
-
defaultPipelineInclude,
|
|
97
|
-
defaultPipelineExclude,
|
|
98
|
-
{ resolve: typeof configOrPath === "string" ? configOrPath : root }
|
|
99
|
-
);
|
|
100
|
-
const invalidations = [];
|
|
101
|
-
const reloadListeners = [];
|
|
102
|
-
const modules = new core.BetterMap();
|
|
103
|
-
const tokens = /* @__PURE__ */ new Set();
|
|
104
|
-
const tasks = [];
|
|
105
|
-
const affectedModules = /* @__PURE__ */ new Set();
|
|
106
|
-
let ready = reloadConfig();
|
|
107
|
-
async function reloadConfig() {
|
|
108
|
-
const result = await config.loadConfig(root, configOrPath, extraConfigSources, defaults);
|
|
109
|
-
resolveConfigResult(result);
|
|
110
|
-
deprecationCheck(result.config);
|
|
111
|
-
rawConfig = result.config;
|
|
112
|
-
configFileList = result.sources;
|
|
113
|
-
uno.setConfig(rawConfig);
|
|
114
|
-
uno.config.envMode = "dev";
|
|
115
|
-
rollupFilter = rawConfig.content?.pipeline === false ? () => false : pluginutils.createFilter(
|
|
116
|
-
rawConfig.content?.pipeline?.include || rawConfig.include || defaultPipelineInclude,
|
|
117
|
-
rawConfig.content?.pipeline?.exclude || rawConfig.exclude || defaultPipelineExclude,
|
|
118
|
-
{ resolve: typeof configOrPath === "string" ? configOrPath : root }
|
|
119
|
-
);
|
|
120
|
-
tokens.clear();
|
|
121
|
-
await Promise.all(modules.map((code, id) => uno.applyExtractors(code.replace(SKIP_COMMENT_RE, ""), id, tokens)));
|
|
122
|
-
invalidate();
|
|
123
|
-
dispatchReload();
|
|
124
|
-
const presets = /* @__PURE__ */ new Set();
|
|
125
|
-
uno.config.presets.forEach((i) => {
|
|
126
|
-
if (!i.name)
|
|
127
|
-
return;
|
|
128
|
-
if (presets.has(i.name))
|
|
129
|
-
console.warn(`[unocss] duplication of preset ${i.name} found, there might be something wrong with your config.`);
|
|
130
|
-
else
|
|
131
|
-
presets.add(i.name);
|
|
132
|
-
});
|
|
133
|
-
return result;
|
|
134
|
-
}
|
|
135
|
-
async function updateRoot(newRoot) {
|
|
136
|
-
if (newRoot !== root) {
|
|
137
|
-
root = newRoot;
|
|
138
|
-
ready = reloadConfig();
|
|
139
|
-
}
|
|
140
|
-
return await ready;
|
|
141
|
-
}
|
|
142
|
-
function invalidate() {
|
|
143
|
-
invalidations.forEach((cb) => cb());
|
|
144
|
-
}
|
|
145
|
-
function dispatchReload() {
|
|
146
|
-
reloadListeners.forEach((cb) => cb());
|
|
147
|
-
}
|
|
148
|
-
async function extract(code, id) {
|
|
149
|
-
if (id)
|
|
150
|
-
modules.set(id, code);
|
|
151
|
-
const len = tokens.size;
|
|
152
|
-
await uno.applyExtractors(code.replace(SKIP_COMMENT_RE, ""), id, tokens);
|
|
153
|
-
if (tokens.size > len)
|
|
154
|
-
invalidate();
|
|
155
|
-
}
|
|
156
|
-
function filter(code, id) {
|
|
157
|
-
if (code.includes(IGNORE_COMMENT))
|
|
158
|
-
return false;
|
|
159
|
-
return code.includes(INCLUDE_COMMENT) || code.includes(CSS_PLACEHOLDER) || rollupFilter(id.replace(/\?v=\w+$/, ""));
|
|
160
|
-
}
|
|
161
|
-
async function getConfig() {
|
|
162
|
-
await ready;
|
|
163
|
-
return rawConfig;
|
|
164
|
-
}
|
|
165
|
-
async function flushTasks() {
|
|
166
|
-
const _tasks = [...tasks];
|
|
167
|
-
await Promise.all(_tasks);
|
|
168
|
-
tasks.splice(0, _tasks.length);
|
|
169
|
-
}
|
|
170
|
-
return {
|
|
171
|
-
get ready() {
|
|
172
|
-
return ready;
|
|
173
|
-
},
|
|
174
|
-
tokens,
|
|
175
|
-
modules,
|
|
176
|
-
affectedModules,
|
|
177
|
-
tasks,
|
|
178
|
-
flushTasks,
|
|
179
|
-
invalidate,
|
|
180
|
-
onInvalidate(fn) {
|
|
181
|
-
invalidations.push(fn);
|
|
182
|
-
},
|
|
183
|
-
filter,
|
|
184
|
-
reloadConfig,
|
|
185
|
-
onReload(fn) {
|
|
186
|
-
reloadListeners.push(fn);
|
|
187
|
-
},
|
|
188
|
-
uno,
|
|
189
|
-
extract,
|
|
190
|
-
getConfig,
|
|
191
|
-
get root() {
|
|
192
|
-
return root;
|
|
193
|
-
},
|
|
194
|
-
updateRoot,
|
|
195
|
-
getConfigFileList: () => configFileList
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function getPath(id) {
|
|
200
|
-
return id.replace(/\?.*$/, "");
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function getHash(input, length = 8) {
|
|
204
|
-
return node_crypto.createHash("sha256").update(input).digest("hex").slice(0, length);
|
|
205
|
-
}
|
|
206
|
-
function hash(str) {
|
|
207
|
-
let i;
|
|
208
|
-
let l;
|
|
209
|
-
let hval = 2166136261;
|
|
210
|
-
for (i = 0, l = str.length; i < l; i++) {
|
|
211
|
-
hval ^= str.charCodeAt(i);
|
|
212
|
-
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
|
|
213
|
-
}
|
|
214
|
-
return `00000${(hval >>> 0).toString(36)}`.slice(-6);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function replaceAsync(string, searchValue, replacer) {
|
|
218
|
-
try {
|
|
219
|
-
if (typeof replacer === "function") {
|
|
220
|
-
const values = [];
|
|
221
|
-
String.prototype.replace.call(string, searchValue, (...args) => {
|
|
222
|
-
values.push(replacer(...args));
|
|
223
|
-
return "";
|
|
224
|
-
});
|
|
225
|
-
return Promise.all(values).then((resolvedValues) => {
|
|
226
|
-
return String.prototype.replace.call(string, searchValue, () => {
|
|
227
|
-
return resolvedValues.shift() || "";
|
|
228
|
-
});
|
|
229
|
-
});
|
|
230
|
-
} else {
|
|
231
|
-
return Promise.resolve(
|
|
232
|
-
String.prototype.replace.call(string, searchValue, replacer)
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
} catch (error) {
|
|
236
|
-
return Promise.reject(error);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
function ChunkModeBuildPlugin({ uno, filter }) {
|
|
241
|
-
let cssPlugin;
|
|
242
|
-
const files = {};
|
|
243
|
-
return {
|
|
244
|
-
name: "unocss:chunk",
|
|
245
|
-
apply: "build",
|
|
246
|
-
enforce: "pre",
|
|
247
|
-
configResolved(config) {
|
|
248
|
-
cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
249
|
-
},
|
|
250
|
-
transform(code, id) {
|
|
251
|
-
if (!filter(code, id))
|
|
252
|
-
return;
|
|
253
|
-
files[id] = code;
|
|
254
|
-
return null;
|
|
255
|
-
},
|
|
256
|
-
async renderChunk(_, chunk) {
|
|
257
|
-
const chunks = Object.keys(chunk.modules).map((i) => files[i]).filter(Boolean);
|
|
258
|
-
if (!chunks.length)
|
|
259
|
-
return null;
|
|
260
|
-
const tokens = /* @__PURE__ */ new Set();
|
|
261
|
-
await Promise.all(chunks.map((c) => uno.applyExtractors(c, void 0, tokens)));
|
|
262
|
-
const { css } = await uno.generate(tokens);
|
|
263
|
-
const fakeCssId = `${chunk.fileName}.css`;
|
|
264
|
-
await cssPlugin.transform(css, fakeCssId);
|
|
265
|
-
chunk.modules[fakeCssId] = {
|
|
266
|
-
code: null,
|
|
267
|
-
originalLength: 0,
|
|
268
|
-
removedExports: [],
|
|
269
|
-
renderedExports: [],
|
|
270
|
-
renderedLength: 0
|
|
271
|
-
};
|
|
272
|
-
return null;
|
|
273
|
-
},
|
|
274
|
-
async transformIndexHtml(code) {
|
|
275
|
-
const { css } = await uno.generate(code);
|
|
276
|
-
if (css)
|
|
277
|
-
return `${code}<style>${css}</style>`;
|
|
278
|
-
}
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
async function applyTransformers(ctx, original, id, enforce = "default") {
|
|
283
|
-
if (original.includes(IGNORE_COMMENT))
|
|
284
|
-
return;
|
|
285
|
-
const transformers = (ctx.uno.config.transformers || []).filter((i) => (i.enforce || "default") === enforce);
|
|
286
|
-
if (!transformers.length)
|
|
287
|
-
return;
|
|
288
|
-
const skipMap = /* @__PURE__ */ new Map();
|
|
289
|
-
let code = original;
|
|
290
|
-
let s = new MagicString__default(transformSkipCode(code, skipMap));
|
|
291
|
-
const maps = [];
|
|
292
|
-
for (const t of transformers) {
|
|
293
|
-
if (t.idFilter) {
|
|
294
|
-
if (!t.idFilter(id))
|
|
295
|
-
continue;
|
|
296
|
-
} else if (!ctx.filter(code, id)) {
|
|
297
|
-
continue;
|
|
298
|
-
}
|
|
299
|
-
await t.transform(s, id, ctx);
|
|
300
|
-
if (s.hasChanged()) {
|
|
301
|
-
code = restoreSkipCode(s.toString(), skipMap);
|
|
302
|
-
maps.push(s.generateMap({ hires: true, source: id }));
|
|
303
|
-
s = new MagicString__default(code);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
if (code !== original) {
|
|
307
|
-
ctx.affectedModules.add(id);
|
|
308
|
-
return {
|
|
309
|
-
code,
|
|
310
|
-
map: remapping__default(maps, (_, ctx2) => {
|
|
311
|
-
ctx2.content = code;
|
|
312
|
-
return null;
|
|
313
|
-
})
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
function transformSkipCode(code, map) {
|
|
318
|
-
for (const item of Array.from(code.matchAll(SKIP_COMMENT_RE))) {
|
|
319
|
-
if (item != null) {
|
|
320
|
-
const matched = item[0];
|
|
321
|
-
const withHashKey = `@unocss-skip-placeholder-${hash(matched)}`;
|
|
322
|
-
map.set(withHashKey, matched);
|
|
323
|
-
code = code.replace(matched, withHashKey);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
return code;
|
|
327
|
-
}
|
|
328
|
-
function restoreSkipCode(code, map) {
|
|
329
|
-
for (const [withHashKey, matched] of map.entries())
|
|
330
|
-
code = code.replace(withHashKey, matched);
|
|
331
|
-
return code;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
async function setupContentExtractor(ctx, shouldWatch = false) {
|
|
335
|
-
const { content } = await ctx.getConfig();
|
|
336
|
-
const { extract, tasks, root, filter } = ctx;
|
|
337
|
-
if (content?.inline) {
|
|
338
|
-
await Promise.all(
|
|
339
|
-
content.inline.map(async (c, idx) => {
|
|
340
|
-
if (typeof c === "function")
|
|
341
|
-
c = await c();
|
|
342
|
-
if (typeof c === "string")
|
|
343
|
-
c = { code: c };
|
|
344
|
-
return extract(c.code, c.id ?? `__plain_content_${idx}__`);
|
|
345
|
-
})
|
|
346
|
-
);
|
|
347
|
-
}
|
|
348
|
-
if (content?.filesystem) {
|
|
349
|
-
const files = await fg__default(content.filesystem, { cwd: root });
|
|
350
|
-
async function extractFile(file) {
|
|
351
|
-
file = node_path.isAbsolute(file) ? file : node_path.resolve(root, file);
|
|
352
|
-
const code = await fs__default.readFile(file, "utf-8");
|
|
353
|
-
if (!filter(code, file))
|
|
354
|
-
return;
|
|
355
|
-
const preTransform = await applyTransformers(ctx, code, file, "pre");
|
|
356
|
-
const defaultTransform = await applyTransformers(ctx, preTransform?.code || code, file);
|
|
357
|
-
await applyTransformers(ctx, defaultTransform?.code || preTransform?.code || code, file, "post");
|
|
358
|
-
return await extract(preTransform?.code || code, file);
|
|
359
|
-
}
|
|
360
|
-
if (shouldWatch) {
|
|
361
|
-
const { watch } = await import('chokidar');
|
|
362
|
-
const ignored = ["**/{.git,node_modules}/**"];
|
|
363
|
-
const watcher = watch(files, {
|
|
364
|
-
ignorePermissionErrors: true,
|
|
365
|
-
ignored,
|
|
366
|
-
cwd: root,
|
|
367
|
-
ignoreInitial: true
|
|
368
|
-
});
|
|
369
|
-
watcher.on("all", (type, file) => {
|
|
370
|
-
if (type === "add" || type === "change") {
|
|
371
|
-
const absolutePath = node_path.resolve(root, file);
|
|
372
|
-
tasks.push(extractFile(absolutePath));
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
await Promise.all(files.map(extractFile));
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
function isLegacyChunk(chunk, options) {
|
|
381
|
-
return options.format === "system" && chunk.fileName.includes("-legacy");
|
|
382
|
-
}
|
|
383
|
-
function GlobalModeBuildPlugin(ctx) {
|
|
384
|
-
const { uno, ready, extract, tokens, filter, getConfig, tasks, flushTasks } = ctx;
|
|
385
|
-
const vfsLayers = /* @__PURE__ */ new Set();
|
|
386
|
-
const layerImporterMap = /* @__PURE__ */ new Map();
|
|
387
|
-
let viteConfig;
|
|
388
|
-
const cssPostPlugins = /* @__PURE__ */ new Map();
|
|
389
|
-
const cssPlugins = /* @__PURE__ */ new Map();
|
|
390
|
-
async function applyCssTransform(css, id, dir, ctx2) {
|
|
391
|
-
const {
|
|
392
|
-
postcss = true
|
|
393
|
-
} = await getConfig();
|
|
394
|
-
if (!cssPlugins.get(dir) || !postcss)
|
|
395
|
-
return css;
|
|
396
|
-
const result = await cssPlugins.get(dir).transform.call(ctx2, css, id);
|
|
397
|
-
if (!result)
|
|
398
|
-
return css;
|
|
399
|
-
if (typeof result === "string")
|
|
400
|
-
css = result;
|
|
401
|
-
else if (result.code)
|
|
402
|
-
css = result.code;
|
|
403
|
-
css = css.replace(/[\n\r]/g, "");
|
|
404
|
-
return css;
|
|
405
|
-
}
|
|
406
|
-
let lastTokenSize = 0;
|
|
407
|
-
let lastResult;
|
|
408
|
-
async function generateAll() {
|
|
409
|
-
await flushTasks();
|
|
410
|
-
if (lastResult && lastTokenSize === tokens.size)
|
|
411
|
-
return lastResult;
|
|
412
|
-
lastResult = await uno.generate(tokens, { minify: true });
|
|
413
|
-
lastTokenSize = tokens.size;
|
|
414
|
-
return lastResult;
|
|
415
|
-
}
|
|
416
|
-
let replaced = false;
|
|
417
|
-
return [
|
|
418
|
-
{
|
|
419
|
-
name: "unocss:global:build:scan",
|
|
420
|
-
apply: "build",
|
|
421
|
-
enforce: "pre",
|
|
422
|
-
async buildStart() {
|
|
423
|
-
vfsLayers.clear();
|
|
424
|
-
tasks.length = 0;
|
|
425
|
-
lastTokenSize = 0;
|
|
426
|
-
lastResult = void 0;
|
|
427
|
-
},
|
|
428
|
-
transform(code, id) {
|
|
429
|
-
if (filter(code, id))
|
|
430
|
-
tasks.push(extract(code, id));
|
|
431
|
-
return null;
|
|
432
|
-
},
|
|
433
|
-
transformIndexHtml: {
|
|
434
|
-
order: "pre",
|
|
435
|
-
handler(code, { filename }) {
|
|
436
|
-
tasks.push(extract(code, filename));
|
|
437
|
-
},
|
|
438
|
-
// Compatibility with Legacy Vite
|
|
439
|
-
enforce: "pre",
|
|
440
|
-
transform(code, { filename }) {
|
|
441
|
-
tasks.push(extract(code, filename));
|
|
442
|
-
}
|
|
443
|
-
},
|
|
444
|
-
resolveId(id, importer) {
|
|
445
|
-
const entry = resolveId(id);
|
|
446
|
-
if (entry) {
|
|
447
|
-
const layer = resolveLayer(entry);
|
|
448
|
-
if (layer) {
|
|
449
|
-
vfsLayers.add(layer);
|
|
450
|
-
if (importer)
|
|
451
|
-
layerImporterMap.set(importer, entry);
|
|
452
|
-
}
|
|
453
|
-
return entry;
|
|
454
|
-
}
|
|
455
|
-
},
|
|
456
|
-
load(id) {
|
|
457
|
-
const layer = resolveLayer(getPath(id));
|
|
458
|
-
if (layer) {
|
|
459
|
-
vfsLayers.add(layer);
|
|
460
|
-
return getLayerPlaceholder(layer);
|
|
461
|
-
}
|
|
462
|
-
},
|
|
463
|
-
moduleParsed({ id, importedIds }) {
|
|
464
|
-
if (!layerImporterMap.has(id))
|
|
465
|
-
return;
|
|
466
|
-
const layerKey = layerImporterMap.get(id);
|
|
467
|
-
if (!importedIds.includes(layerKey)) {
|
|
468
|
-
layerImporterMap.delete(id);
|
|
469
|
-
vfsLayers.delete(resolveLayer(layerKey));
|
|
470
|
-
}
|
|
471
|
-
},
|
|
472
|
-
async configResolved(config) {
|
|
473
|
-
const distDirs = [
|
|
474
|
-
node_path.resolve(config.root, config.build.outDir)
|
|
475
|
-
];
|
|
476
|
-
if (config.build.rollupOptions.output) {
|
|
477
|
-
const outputOptions = config.build.rollupOptions.output;
|
|
478
|
-
const outputDirs = Array.isArray(outputOptions) ? outputOptions.map((option) => option.dir).filter(Boolean) : outputOptions.dir ? [outputOptions.dir] : [];
|
|
479
|
-
outputDirs.forEach((dir) => {
|
|
480
|
-
distDirs.push(dir);
|
|
481
|
-
if (!node_path.isAbsolute(dir))
|
|
482
|
-
distDirs.push(node_path.resolve(config.root, dir));
|
|
483
|
-
});
|
|
484
|
-
}
|
|
485
|
-
const cssPostPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
486
|
-
const cssPlugin = config.plugins.find((i) => i.name === "vite:css");
|
|
487
|
-
if (cssPostPlugin)
|
|
488
|
-
distDirs.forEach((dir) => cssPostPlugins.set(dir, cssPostPlugin));
|
|
489
|
-
if (cssPlugin)
|
|
490
|
-
distDirs.forEach((dir) => cssPlugins.set(dir, cssPlugin));
|
|
491
|
-
await ready;
|
|
492
|
-
},
|
|
493
|
-
// we inject a hash to chunk before the dist hash calculation to make sure
|
|
494
|
-
// the hash is different when unocss changes
|
|
495
|
-
async renderChunk(_, chunk, options) {
|
|
496
|
-
if (isLegacyChunk(chunk, options))
|
|
497
|
-
return null;
|
|
498
|
-
if (!Object.keys(chunk.modules).some((i) => RESOLVED_ID_RE.test(i)))
|
|
499
|
-
return null;
|
|
500
|
-
const cssPost = cssPostPlugins.get(options.dir);
|
|
501
|
-
if (!cssPost) {
|
|
502
|
-
this.warn("[unocss] failed to find vite:css-post plugin. It might be an internal bug of UnoCSS");
|
|
503
|
-
return null;
|
|
504
|
-
}
|
|
505
|
-
let { css } = await generateAll();
|
|
506
|
-
const fakeCssId = `${viteConfig.root}/${chunk.fileName}-unocss-hash.css`;
|
|
507
|
-
css = await applyCssTransform(css, fakeCssId, options.dir, this);
|
|
508
|
-
const hash = getHash(css);
|
|
509
|
-
const transformHandler = "handler" in cssPost.transform ? cssPost.transform.handler : cssPost.transform;
|
|
510
|
-
await transformHandler.call({}, getHashPlaceholder(hash), fakeCssId);
|
|
511
|
-
chunk.modules[fakeCssId] = {
|
|
512
|
-
code: null,
|
|
513
|
-
originalLength: 0,
|
|
514
|
-
removedExports: [],
|
|
515
|
-
renderedExports: [],
|
|
516
|
-
renderedLength: 0
|
|
517
|
-
};
|
|
518
|
-
return null;
|
|
519
|
-
}
|
|
520
|
-
},
|
|
521
|
-
{
|
|
522
|
-
name: "unocss:global:content",
|
|
523
|
-
enforce: "pre",
|
|
524
|
-
configResolved(config) {
|
|
525
|
-
viteConfig = config;
|
|
526
|
-
},
|
|
527
|
-
buildStart() {
|
|
528
|
-
tasks.push(setupContentExtractor(ctx, viteConfig.command === "serve"));
|
|
529
|
-
}
|
|
530
|
-
},
|
|
531
|
-
{
|
|
532
|
-
name: "unocss:global:build:generate",
|
|
533
|
-
apply: "build",
|
|
534
|
-
async renderChunk(code, chunk, options) {
|
|
535
|
-
if (isLegacyChunk(chunk, options))
|
|
536
|
-
return null;
|
|
537
|
-
if (!Object.keys(chunk.modules).some((i) => RESOLVED_ID_RE.test(i)))
|
|
538
|
-
return null;
|
|
539
|
-
const cssPost = cssPostPlugins.get(options.dir);
|
|
540
|
-
if (!cssPost) {
|
|
541
|
-
this.warn("[unocss] failed to find vite:css-post plugin. It might be an internal bug of UnoCSS");
|
|
542
|
-
return null;
|
|
543
|
-
}
|
|
544
|
-
const result = await generateAll();
|
|
545
|
-
const mappedVfsLayer = Array.from(vfsLayers).map((layer) => layer === LAYER_MARK_ALL ? layer : layer.replace(/^_/, ""));
|
|
546
|
-
const cssWithLayers = Array.from(vfsLayers).map((layer) => `#--unocss-layer-start--${layer}--{start:${layer}} ${layer === LAYER_MARK_ALL ? result.getLayers(void 0, mappedVfsLayer) : result.getLayer(layer.replace(/^_/, "")) || ""} #--unocss-layer-end--${layer}--{end:${layer}}`).join("");
|
|
547
|
-
const fakeCssId = `${viteConfig.root}/${chunk.fileName}-unocss-hash.css`;
|
|
548
|
-
const css = await applyCssTransform(cssWithLayers, fakeCssId, options.dir, this);
|
|
549
|
-
const transformHandler = "handler" in cssPost.transform ? cssPost.transform.handler : cssPost.transform;
|
|
550
|
-
await transformHandler.call({}, css, fakeCssId);
|
|
551
|
-
}
|
|
552
|
-
},
|
|
553
|
-
{
|
|
554
|
-
name: "unocss:global:build:bundle",
|
|
555
|
-
apply: "build",
|
|
556
|
-
enforce: "post",
|
|
557
|
-
// rewrite the css placeholders
|
|
558
|
-
async generateBundle(options, bundle) {
|
|
559
|
-
const checkJs = ["umd", "amd", "iife"].includes(options.format);
|
|
560
|
-
const files = Object.keys(bundle).filter((i) => i.endsWith(".css") || checkJs && i.endsWith(".js"));
|
|
561
|
-
if (!files.length)
|
|
562
|
-
return;
|
|
563
|
-
if (!vfsLayers.size) {
|
|
564
|
-
if (replaced)
|
|
565
|
-
return;
|
|
566
|
-
const msg = "[unocss] Entry module not found. Did you add `import 'uno.css'` in your main entry?";
|
|
567
|
-
this.warn(msg);
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
const getLayer = (layer, input, replace = false) => {
|
|
571
|
-
const re = new RegExp(`#--unocss-layer-start--${layer}--\\{start:${layer}\\}([\\s\\S]*?)#--unocss-layer-end--${layer}--\\{end:${layer}\\}`, "g");
|
|
572
|
-
if (replace)
|
|
573
|
-
return input.replace(re, "");
|
|
574
|
-
const match = re.exec(input);
|
|
575
|
-
if (match)
|
|
576
|
-
return match[1];
|
|
577
|
-
return "";
|
|
578
|
-
};
|
|
579
|
-
for (const file of files) {
|
|
580
|
-
const chunk = bundle[file];
|
|
581
|
-
if (chunk.type === "asset" && typeof chunk.source === "string") {
|
|
582
|
-
const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
|
|
583
|
-
chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_, layer) => {
|
|
584
|
-
replaced = true;
|
|
585
|
-
return getLayer(layer, css);
|
|
586
|
-
});
|
|
587
|
-
Array.from(vfsLayers).forEach((layer) => {
|
|
588
|
-
chunk.source = getLayer(layer, chunk.source, true);
|
|
589
|
-
});
|
|
590
|
-
} else if (chunk.type === "chunk" && typeof chunk.code === "string") {
|
|
591
|
-
const js = chunk.code.replace(HASH_PLACEHOLDER_RE, "");
|
|
592
|
-
chunk.code = await replaceAsync(js, LAYER_PLACEHOLDER_RE, async (_, layer) => {
|
|
593
|
-
replaced = true;
|
|
594
|
-
const css = getLayer(layer, js);
|
|
595
|
-
return css.replace(/\n/g, "").replace(/(?<!\\)(['"])/g, "\\$1");
|
|
596
|
-
});
|
|
597
|
-
Array.from(vfsLayers).forEach((layer) => {
|
|
598
|
-
chunk.code = getLayer(layer, chunk.code, true);
|
|
599
|
-
});
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
if (!replaced) {
|
|
603
|
-
let msg = "[unocss] does not found CSS placeholder in the generated chunks";
|
|
604
|
-
if (viteConfig.build.lib && checkJs)
|
|
605
|
-
msg += "\nIt seems you are building in library mode, it's recommended to set `build.cssCodeSplit` to true.\nSee https://github.com/vitejs/vite/issues/1579";
|
|
606
|
-
else
|
|
607
|
-
msg += "\nThis is likely an internal bug of unocss vite plugin";
|
|
608
|
-
this.error(msg);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
];
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
const WARN_TIMEOUT = 2e4;
|
|
616
|
-
const WS_EVENT_PREFIX = "unocss:hmr";
|
|
617
|
-
const HASH_LENGTH = 6;
|
|
618
|
-
function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules, onInvalidate, extract, filter, getConfig }) {
|
|
619
|
-
const servers = [];
|
|
620
|
-
const entries = /* @__PURE__ */ new Set();
|
|
621
|
-
let invalidateTimer;
|
|
622
|
-
const lastServedHash = /* @__PURE__ */ new Map();
|
|
623
|
-
let lastServedTime = Date.now();
|
|
624
|
-
let resolved = false;
|
|
625
|
-
let resolvedWarnTimer;
|
|
626
|
-
async function generateCSS(layer) {
|
|
627
|
-
await flushTasks();
|
|
628
|
-
let result;
|
|
629
|
-
let tokensSize = tokens.size;
|
|
630
|
-
do {
|
|
631
|
-
result = await uno.generate(tokens);
|
|
632
|
-
if (tokensSize === tokens.size)
|
|
633
|
-
break;
|
|
634
|
-
tokensSize = tokens.size;
|
|
635
|
-
} while (true);
|
|
636
|
-
const css = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer);
|
|
637
|
-
const hash = getHash(css || "", HASH_LENGTH);
|
|
638
|
-
lastServedHash.set(layer, hash);
|
|
639
|
-
lastServedTime = Date.now();
|
|
640
|
-
return { hash, css };
|
|
641
|
-
}
|
|
642
|
-
function invalidate(timer = 10, ids = entries) {
|
|
643
|
-
for (const server of servers) {
|
|
644
|
-
for (const id of ids) {
|
|
645
|
-
const mod = server.moduleGraph.getModuleById(id);
|
|
646
|
-
if (!mod)
|
|
647
|
-
continue;
|
|
648
|
-
server.moduleGraph.invalidateModule(mod);
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
clearTimeout(invalidateTimer);
|
|
652
|
-
invalidateTimer = setTimeout(() => {
|
|
653
|
-
lastServedHash.clear();
|
|
654
|
-
sendUpdate(ids);
|
|
655
|
-
}, timer);
|
|
656
|
-
}
|
|
657
|
-
function sendUpdate(ids) {
|
|
658
|
-
for (const server of servers) {
|
|
659
|
-
server.ws.send({
|
|
660
|
-
type: "update",
|
|
661
|
-
updates: Array.from(ids).map((id) => {
|
|
662
|
-
const mod = server.moduleGraph.getModuleById(id);
|
|
663
|
-
if (!mod)
|
|
664
|
-
return null;
|
|
665
|
-
return {
|
|
666
|
-
acceptedPath: mod.url,
|
|
667
|
-
path: mod.url,
|
|
668
|
-
timestamp: lastServedTime,
|
|
669
|
-
type: "js-update"
|
|
670
|
-
};
|
|
671
|
-
}).filter(core.notNull)
|
|
672
|
-
});
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
function setWarnTimer() {
|
|
676
|
-
if (!resolved && !resolvedWarnTimer) {
|
|
677
|
-
resolvedWarnTimer = setTimeout(() => {
|
|
678
|
-
if (process__default.env.TEST || process__default.env.NODE_ENV === "test")
|
|
679
|
-
return;
|
|
680
|
-
if (!resolved) {
|
|
681
|
-
const msg = "[unocss] Entry module not found. Did you add `import 'uno.css'` in your main entry?";
|
|
682
|
-
console.warn(msg);
|
|
683
|
-
servers.forEach(({ ws }) => ws.send({
|
|
684
|
-
type: "error",
|
|
685
|
-
err: { message: msg, stack: "" }
|
|
686
|
-
}));
|
|
687
|
-
}
|
|
688
|
-
}, WARN_TIMEOUT);
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
function clearWarnTimer() {
|
|
692
|
-
if (resolvedWarnTimer) {
|
|
693
|
-
clearTimeout(resolvedWarnTimer);
|
|
694
|
-
resolvedWarnTimer = void 0;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
onInvalidate(() => {
|
|
698
|
-
invalidate(10, /* @__PURE__ */ new Set([...entries, ...affectedModules]));
|
|
699
|
-
});
|
|
700
|
-
return [
|
|
701
|
-
{
|
|
702
|
-
name: "unocss:global",
|
|
703
|
-
apply: "serve",
|
|
704
|
-
enforce: "pre",
|
|
705
|
-
async configureServer(_server) {
|
|
706
|
-
servers.push(_server);
|
|
707
|
-
_server.ws.on(WS_EVENT_PREFIX, async ([layer]) => {
|
|
708
|
-
const preHash = lastServedHash.get(layer);
|
|
709
|
-
await generateCSS(layer);
|
|
710
|
-
if (lastServedHash.get(layer) !== preHash)
|
|
711
|
-
sendUpdate(entries);
|
|
712
|
-
});
|
|
713
|
-
},
|
|
714
|
-
buildStart() {
|
|
715
|
-
uno.generate([], { preflights: true });
|
|
716
|
-
},
|
|
717
|
-
transform(code, id) {
|
|
718
|
-
if (filter(code, id))
|
|
719
|
-
tasks.push(extract(code, id));
|
|
720
|
-
return null;
|
|
721
|
-
},
|
|
722
|
-
transformIndexHtml: {
|
|
723
|
-
order: "pre",
|
|
724
|
-
handler(code, { filename }) {
|
|
725
|
-
setWarnTimer();
|
|
726
|
-
tasks.push(extract(code, filename));
|
|
727
|
-
},
|
|
728
|
-
// Compatibility with Legacy Vite
|
|
729
|
-
enforce: "pre",
|
|
730
|
-
transform(code, { filename }) {
|
|
731
|
-
setWarnTimer();
|
|
732
|
-
tasks.push(extract(code, filename));
|
|
733
|
-
}
|
|
734
|
-
},
|
|
735
|
-
resolveId(id) {
|
|
736
|
-
const entry = resolveId(id);
|
|
737
|
-
if (entry) {
|
|
738
|
-
resolved = true;
|
|
739
|
-
clearWarnTimer();
|
|
740
|
-
entries.add(entry);
|
|
741
|
-
return entry;
|
|
742
|
-
}
|
|
743
|
-
},
|
|
744
|
-
async load(id) {
|
|
745
|
-
const layer = resolveLayer(getPath(id));
|
|
746
|
-
if (!layer)
|
|
747
|
-
return null;
|
|
748
|
-
const { hash, css } = await generateCSS(layer);
|
|
749
|
-
return {
|
|
750
|
-
// add hash to the chunk of CSS that it will send back to client to check if there is new CSS generated
|
|
751
|
-
code: `__uno_hash_${hash}{--:'';}${css}`,
|
|
752
|
-
map: { mappings: "" }
|
|
753
|
-
};
|
|
754
|
-
},
|
|
755
|
-
closeBundle() {
|
|
756
|
-
clearWarnTimer();
|
|
757
|
-
}
|
|
758
|
-
},
|
|
759
|
-
{
|
|
760
|
-
name: "unocss:global:post",
|
|
761
|
-
apply(config, env) {
|
|
762
|
-
return env.command === "serve" && !config.build?.ssr;
|
|
763
|
-
},
|
|
764
|
-
enforce: "post",
|
|
765
|
-
async transform(code, id) {
|
|
766
|
-
const layer = resolveLayer(getPath(id));
|
|
767
|
-
if (layer && code.includes("import.meta.hot")) {
|
|
768
|
-
let hmr = `
|
|
769
|
-
try {
|
|
770
|
-
let hash = __vite__css.match(/__uno_hash_(\\w{${HASH_LENGTH}})/)
|
|
771
|
-
hash = hash && hash[1]
|
|
772
|
-
if (!hash)
|
|
773
|
-
console.warn('[unocss-hmr]', 'failed to get unocss hash, hmr might not work')
|
|
774
|
-
else
|
|
775
|
-
await import.meta.hot.send('${WS_EVENT_PREFIX}', ['${layer}']);
|
|
776
|
-
} catch (e) {
|
|
777
|
-
console.warn('[unocss-hmr]', e)
|
|
778
|
-
}
|
|
779
|
-
if (!import.meta.url.includes('?'))
|
|
780
|
-
await new Promise(resolve => setTimeout(resolve, 100))`;
|
|
781
|
-
const config = await getConfig();
|
|
782
|
-
if (config.hmrTopLevelAwait === false)
|
|
783
|
-
hmr = `;(async function() {${hmr}
|
|
784
|
-
})()`;
|
|
785
|
-
hmr = `
|
|
786
|
-
if (import.meta.hot) {${hmr}}`;
|
|
787
|
-
const s = new MagicString__default(code);
|
|
788
|
-
s.append(hmr);
|
|
789
|
-
return {
|
|
790
|
-
code: s.toString(),
|
|
791
|
-
map: s.generateMap()
|
|
792
|
-
};
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
];
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
function GlobalModePlugin(ctx) {
|
|
800
|
-
return [
|
|
801
|
-
...GlobalModeBuildPlugin(ctx),
|
|
802
|
-
...GlobalModeDevPlugin(ctx)
|
|
803
|
-
];
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
const VIRTUAL_PREFIX = "/@unocss/";
|
|
807
|
-
const SCOPE_IMPORT_RE = / from (['"])(@unocss\/scope)\1/;
|
|
808
|
-
function PerModuleModePlugin({ uno, filter }) {
|
|
809
|
-
const moduleMap = /* @__PURE__ */ new Map();
|
|
810
|
-
let server;
|
|
811
|
-
const invalidate = (hash) => {
|
|
812
|
-
if (!server)
|
|
813
|
-
return;
|
|
814
|
-
const id = `${VIRTUAL_PREFIX}${hash}.css`;
|
|
815
|
-
const mod = server.moduleGraph.getModuleById(id);
|
|
816
|
-
if (!mod)
|
|
817
|
-
return;
|
|
818
|
-
server.moduleGraph.invalidateModule(mod);
|
|
819
|
-
server.ws.send({
|
|
820
|
-
type: "update",
|
|
821
|
-
updates: [{
|
|
822
|
-
acceptedPath: id,
|
|
823
|
-
path: id,
|
|
824
|
-
timestamp: +Date.now(),
|
|
825
|
-
type: "js-update"
|
|
826
|
-
}]
|
|
827
|
-
});
|
|
828
|
-
};
|
|
829
|
-
return [
|
|
830
|
-
{
|
|
831
|
-
name: "unocss:module-scope:pre",
|
|
832
|
-
enforce: "pre",
|
|
833
|
-
resolveId(id) {
|
|
834
|
-
const entry = resolveId(id);
|
|
835
|
-
if (entry)
|
|
836
|
-
return entry;
|
|
837
|
-
},
|
|
838
|
-
async load(id) {
|
|
839
|
-
const layer = resolveLayer(getPath(id));
|
|
840
|
-
if (!layer)
|
|
841
|
-
return null;
|
|
842
|
-
const { css } = await uno.generate("", { preflights: true });
|
|
843
|
-
if (!css)
|
|
844
|
-
return null;
|
|
845
|
-
return {
|
|
846
|
-
code: css,
|
|
847
|
-
map: null
|
|
848
|
-
};
|
|
849
|
-
},
|
|
850
|
-
async transform(code, id) {
|
|
851
|
-
if (!filter(code, id))
|
|
852
|
-
return;
|
|
853
|
-
const hash = getHash(id);
|
|
854
|
-
const hasScope = SCOPE_IMPORT_RE.test(code);
|
|
855
|
-
const { css } = await uno.generate(code, { id, scope: hasScope ? `.${hash}` : void 0, preflights: false });
|
|
856
|
-
if (!css && !hasScope)
|
|
857
|
-
return null;
|
|
858
|
-
if (hasScope)
|
|
859
|
-
code = code.replace(SCOPE_IMPORT_RE, ` from 'data:text/javascript;base64,${node_buffer.Buffer.from(`export default () => "${hash}"`).toString("base64")}'`);
|
|
860
|
-
moduleMap.set(hash, [id, css]);
|
|
861
|
-
invalidate(hash);
|
|
862
|
-
return null;
|
|
863
|
-
}
|
|
864
|
-
},
|
|
865
|
-
{
|
|
866
|
-
name: "unocss:module-scope",
|
|
867
|
-
enforce: "post",
|
|
868
|
-
configureServer(_server) {
|
|
869
|
-
server = _server;
|
|
870
|
-
},
|
|
871
|
-
async transform(code, id) {
|
|
872
|
-
if (!filter(code, id))
|
|
873
|
-
return;
|
|
874
|
-
const hash = getHash(id);
|
|
875
|
-
invalidate(hash);
|
|
876
|
-
const module = moduleMap.get(hash) || [];
|
|
877
|
-
if (module.length) {
|
|
878
|
-
return {
|
|
879
|
-
code: `import "${VIRTUAL_PREFIX}${hash}.css";${code}`,
|
|
880
|
-
map: null
|
|
881
|
-
};
|
|
882
|
-
}
|
|
883
|
-
},
|
|
884
|
-
resolveId(id) {
|
|
885
|
-
return id.startsWith(VIRTUAL_PREFIX) ? id : null;
|
|
886
|
-
},
|
|
887
|
-
load(id) {
|
|
888
|
-
if (!id.startsWith(VIRTUAL_PREFIX))
|
|
889
|
-
return null;
|
|
890
|
-
const hash = id.slice(VIRTUAL_PREFIX.length, -".css".length);
|
|
891
|
-
const [source, css] = moduleMap.get(hash) || [];
|
|
892
|
-
if (source)
|
|
893
|
-
this.addWatchFile(source);
|
|
894
|
-
return `
|
|
895
|
-
/* unocss ${source} */
|
|
896
|
-
${css}`;
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
];
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
function VueScopedPlugin({ uno, ready }) {
|
|
903
|
-
let filter = pluginutils.createFilter([/\.vue$/], defaultPipelineExclude);
|
|
904
|
-
async function transformSFC(code) {
|
|
905
|
-
const { css } = await uno.generate(code);
|
|
906
|
-
if (!css)
|
|
907
|
-
return null;
|
|
908
|
-
return `${code}
|
|
909
|
-
<style scoped>${css}</style>`;
|
|
910
|
-
}
|
|
911
|
-
return {
|
|
912
|
-
name: "unocss:vue-scoped",
|
|
913
|
-
enforce: "pre",
|
|
914
|
-
async configResolved() {
|
|
915
|
-
const { config } = await ready;
|
|
916
|
-
filter = config.content?.pipeline === false ? () => false : pluginutils.createFilter(
|
|
917
|
-
config.content?.pipeline?.include ?? config.include ?? [/\.vue$/],
|
|
918
|
-
config.content?.pipeline?.exclude ?? config.exclude ?? defaultPipelineExclude
|
|
919
|
-
);
|
|
920
|
-
},
|
|
921
|
-
transform(code, id) {
|
|
922
|
-
if (!filter(id) || !id.endsWith(".vue"))
|
|
923
|
-
return;
|
|
924
|
-
return transformSFC(code);
|
|
925
|
-
},
|
|
926
|
-
handleHotUpdate(ctx) {
|
|
927
|
-
const read = ctx.read;
|
|
928
|
-
if (filter(ctx.file)) {
|
|
929
|
-
ctx.read = async () => {
|
|
930
|
-
const code = await read();
|
|
931
|
-
return await transformSFC(code) || code;
|
|
932
|
-
};
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
};
|
|
936
|
-
}
|
|
937
|
-
|
|
938
|
-
function ShadowDomModuleModePlugin({ uno }) {
|
|
939
|
-
const partExtractorRegex = /^part-\[(.+)]:/;
|
|
940
|
-
const nameRegexp = /<([^\s^!>]+)\s*([^>]*)>/;
|
|
941
|
-
const vueSFCStyleRE = new RegExp(`<style.*>[\\s\\S]*${CSS_PLACEHOLDER}[\\s\\S]*<\\/style>`);
|
|
942
|
-
const checkElement = (useParts, idxResolver, element) => {
|
|
943
|
-
if (!element)
|
|
944
|
-
return null;
|
|
945
|
-
const applyParts = useParts.filter((p) => element[2].includes(p.rule));
|
|
946
|
-
if (applyParts.length === 0)
|
|
947
|
-
return null;
|
|
948
|
-
const name = element[1];
|
|
949
|
-
const idx = idxResolver(name);
|
|
950
|
-
return {
|
|
951
|
-
name,
|
|
952
|
-
entries: applyParts.map(({ rule, part }) => [
|
|
953
|
-
`.${rule.replace(/[:[\]]/g, "\\$&")}::part(${part})`,
|
|
954
|
-
`${name}:nth-of-type(${idx})::part(${part})`
|
|
955
|
-
])
|
|
956
|
-
};
|
|
957
|
-
};
|
|
958
|
-
const idxMapFactory = () => {
|
|
959
|
-
const elementIdxMap = /* @__PURE__ */ new Map();
|
|
960
|
-
return {
|
|
961
|
-
idxResolver: (name) => {
|
|
962
|
-
let idx = elementIdxMap.get(name);
|
|
963
|
-
if (!idx) {
|
|
964
|
-
idx = 1;
|
|
965
|
-
elementIdxMap.set(name, idx);
|
|
966
|
-
}
|
|
967
|
-
return idx;
|
|
968
|
-
},
|
|
969
|
-
incrementIdx: (name) => {
|
|
970
|
-
elementIdxMap.set(name, elementIdxMap.get(name) + 1);
|
|
971
|
-
}
|
|
972
|
-
};
|
|
973
|
-
};
|
|
974
|
-
const transformWebComponent = async (code, id) => {
|
|
975
|
-
if (!code.match(CSS_PLACEHOLDER))
|
|
976
|
-
return code;
|
|
977
|
-
let { css, matched } = await uno.generate(code, {
|
|
978
|
-
preflights: true,
|
|
979
|
-
safelist: true
|
|
980
|
-
});
|
|
981
|
-
if (css && matched) {
|
|
982
|
-
const useParts = Array.from(matched).reduce((acc, rule) => {
|
|
983
|
-
const matcher = rule.match(partExtractorRegex);
|
|
984
|
-
if (matcher)
|
|
985
|
-
acc.push({ part: matcher[1], rule });
|
|
986
|
-
return acc;
|
|
987
|
-
}, new Array());
|
|
988
|
-
if (useParts.length > 0) {
|
|
989
|
-
let useCode = code;
|
|
990
|
-
let element;
|
|
991
|
-
const partsToApply = /* @__PURE__ */ new Map();
|
|
992
|
-
const { idxResolver, incrementIdx } = idxMapFactory();
|
|
993
|
-
while (element = nameRegexp.exec(useCode)) {
|
|
994
|
-
const result = checkElement(
|
|
995
|
-
useParts,
|
|
996
|
-
idxResolver,
|
|
997
|
-
element
|
|
998
|
-
);
|
|
999
|
-
if (result) {
|
|
1000
|
-
result.entries.forEach(([name, replacement]) => {
|
|
1001
|
-
let list = partsToApply.get(name);
|
|
1002
|
-
if (!list) {
|
|
1003
|
-
list = [];
|
|
1004
|
-
partsToApply.set(name, list);
|
|
1005
|
-
}
|
|
1006
|
-
list.push(replacement);
|
|
1007
|
-
});
|
|
1008
|
-
incrementIdx(result.name);
|
|
1009
|
-
}
|
|
1010
|
-
useCode = useCode.slice(element[0].length + 1);
|
|
1011
|
-
}
|
|
1012
|
-
if (partsToApply.size > 0) {
|
|
1013
|
-
css = Array.from(partsToApply.entries()).reduce((k, [r, name]) => {
|
|
1014
|
-
return k.replace(r, name.join(",\n"));
|
|
1015
|
-
}, css);
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
if (id.includes("?vue&type=style") || id.endsWith(".vue") && vueSFCStyleRE.test(code))
|
|
1020
|
-
return code.replace(new RegExp(`(\\/\\*\\s*)?${CSS_PLACEHOLDER}(\\s*\\*\\/)?`), css || "");
|
|
1021
|
-
return code.replace(CSS_PLACEHOLDER, css?.replace(/\\/g, "\\\\")?.replace(/`/g, "\\`") ?? "");
|
|
1022
|
-
};
|
|
1023
|
-
return {
|
|
1024
|
-
name: "unocss:shadow-dom",
|
|
1025
|
-
enforce: "pre",
|
|
1026
|
-
async transform(code, id) {
|
|
1027
|
-
return transformWebComponent(code, id);
|
|
1028
|
-
},
|
|
1029
|
-
handleHotUpdate(ctx) {
|
|
1030
|
-
const read = ctx.read;
|
|
1031
|
-
ctx.read = async () => {
|
|
1032
|
-
const code = await read();
|
|
1033
|
-
return await transformWebComponent(code, ctx.file);
|
|
1034
|
-
};
|
|
1035
|
-
}
|
|
1036
|
-
};
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
function ConfigHMRPlugin(ctx) {
|
|
1040
|
-
const { ready, uno } = ctx;
|
|
1041
|
-
return {
|
|
1042
|
-
name: "unocss:config",
|
|
1043
|
-
async configResolved(config) {
|
|
1044
|
-
await ctx.updateRoot(config.root);
|
|
1045
|
-
},
|
|
1046
|
-
async configureServer(server) {
|
|
1047
|
-
uno.config.envMode = "dev";
|
|
1048
|
-
const { sources } = await ready;
|
|
1049
|
-
if (!sources.length)
|
|
1050
|
-
return;
|
|
1051
|
-
server.watcher.add(sources);
|
|
1052
|
-
server.watcher.on("change", async (p) => {
|
|
1053
|
-
if (!sources.includes(p))
|
|
1054
|
-
return;
|
|
1055
|
-
await ctx.reloadConfig();
|
|
1056
|
-
server.ws.send({
|
|
1057
|
-
type: "custom",
|
|
1058
|
-
event: "unocss:config-changed"
|
|
1059
|
-
});
|
|
1060
|
-
});
|
|
1061
|
-
}
|
|
1062
|
-
};
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
function createTransformerPlugins(ctx) {
|
|
1066
|
-
const orders = ["default", "pre", "post"];
|
|
1067
|
-
return orders.map((_order) => {
|
|
1068
|
-
const order = _order === "default" ? void 0 : _order;
|
|
1069
|
-
const htmlHandler = (code) => {
|
|
1070
|
-
return applyTransformers(ctx, code, "index.html", order).then((t) => t?.code);
|
|
1071
|
-
};
|
|
1072
|
-
return {
|
|
1073
|
-
name: `unocss:transformers:${order}`,
|
|
1074
|
-
enforce: order,
|
|
1075
|
-
transform(code, id) {
|
|
1076
|
-
return applyTransformers(ctx, code, id, order);
|
|
1077
|
-
},
|
|
1078
|
-
transformIndexHtml: {
|
|
1079
|
-
order,
|
|
1080
|
-
handler: htmlHandler,
|
|
1081
|
-
// Compatibility with Legacy Vite
|
|
1082
|
-
enforce: order,
|
|
1083
|
-
transform: htmlHandler
|
|
1084
|
-
}
|
|
1085
|
-
};
|
|
1086
|
-
});
|
|
1087
|
-
}
|
|
1088
|
-
|
|
1089
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname : node_path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
|
|
1090
|
-
const DEVTOOLS_MODULE_ID = "virtual:unocss-devtools";
|
|
1091
|
-
const MOCK_CLASSES_MODULE_ID = "virtual:unocss-mock-classes";
|
|
1092
|
-
const MOCK_CLASSES_PATH = "/@unocss/mock-classes";
|
|
1093
|
-
const DEVTOOLS_PATH = "/@unocss/devtools";
|
|
1094
|
-
const DEVTOOLS_CSS_PATH = "/@unocss/devtools.css";
|
|
1095
|
-
const devtoolCss = /* @__PURE__ */ new Set();
|
|
1096
|
-
const MODULES_MAP = {
|
|
1097
|
-
[DEVTOOLS_MODULE_ID]: DEVTOOLS_PATH,
|
|
1098
|
-
[MOCK_CLASSES_MODULE_ID]: MOCK_CLASSES_PATH
|
|
1099
|
-
};
|
|
1100
|
-
const BASE_POST_PATH = "/@unocss-devtools-update";
|
|
1101
|
-
function getBodyJson(req) {
|
|
1102
|
-
return new Promise((resolve2, reject) => {
|
|
1103
|
-
let body = "";
|
|
1104
|
-
req.on("data", (chunk) => body += chunk);
|
|
1105
|
-
req.on("error", reject);
|
|
1106
|
-
req.on("end", () => {
|
|
1107
|
-
try {
|
|
1108
|
-
resolve2(JSON.parse(body) || {});
|
|
1109
|
-
} catch (e) {
|
|
1110
|
-
reject(e);
|
|
1111
|
-
}
|
|
1112
|
-
});
|
|
1113
|
-
});
|
|
1114
|
-
}
|
|
1115
|
-
function createDevtoolsPlugin(ctx, pluginConfig) {
|
|
1116
|
-
let config;
|
|
1117
|
-
let server;
|
|
1118
|
-
let clientCode = "";
|
|
1119
|
-
let devtoolTimer;
|
|
1120
|
-
let lastUpdate = Date.now();
|
|
1121
|
-
let postPath = BASE_POST_PATH;
|
|
1122
|
-
function toClass(name) {
|
|
1123
|
-
return `${core.toEscapedSelector(name)}{}`;
|
|
1124
|
-
}
|
|
1125
|
-
function updateDevtoolClass() {
|
|
1126
|
-
clearTimeout(devtoolTimer);
|
|
1127
|
-
devtoolTimer = setTimeout(() => {
|
|
1128
|
-
lastUpdate = Date.now();
|
|
1129
|
-
if (!server)
|
|
1130
|
-
return;
|
|
1131
|
-
const mod = server.moduleGraph.getModuleById(DEVTOOLS_CSS_PATH);
|
|
1132
|
-
if (!mod)
|
|
1133
|
-
return;
|
|
1134
|
-
server.moduleGraph.invalidateModule(mod);
|
|
1135
|
-
server.ws.send({
|
|
1136
|
-
type: "update",
|
|
1137
|
-
updates: [{
|
|
1138
|
-
acceptedPath: DEVTOOLS_CSS_PATH,
|
|
1139
|
-
path: DEVTOOLS_CSS_PATH,
|
|
1140
|
-
timestamp: lastUpdate,
|
|
1141
|
-
type: "js-update"
|
|
1142
|
-
}]
|
|
1143
|
-
});
|
|
1144
|
-
}, 100);
|
|
1145
|
-
}
|
|
1146
|
-
async function getMockClassesInjector() {
|
|
1147
|
-
const suggest = Object.keys(ctx.uno.config.rulesStaticMap);
|
|
1148
|
-
const comment = "/* unocss CSS mock class names for devtools auto-completion */\n";
|
|
1149
|
-
const css = suggest.map(toClass).join("");
|
|
1150
|
-
return `
|
|
1151
|
-
const style = document.createElement('style')
|
|
1152
|
-
style.setAttribute('type', 'text/css')
|
|
1153
|
-
style.innerHTML = ${JSON.stringify(comment + css)}
|
|
1154
|
-
document.head.prepend(style)
|
|
1155
|
-
`;
|
|
1156
|
-
}
|
|
1157
|
-
return [
|
|
1158
|
-
{
|
|
1159
|
-
name: "unocss:devtools",
|
|
1160
|
-
configResolved(_config) {
|
|
1161
|
-
config = _config;
|
|
1162
|
-
postPath = `${config.base?.replace(/\/$/, "") ?? ""}${BASE_POST_PATH}`;
|
|
1163
|
-
},
|
|
1164
|
-
configureServer(_server) {
|
|
1165
|
-
server = _server;
|
|
1166
|
-
server.middlewares.use(async (req, res, next) => {
|
|
1167
|
-
if (req.url !== postPath)
|
|
1168
|
-
return next();
|
|
1169
|
-
try {
|
|
1170
|
-
const data = await getBodyJson(req);
|
|
1171
|
-
const type = data?.type;
|
|
1172
|
-
let changed = false;
|
|
1173
|
-
switch (type) {
|
|
1174
|
-
case "add-classes":
|
|
1175
|
-
data.data.forEach((key) => {
|
|
1176
|
-
if (!devtoolCss.has(key)) {
|
|
1177
|
-
devtoolCss.add(key);
|
|
1178
|
-
changed = true;
|
|
1179
|
-
}
|
|
1180
|
-
});
|
|
1181
|
-
if (changed)
|
|
1182
|
-
updateDevtoolClass();
|
|
1183
|
-
}
|
|
1184
|
-
res.statusCode = 200;
|
|
1185
|
-
} catch (e) {
|
|
1186
|
-
console.error(e);
|
|
1187
|
-
res.statusCode = 500;
|
|
1188
|
-
}
|
|
1189
|
-
res.end();
|
|
1190
|
-
});
|
|
1191
|
-
},
|
|
1192
|
-
resolveId(id) {
|
|
1193
|
-
if (id === DEVTOOLS_CSS_PATH)
|
|
1194
|
-
return DEVTOOLS_CSS_PATH;
|
|
1195
|
-
return MODULES_MAP[id];
|
|
1196
|
-
},
|
|
1197
|
-
async load(id) {
|
|
1198
|
-
if (id === DEVTOOLS_PATH) {
|
|
1199
|
-
if (!clientCode) {
|
|
1200
|
-
clientCode = [
|
|
1201
|
-
await fs__default$1.promises.readFile(node_path.resolve(_dirname, "client.mjs"), "utf-8"),
|
|
1202
|
-
`import('${MOCK_CLASSES_MODULE_ID}')`,
|
|
1203
|
-
`import('${DEVTOOLS_CSS_PATH}')`
|
|
1204
|
-
].join("\n").replace("__POST_PATH__", `${config.server?.origin ?? ""}${postPath}`).replace("__POST_FETCH_MODE__", pluginConfig.fetchMode ?? "cors");
|
|
1205
|
-
}
|
|
1206
|
-
return config.command === "build" ? "" : clientCode;
|
|
1207
|
-
} else if (id === MOCK_CLASSES_PATH) {
|
|
1208
|
-
return await getMockClassesInjector();
|
|
1209
|
-
} else if (id === DEVTOOLS_CSS_PATH) {
|
|
1210
|
-
const { css } = await ctx.uno.generate(devtoolCss);
|
|
1211
|
-
return css;
|
|
1212
|
-
}
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
];
|
|
1216
|
-
}
|
|
1217
|
-
|
|
1218
|
-
function defineConfig(config) {
|
|
1219
|
-
return config;
|
|
1220
|
-
}
|
|
1221
|
-
function UnocssPlugin(configOrPath, defaults = {}) {
|
|
1222
|
-
const ctx = createContext(configOrPath, {
|
|
1223
|
-
envMode: process__default.env.NODE_ENV === "development" ? "dev" : "build",
|
|
1224
|
-
...defaults
|
|
1225
|
-
});
|
|
1226
|
-
const inlineConfig = configOrPath && typeof configOrPath !== "string" ? configOrPath : {};
|
|
1227
|
-
const mode = inlineConfig.mode ?? "global";
|
|
1228
|
-
const plugins = [
|
|
1229
|
-
ConfigHMRPlugin(ctx),
|
|
1230
|
-
...createTransformerPlugins(ctx),
|
|
1231
|
-
...createDevtoolsPlugin(ctx, inlineConfig),
|
|
1232
|
-
{
|
|
1233
|
-
name: "unocss:api",
|
|
1234
|
-
api: {
|
|
1235
|
-
getContext: () => ctx,
|
|
1236
|
-
getMode: () => mode
|
|
1237
|
-
}
|
|
1238
|
-
}
|
|
1239
|
-
];
|
|
1240
|
-
if (inlineConfig.inspector !== false)
|
|
1241
|
-
plugins.push(UnocssInspector__default(ctx));
|
|
1242
|
-
if (mode === "per-module") {
|
|
1243
|
-
plugins.push(...PerModuleModePlugin(ctx));
|
|
1244
|
-
} else if (mode === "vue-scoped") {
|
|
1245
|
-
plugins.push(VueScopedPlugin(ctx));
|
|
1246
|
-
} else if (mode === "svelte-scoped") {
|
|
1247
|
-
throw new Error("[unocss] svelte-scoped mode is now its own package, please use @unocss/svelte-scoped according to the docs");
|
|
1248
|
-
} else if (mode === "shadow-dom") {
|
|
1249
|
-
plugins.push(ShadowDomModuleModePlugin(ctx));
|
|
1250
|
-
} else if (mode === "global") {
|
|
1251
|
-
plugins.push(...GlobalModePlugin(ctx));
|
|
1252
|
-
} else if (mode === "dist-chunk") {
|
|
1253
|
-
plugins.push(
|
|
1254
|
-
ChunkModeBuildPlugin(ctx),
|
|
1255
|
-
...GlobalModeDevPlugin(ctx)
|
|
1256
|
-
);
|
|
1257
|
-
} else {
|
|
1258
|
-
throw new Error(`[unocss] unknown mode "${mode}"`);
|
|
1259
|
-
}
|
|
1260
|
-
return plugins.filter(Boolean);
|
|
1261
|
-
}
|
|
1262
|
-
|
|
1263
|
-
exports.ChunkModeBuildPlugin = ChunkModeBuildPlugin;
|
|
1264
|
-
exports.GlobalModeBuildPlugin = GlobalModeBuildPlugin;
|
|
1265
|
-
exports.GlobalModeDevPlugin = GlobalModeDevPlugin;
|
|
1266
|
-
exports.GlobalModePlugin = GlobalModePlugin;
|
|
1267
|
-
exports.PerModuleModePlugin = PerModuleModePlugin;
|
|
1268
|
-
exports.VueScopedPlugin = VueScopedPlugin;
|
|
1269
|
-
exports.default = UnocssPlugin;
|
|
1270
|
-
exports.defineConfig = defineConfig;
|