@unocss/vite 0.38.1 → 0.39.1
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/dist/index.cjs +65 -44
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +65 -44
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
const UnocssInspector = require('@unocss/inspector');
|
|
6
|
+
const path = require('path');
|
|
6
7
|
const crypto = require('crypto');
|
|
7
8
|
const core = require('@unocss/core');
|
|
8
9
|
const pluginutils = require('@rollup/pluginutils');
|
|
9
10
|
const MagicString = require('magic-string');
|
|
10
11
|
const fs = require('fs');
|
|
11
|
-
const path = require('path');
|
|
12
12
|
const url = require('url');
|
|
13
13
|
const config = require('@unocss/config');
|
|
14
14
|
|
|
@@ -29,17 +29,16 @@ function resolveId(id) {
|
|
|
29
29
|
for (const alias of VIRTUAL_ENTRY_ALIAS) {
|
|
30
30
|
const match = id.match(alias);
|
|
31
31
|
if (match) {
|
|
32
|
-
return match[1] ? {
|
|
33
|
-
id: `/__uno_${match[1]}.css`,
|
|
34
|
-
layer: match[1]
|
|
35
|
-
} : {
|
|
36
|
-
id: "/__uno.css",
|
|
37
|
-
layer: LAYER_MARK_ALL
|
|
38
|
-
};
|
|
32
|
+
return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
|
|
39
33
|
}
|
|
40
34
|
}
|
|
41
35
|
}
|
|
42
|
-
const RESOLVED_ID_RE = /\/__uno(_.*?)?\.css$/;
|
|
36
|
+
const RESOLVED_ID_RE = /\/__uno(?:(_.*?))?\.css$/;
|
|
37
|
+
function resolveLayer(id) {
|
|
38
|
+
const match = id.match(RESOLVED_ID_RE);
|
|
39
|
+
if (match)
|
|
40
|
+
return match[1] || LAYER_MARK_ALL;
|
|
41
|
+
}
|
|
43
42
|
const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
|
|
44
43
|
function getLayerPlaceholder(layer) {
|
|
45
44
|
return `#--unocss--{layer:${layer}}`;
|
|
@@ -60,6 +59,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
60
59
|
const uno = core.createGenerator(rawConfig, defaults);
|
|
61
60
|
let rollupFilter = pluginutils.createFilter(defaultInclude, defaultExclude);
|
|
62
61
|
const invalidations = [];
|
|
62
|
+
const reloadListeners = [];
|
|
63
63
|
const modules = new core.BetterMap();
|
|
64
64
|
const tokens = /* @__PURE__ */ new Set();
|
|
65
65
|
let ready = reloadConfig();
|
|
@@ -73,6 +73,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
73
73
|
tokens.clear();
|
|
74
74
|
await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
|
|
75
75
|
invalidate();
|
|
76
|
+
dispatchReload();
|
|
76
77
|
const presets = /* @__PURE__ */ new Set();
|
|
77
78
|
uno.config.presets.forEach((i) => {
|
|
78
79
|
if (!i.name)
|
|
@@ -94,6 +95,9 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
94
95
|
function invalidate() {
|
|
95
96
|
invalidations.forEach((cb) => cb());
|
|
96
97
|
}
|
|
98
|
+
function dispatchReload() {
|
|
99
|
+
reloadListeners.forEach((cb) => cb());
|
|
100
|
+
}
|
|
97
101
|
async function extract(code, id) {
|
|
98
102
|
if (id)
|
|
99
103
|
modules.set(id, code);
|
|
@@ -123,6 +127,9 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
123
127
|
},
|
|
124
128
|
filter,
|
|
125
129
|
reloadConfig,
|
|
130
|
+
onReload(fn) {
|
|
131
|
+
reloadListeners.push(fn);
|
|
132
|
+
},
|
|
126
133
|
uno,
|
|
127
134
|
extract,
|
|
128
135
|
getConfig,
|
|
@@ -202,13 +209,13 @@ function ChunkModeBuildPlugin({ uno, filter }) {
|
|
|
202
209
|
};
|
|
203
210
|
}
|
|
204
211
|
|
|
205
|
-
function GlobalModeBuildPlugin({ uno, ready, extract, tokens,
|
|
206
|
-
const
|
|
212
|
+
function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig }) {
|
|
213
|
+
const vfsLayers = /* @__PURE__ */ new Set();
|
|
207
214
|
const layerImporterMap = /* @__PURE__ */ new Map();
|
|
208
215
|
let tasks = [];
|
|
209
216
|
const cssPostPlugins = /* @__PURE__ */ new Map();
|
|
210
217
|
const cssPlugins = /* @__PURE__ */ new Map();
|
|
211
|
-
async function
|
|
218
|
+
async function applyCssTransform(css, id, dir) {
|
|
212
219
|
const {
|
|
213
220
|
postcss = true
|
|
214
221
|
} = await getConfig();
|
|
@@ -224,6 +231,16 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
224
231
|
css = css.replace(/[\n\r]/g, "");
|
|
225
232
|
return css;
|
|
226
233
|
}
|
|
234
|
+
let lastTokenSize = 0;
|
|
235
|
+
let lastResult;
|
|
236
|
+
async function generateAll() {
|
|
237
|
+
await Promise.all(tasks);
|
|
238
|
+
if (lastResult && lastTokenSize === tokens.size)
|
|
239
|
+
return lastResult;
|
|
240
|
+
lastResult = await uno.generate(tokens, { minify: true });
|
|
241
|
+
lastTokenSize = tokens.size;
|
|
242
|
+
return lastResult;
|
|
243
|
+
}
|
|
227
244
|
return [
|
|
228
245
|
{
|
|
229
246
|
name: "unocss:global:build:scan",
|
|
@@ -231,6 +248,8 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
231
248
|
enforce: "pre",
|
|
232
249
|
buildStart() {
|
|
233
250
|
tasks = [];
|
|
251
|
+
lastTokenSize = 0;
|
|
252
|
+
lastResult = void 0;
|
|
234
253
|
},
|
|
235
254
|
transform(code, id) {
|
|
236
255
|
if (filter(code, id))
|
|
@@ -246,14 +265,17 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
246
265
|
resolveId(id, importer) {
|
|
247
266
|
const entry = resolveId(id);
|
|
248
267
|
if (entry) {
|
|
249
|
-
|
|
250
|
-
if (
|
|
251
|
-
|
|
252
|
-
|
|
268
|
+
const layer = resolveLayer(entry);
|
|
269
|
+
if (layer) {
|
|
270
|
+
vfsLayers.add(layer);
|
|
271
|
+
if (importer)
|
|
272
|
+
layerImporterMap.set(importer, entry);
|
|
273
|
+
}
|
|
274
|
+
return entry;
|
|
253
275
|
}
|
|
254
276
|
},
|
|
255
277
|
load(id) {
|
|
256
|
-
const layer =
|
|
278
|
+
const layer = resolveLayer(getPath(id));
|
|
257
279
|
if (layer)
|
|
258
280
|
return getLayerPlaceholder(layer);
|
|
259
281
|
},
|
|
@@ -263,31 +285,28 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
263
285
|
const layerKey = layerImporterMap.get(id);
|
|
264
286
|
if (!importedIds.includes(layerKey)) {
|
|
265
287
|
layerImporterMap.delete(id);
|
|
266
|
-
|
|
288
|
+
vfsLayers.delete(resolveLayer(layerKey));
|
|
267
289
|
}
|
|
268
290
|
},
|
|
269
291
|
async configResolved(config) {
|
|
270
|
-
|
|
271
|
-
|
|
292
|
+
const distDir = path.resolve(config.root, config.build.outDir);
|
|
293
|
+
cssPostPlugins.set(distDir, config.plugins.find((i) => i.name === "vite:css-post"));
|
|
294
|
+
cssPlugins.set(distDir, config.plugins.find((i) => i.name === "vite:css"));
|
|
272
295
|
await ready;
|
|
273
296
|
},
|
|
274
297
|
async renderChunk(_, chunk, options) {
|
|
275
|
-
if (!
|
|
298
|
+
if (!Object.keys(chunk.modules).some((i) => i.match(RESOLVED_ID_RE)))
|
|
276
299
|
return null;
|
|
277
|
-
const
|
|
278
|
-
if (!
|
|
300
|
+
const cssPost = cssPostPlugins.get(options.dir);
|
|
301
|
+
if (!cssPost) {
|
|
302
|
+
this.warn("[unocss] failed to find vite:css-post plugin. It might be an internal bug of UnoCSS");
|
|
279
303
|
return null;
|
|
304
|
+
}
|
|
305
|
+
let { css } = await generateAll();
|
|
280
306
|
const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
|
|
281
|
-
|
|
282
|
-
await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
|
|
283
|
-
let { css } = await uno.generate(tokens2, { minify: true });
|
|
284
|
-
if (!css)
|
|
285
|
-
return null;
|
|
286
|
-
if (!Object.keys(chunk.modules).some((i) => i.match(RESOLVED_ID_RE)))
|
|
287
|
-
return null;
|
|
288
|
-
css = await transformCSS(css, fakeCssId, options.dir);
|
|
307
|
+
css = await applyCssTransform(css, fakeCssId, options.dir);
|
|
289
308
|
const hash = getHash(css);
|
|
290
|
-
await
|
|
309
|
+
await cssPost.transform.call({}, getHashPlaceholder(hash), fakeCssId);
|
|
291
310
|
chunk.modules[fakeCssId] = {
|
|
292
311
|
code: null,
|
|
293
312
|
originalLength: 0,
|
|
@@ -309,13 +328,12 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
309
328
|
const cssFiles = files.filter((i) => i.endsWith(".css"));
|
|
310
329
|
if (!cssFiles.length)
|
|
311
330
|
return;
|
|
312
|
-
if (!
|
|
331
|
+
if (!vfsLayers.size) {
|
|
313
332
|
const msg = "[unocss] entry module not found, have you add `import 'uno.css'` in your main entry?";
|
|
314
333
|
this.warn(msg);
|
|
315
334
|
return;
|
|
316
335
|
}
|
|
317
|
-
await
|
|
318
|
-
const result = await uno.generate(tokens, { minify: true });
|
|
336
|
+
const result = await generateAll();
|
|
319
337
|
let replaced = false;
|
|
320
338
|
for (const file of cssFiles) {
|
|
321
339
|
const chunk = bundle[file];
|
|
@@ -323,7 +341,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
323
341
|
const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
|
|
324
342
|
chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
|
|
325
343
|
replaced = true;
|
|
326
|
-
return await
|
|
344
|
+
return await applyCssTransform(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayers)) : result.getLayer(layer) || "", `${chunk.fileName}.css`, options.dir);
|
|
327
345
|
});
|
|
328
346
|
}
|
|
329
347
|
}
|
|
@@ -340,7 +358,7 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
|
340
358
|
const servers = [];
|
|
341
359
|
let base = "";
|
|
342
360
|
const tasks = [];
|
|
343
|
-
const entries = /* @__PURE__ */ new
|
|
361
|
+
const entries = /* @__PURE__ */ new Set();
|
|
344
362
|
let invalidateTimer;
|
|
345
363
|
let lastUpdate = Date.now();
|
|
346
364
|
let lastServed = 0;
|
|
@@ -355,7 +373,7 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
|
355
373
|
}
|
|
356
374
|
function invalidate(timer = 10) {
|
|
357
375
|
for (const server of servers) {
|
|
358
|
-
for (const id of entries
|
|
376
|
+
for (const id of entries) {
|
|
359
377
|
const mod = server.moduleGraph.getModuleById(id);
|
|
360
378
|
if (!mod)
|
|
361
379
|
continue;
|
|
@@ -370,7 +388,7 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
|
370
388
|
for (const server of servers) {
|
|
371
389
|
server.ws.send({
|
|
372
390
|
type: "update",
|
|
373
|
-
updates: Array.from(entries
|
|
391
|
+
updates: Array.from(entries).map((i) => ({
|
|
374
392
|
acceptedPath: i,
|
|
375
393
|
path: i,
|
|
376
394
|
timestamp: lastUpdate,
|
|
@@ -428,18 +446,18 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
|
428
446
|
const entry = resolveId(id);
|
|
429
447
|
if (entry) {
|
|
430
448
|
resolved = true;
|
|
431
|
-
entries.
|
|
432
|
-
return entry
|
|
449
|
+
entries.add(entry);
|
|
450
|
+
return entry;
|
|
433
451
|
}
|
|
434
452
|
},
|
|
435
453
|
async load(id) {
|
|
436
|
-
const layer =
|
|
454
|
+
const layer = resolveLayer(getPath(id));
|
|
437
455
|
if (!layer)
|
|
438
456
|
return null;
|
|
439
457
|
await Promise.all(tasks);
|
|
440
458
|
const result = await uno.generate(tokens);
|
|
441
459
|
lastServed = Date.now();
|
|
442
|
-
return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries.
|
|
460
|
+
return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer);
|
|
443
461
|
}
|
|
444
462
|
},
|
|
445
463
|
{
|
|
@@ -636,7 +654,10 @@ function ShadowDomModuleModePlugin({ uno }) {
|
|
|
636
654
|
const transformWebComponent = async (code) => {
|
|
637
655
|
if (!code.match(CSS_PLACEHOLDER))
|
|
638
656
|
return code;
|
|
639
|
-
let { css, matched } = await uno.generate(code, {
|
|
657
|
+
let { css, matched } = await uno.generate(code, {
|
|
658
|
+
preflights: true,
|
|
659
|
+
safelist: true
|
|
660
|
+
});
|
|
640
661
|
if (css && matched) {
|
|
641
662
|
const useParts = Array.from(matched).reduce((acc, rule) => {
|
|
642
663
|
const matcher = rule.match(partExtractorRegex);
|
package/dist/index.d.ts
CHANGED
|
@@ -41,7 +41,7 @@ declare function ChunkModeBuildPlugin({ uno, filter }: UnocssPluginContext): Plu
|
|
|
41
41
|
|
|
42
42
|
declare function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[];
|
|
43
43
|
|
|
44
|
-
declare function GlobalModeBuildPlugin({ uno, ready, extract, tokens,
|
|
44
|
+
declare function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig }: UnocssPluginContext<VitePluginConfig>): Plugin[];
|
|
45
45
|
|
|
46
46
|
declare function GlobalModePlugin(ctx: UnocssPluginContext): vite.Plugin[];
|
|
47
47
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import UnocssInspector from '@unocss/inspector';
|
|
2
|
+
import { resolve, dirname } from 'path';
|
|
2
3
|
import { createHash } from 'crypto';
|
|
3
4
|
import { cssIdRE, createGenerator, BetterMap, toEscapedSelector } from '@unocss/core';
|
|
4
5
|
import { createFilter } from '@rollup/pluginutils';
|
|
5
6
|
import MagicString from 'magic-string';
|
|
6
7
|
import fs from 'fs';
|
|
7
|
-
import { resolve, dirname } from 'path';
|
|
8
8
|
import { fileURLToPath } from 'url';
|
|
9
9
|
import { loadConfig } from '@unocss/config';
|
|
10
10
|
|
|
@@ -19,17 +19,16 @@ function resolveId(id) {
|
|
|
19
19
|
for (const alias of VIRTUAL_ENTRY_ALIAS) {
|
|
20
20
|
const match = id.match(alias);
|
|
21
21
|
if (match) {
|
|
22
|
-
return match[1] ? {
|
|
23
|
-
id: `/__uno_${match[1]}.css`,
|
|
24
|
-
layer: match[1]
|
|
25
|
-
} : {
|
|
26
|
-
id: "/__uno.css",
|
|
27
|
-
layer: LAYER_MARK_ALL
|
|
28
|
-
};
|
|
22
|
+
return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
|
|
29
23
|
}
|
|
30
24
|
}
|
|
31
25
|
}
|
|
32
|
-
const RESOLVED_ID_RE = /\/__uno(_.*?)?\.css$/;
|
|
26
|
+
const RESOLVED_ID_RE = /\/__uno(?:(_.*?))?\.css$/;
|
|
27
|
+
function resolveLayer(id) {
|
|
28
|
+
const match = id.match(RESOLVED_ID_RE);
|
|
29
|
+
if (match)
|
|
30
|
+
return match[1] || LAYER_MARK_ALL;
|
|
31
|
+
}
|
|
33
32
|
const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
|
|
34
33
|
function getLayerPlaceholder(layer) {
|
|
35
34
|
return `#--unocss--{layer:${layer}}`;
|
|
@@ -50,6 +49,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
50
49
|
const uno = createGenerator(rawConfig, defaults);
|
|
51
50
|
let rollupFilter = createFilter(defaultInclude, defaultExclude);
|
|
52
51
|
const invalidations = [];
|
|
52
|
+
const reloadListeners = [];
|
|
53
53
|
const modules = new BetterMap();
|
|
54
54
|
const tokens = /* @__PURE__ */ new Set();
|
|
55
55
|
let ready = reloadConfig();
|
|
@@ -63,6 +63,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
63
63
|
tokens.clear();
|
|
64
64
|
await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
|
|
65
65
|
invalidate();
|
|
66
|
+
dispatchReload();
|
|
66
67
|
const presets = /* @__PURE__ */ new Set();
|
|
67
68
|
uno.config.presets.forEach((i) => {
|
|
68
69
|
if (!i.name)
|
|
@@ -84,6 +85,9 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
84
85
|
function invalidate() {
|
|
85
86
|
invalidations.forEach((cb) => cb());
|
|
86
87
|
}
|
|
88
|
+
function dispatchReload() {
|
|
89
|
+
reloadListeners.forEach((cb) => cb());
|
|
90
|
+
}
|
|
87
91
|
async function extract(code, id) {
|
|
88
92
|
if (id)
|
|
89
93
|
modules.set(id, code);
|
|
@@ -113,6 +117,9 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
113
117
|
},
|
|
114
118
|
filter,
|
|
115
119
|
reloadConfig,
|
|
120
|
+
onReload(fn) {
|
|
121
|
+
reloadListeners.push(fn);
|
|
122
|
+
},
|
|
116
123
|
uno,
|
|
117
124
|
extract,
|
|
118
125
|
getConfig,
|
|
@@ -192,13 +199,13 @@ function ChunkModeBuildPlugin({ uno, filter }) {
|
|
|
192
199
|
};
|
|
193
200
|
}
|
|
194
201
|
|
|
195
|
-
function GlobalModeBuildPlugin({ uno, ready, extract, tokens,
|
|
196
|
-
const
|
|
202
|
+
function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig }) {
|
|
203
|
+
const vfsLayers = /* @__PURE__ */ new Set();
|
|
197
204
|
const layerImporterMap = /* @__PURE__ */ new Map();
|
|
198
205
|
let tasks = [];
|
|
199
206
|
const cssPostPlugins = /* @__PURE__ */ new Map();
|
|
200
207
|
const cssPlugins = /* @__PURE__ */ new Map();
|
|
201
|
-
async function
|
|
208
|
+
async function applyCssTransform(css, id, dir) {
|
|
202
209
|
const {
|
|
203
210
|
postcss = true
|
|
204
211
|
} = await getConfig();
|
|
@@ -214,6 +221,16 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
214
221
|
css = css.replace(/[\n\r]/g, "");
|
|
215
222
|
return css;
|
|
216
223
|
}
|
|
224
|
+
let lastTokenSize = 0;
|
|
225
|
+
let lastResult;
|
|
226
|
+
async function generateAll() {
|
|
227
|
+
await Promise.all(tasks);
|
|
228
|
+
if (lastResult && lastTokenSize === tokens.size)
|
|
229
|
+
return lastResult;
|
|
230
|
+
lastResult = await uno.generate(tokens, { minify: true });
|
|
231
|
+
lastTokenSize = tokens.size;
|
|
232
|
+
return lastResult;
|
|
233
|
+
}
|
|
217
234
|
return [
|
|
218
235
|
{
|
|
219
236
|
name: "unocss:global:build:scan",
|
|
@@ -221,6 +238,8 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
221
238
|
enforce: "pre",
|
|
222
239
|
buildStart() {
|
|
223
240
|
tasks = [];
|
|
241
|
+
lastTokenSize = 0;
|
|
242
|
+
lastResult = void 0;
|
|
224
243
|
},
|
|
225
244
|
transform(code, id) {
|
|
226
245
|
if (filter(code, id))
|
|
@@ -236,14 +255,17 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
236
255
|
resolveId(id, importer) {
|
|
237
256
|
const entry = resolveId(id);
|
|
238
257
|
if (entry) {
|
|
239
|
-
|
|
240
|
-
if (
|
|
241
|
-
|
|
242
|
-
|
|
258
|
+
const layer = resolveLayer(entry);
|
|
259
|
+
if (layer) {
|
|
260
|
+
vfsLayers.add(layer);
|
|
261
|
+
if (importer)
|
|
262
|
+
layerImporterMap.set(importer, entry);
|
|
263
|
+
}
|
|
264
|
+
return entry;
|
|
243
265
|
}
|
|
244
266
|
},
|
|
245
267
|
load(id) {
|
|
246
|
-
const layer =
|
|
268
|
+
const layer = resolveLayer(getPath(id));
|
|
247
269
|
if (layer)
|
|
248
270
|
return getLayerPlaceholder(layer);
|
|
249
271
|
},
|
|
@@ -253,31 +275,28 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
253
275
|
const layerKey = layerImporterMap.get(id);
|
|
254
276
|
if (!importedIds.includes(layerKey)) {
|
|
255
277
|
layerImporterMap.delete(id);
|
|
256
|
-
|
|
278
|
+
vfsLayers.delete(resolveLayer(layerKey));
|
|
257
279
|
}
|
|
258
280
|
},
|
|
259
281
|
async configResolved(config) {
|
|
260
|
-
|
|
261
|
-
|
|
282
|
+
const distDir = resolve(config.root, config.build.outDir);
|
|
283
|
+
cssPostPlugins.set(distDir, config.plugins.find((i) => i.name === "vite:css-post"));
|
|
284
|
+
cssPlugins.set(distDir, config.plugins.find((i) => i.name === "vite:css"));
|
|
262
285
|
await ready;
|
|
263
286
|
},
|
|
264
287
|
async renderChunk(_, chunk, options) {
|
|
265
|
-
if (!
|
|
288
|
+
if (!Object.keys(chunk.modules).some((i) => i.match(RESOLVED_ID_RE)))
|
|
266
289
|
return null;
|
|
267
|
-
const
|
|
268
|
-
if (!
|
|
290
|
+
const cssPost = cssPostPlugins.get(options.dir);
|
|
291
|
+
if (!cssPost) {
|
|
292
|
+
this.warn("[unocss] failed to find vite:css-post plugin. It might be an internal bug of UnoCSS");
|
|
269
293
|
return null;
|
|
294
|
+
}
|
|
295
|
+
let { css } = await generateAll();
|
|
270
296
|
const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
|
|
271
|
-
|
|
272
|
-
await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
|
|
273
|
-
let { css } = await uno.generate(tokens2, { minify: true });
|
|
274
|
-
if (!css)
|
|
275
|
-
return null;
|
|
276
|
-
if (!Object.keys(chunk.modules).some((i) => i.match(RESOLVED_ID_RE)))
|
|
277
|
-
return null;
|
|
278
|
-
css = await transformCSS(css, fakeCssId, options.dir);
|
|
297
|
+
css = await applyCssTransform(css, fakeCssId, options.dir);
|
|
279
298
|
const hash = getHash(css);
|
|
280
|
-
await
|
|
299
|
+
await cssPost.transform.call({}, getHashPlaceholder(hash), fakeCssId);
|
|
281
300
|
chunk.modules[fakeCssId] = {
|
|
282
301
|
code: null,
|
|
283
302
|
originalLength: 0,
|
|
@@ -299,13 +318,12 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
299
318
|
const cssFiles = files.filter((i) => i.endsWith(".css"));
|
|
300
319
|
if (!cssFiles.length)
|
|
301
320
|
return;
|
|
302
|
-
if (!
|
|
321
|
+
if (!vfsLayers.size) {
|
|
303
322
|
const msg = "[unocss] entry module not found, have you add `import 'uno.css'` in your main entry?";
|
|
304
323
|
this.warn(msg);
|
|
305
324
|
return;
|
|
306
325
|
}
|
|
307
|
-
await
|
|
308
|
-
const result = await uno.generate(tokens, { minify: true });
|
|
326
|
+
const result = await generateAll();
|
|
309
327
|
let replaced = false;
|
|
310
328
|
for (const file of cssFiles) {
|
|
311
329
|
const chunk = bundle[file];
|
|
@@ -313,7 +331,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
|
|
|
313
331
|
const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
|
|
314
332
|
chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
|
|
315
333
|
replaced = true;
|
|
316
|
-
return await
|
|
334
|
+
return await applyCssTransform(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayers)) : result.getLayer(layer) || "", `${chunk.fileName}.css`, options.dir);
|
|
317
335
|
});
|
|
318
336
|
}
|
|
319
337
|
}
|
|
@@ -330,7 +348,7 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
|
330
348
|
const servers = [];
|
|
331
349
|
let base = "";
|
|
332
350
|
const tasks = [];
|
|
333
|
-
const entries = /* @__PURE__ */ new
|
|
351
|
+
const entries = /* @__PURE__ */ new Set();
|
|
334
352
|
let invalidateTimer;
|
|
335
353
|
let lastUpdate = Date.now();
|
|
336
354
|
let lastServed = 0;
|
|
@@ -345,7 +363,7 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
|
345
363
|
}
|
|
346
364
|
function invalidate(timer = 10) {
|
|
347
365
|
for (const server of servers) {
|
|
348
|
-
for (const id of entries
|
|
366
|
+
for (const id of entries) {
|
|
349
367
|
const mod = server.moduleGraph.getModuleById(id);
|
|
350
368
|
if (!mod)
|
|
351
369
|
continue;
|
|
@@ -360,7 +378,7 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
|
360
378
|
for (const server of servers) {
|
|
361
379
|
server.ws.send({
|
|
362
380
|
type: "update",
|
|
363
|
-
updates: Array.from(entries
|
|
381
|
+
updates: Array.from(entries).map((i) => ({
|
|
364
382
|
acceptedPath: i,
|
|
365
383
|
path: i,
|
|
366
384
|
timestamp: lastUpdate,
|
|
@@ -418,18 +436,18 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
|
418
436
|
const entry = resolveId(id);
|
|
419
437
|
if (entry) {
|
|
420
438
|
resolved = true;
|
|
421
|
-
entries.
|
|
422
|
-
return entry
|
|
439
|
+
entries.add(entry);
|
|
440
|
+
return entry;
|
|
423
441
|
}
|
|
424
442
|
},
|
|
425
443
|
async load(id) {
|
|
426
|
-
const layer =
|
|
444
|
+
const layer = resolveLayer(getPath(id));
|
|
427
445
|
if (!layer)
|
|
428
446
|
return null;
|
|
429
447
|
await Promise.all(tasks);
|
|
430
448
|
const result = await uno.generate(tokens);
|
|
431
449
|
lastServed = Date.now();
|
|
432
|
-
return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries.
|
|
450
|
+
return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer);
|
|
433
451
|
}
|
|
434
452
|
},
|
|
435
453
|
{
|
|
@@ -626,7 +644,10 @@ function ShadowDomModuleModePlugin({ uno }) {
|
|
|
626
644
|
const transformWebComponent = async (code) => {
|
|
627
645
|
if (!code.match(CSS_PLACEHOLDER))
|
|
628
646
|
return code;
|
|
629
|
-
let { css, matched } = await uno.generate(code, {
|
|
647
|
+
let { css, matched } = await uno.generate(code, {
|
|
648
|
+
preflights: true,
|
|
649
|
+
safelist: true
|
|
650
|
+
});
|
|
630
651
|
if (css && matched) {
|
|
631
652
|
const useParts = Array.from(matched).reduce((acc, rule) => {
|
|
632
653
|
const matcher = rule.match(partExtractorRegex);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/vite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.1",
|
|
4
4
|
"description": "The Vite plugin for UnoCSS",
|
|
5
5
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,15 +43,15 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@rollup/pluginutils": "^4.2.1",
|
|
46
|
-
"@unocss/config": "0.
|
|
47
|
-
"@unocss/core": "0.
|
|
48
|
-
"@unocss/inspector": "0.
|
|
49
|
-
"@unocss/scope": "0.
|
|
50
|
-
"@unocss/transformer-directives": "0.
|
|
46
|
+
"@unocss/config": "0.39.1",
|
|
47
|
+
"@unocss/core": "0.39.1",
|
|
48
|
+
"@unocss/inspector": "0.39.1",
|
|
49
|
+
"@unocss/scope": "0.39.1",
|
|
50
|
+
"@unocss/transformer-directives": "0.39.1",
|
|
51
51
|
"magic-string": "^0.26.2"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@unocss/shared-integration": "0.
|
|
54
|
+
"@unocss/shared-integration": "0.39.1",
|
|
55
55
|
"vite": "^2.9.9"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|