@unocss/vite 0.47.5 → 0.48.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/README.md +1 -1
- package/dist/index.cjs +121 -56
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +118 -55
- package/package.json +12 -10
package/README.md
CHANGED
|
@@ -75,7 +75,7 @@ This mode will inject generated CSS to Svelte's `<style>` for isolation.
|
|
|
75
75
|
|
|
76
76
|
Since `Web Components` uses `Shadow DOM`, there is no way to style content directly from a global stylesheet (unless you use `custom css vars`, those will penetrate the `Shadow DOM`), you need to inline the generated css by the plugin into the `Shadow DOM` style.
|
|
77
77
|
|
|
78
|
-
To inline the generated css, you only need to configure the plugin mode to `shadow-dom` and include `@unocss-placeholder` magic placeholder on each web component style css block.
|
|
78
|
+
To inline the generated css, you only need to configure the plugin mode to `shadow-dom` and include `@unocss-placeholder` magic placeholder on each web component style css block. If you are defining your Web Components in Vue SFCs and want to define custom styles alongside UnoCSS, you can wrap placeholder in a CSS comment to avoid syntax errors in your IDE.
|
|
79
79
|
|
|
80
80
|
###### `per-module` (Experimental)
|
|
81
81
|
|
package/dist/index.cjs
CHANGED
|
@@ -9,16 +9,20 @@ const config = require('@unocss/config');
|
|
|
9
9
|
const crypto = require('crypto');
|
|
10
10
|
const MagicString = require('magic-string');
|
|
11
11
|
const path = require('path');
|
|
12
|
+
const fs = require('fs/promises');
|
|
13
|
+
const fg = require('fast-glob');
|
|
12
14
|
const remapping = require('@ampproject/remapping');
|
|
13
|
-
const fs = require('fs');
|
|
15
|
+
const fs$1 = require('fs');
|
|
14
16
|
const url = require('url');
|
|
15
17
|
|
|
16
18
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
17
19
|
|
|
18
20
|
const UnocssInspector__default = /*#__PURE__*/_interopDefaultLegacy(UnocssInspector);
|
|
19
21
|
const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
20
|
-
const remapping__default = /*#__PURE__*/_interopDefaultLegacy(remapping);
|
|
21
22
|
const fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
23
|
+
const fg__default = /*#__PURE__*/_interopDefaultLegacy(fg);
|
|
24
|
+
const remapping__default = /*#__PURE__*/_interopDefaultLegacy(remapping);
|
|
25
|
+
const fs__default$1 = /*#__PURE__*/_interopDefaultLegacy(fs$1);
|
|
22
26
|
|
|
23
27
|
const defaultExclude = [core.cssIdRE];
|
|
24
28
|
const defaultInclude = [/\.(vue|svelte|[jt]sx|mdx?|astro|elm|php|phtml|html)($|\?)/];
|
|
@@ -68,6 +72,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
68
72
|
const reloadListeners = [];
|
|
69
73
|
const modules = new core.BetterMap();
|
|
70
74
|
const tokens = /* @__PURE__ */ new Set();
|
|
75
|
+
const tasks = [];
|
|
71
76
|
const affectedModules = /* @__PURE__ */ new Set();
|
|
72
77
|
let ready = reloadConfig();
|
|
73
78
|
async function reloadConfig() {
|
|
@@ -117,15 +122,20 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
117
122
|
if (tokens.size > len)
|
|
118
123
|
invalidate();
|
|
119
124
|
}
|
|
120
|
-
|
|
125
|
+
function filter(code, id) {
|
|
121
126
|
if (code.includes(IGNORE_COMMENT))
|
|
122
127
|
return false;
|
|
123
128
|
return code.includes(INCLUDE_COMMENT) || code.includes(CSS_PLACEHOLDER) || rollupFilter(id.replace(/\?v=\w+$/, ""));
|
|
124
|
-
}
|
|
129
|
+
}
|
|
125
130
|
async function getConfig() {
|
|
126
131
|
await ready;
|
|
127
132
|
return rawConfig;
|
|
128
133
|
}
|
|
134
|
+
async function flushTasks() {
|
|
135
|
+
const _tasks = [...tasks];
|
|
136
|
+
await Promise.all(_tasks);
|
|
137
|
+
tasks.splice(0, _tasks.length);
|
|
138
|
+
}
|
|
129
139
|
return {
|
|
130
140
|
get ready() {
|
|
131
141
|
return ready;
|
|
@@ -133,6 +143,8 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
133
143
|
tokens,
|
|
134
144
|
modules,
|
|
135
145
|
affectedModules,
|
|
146
|
+
tasks,
|
|
147
|
+
flushTasks,
|
|
136
148
|
invalidate,
|
|
137
149
|
onInvalidate(fn) {
|
|
138
150
|
invalidations.push(fn);
|
|
@@ -224,20 +236,93 @@ function ChunkModeBuildPlugin({ uno, filter }) {
|
|
|
224
236
|
};
|
|
225
237
|
}
|
|
226
238
|
|
|
227
|
-
function
|
|
239
|
+
async function applyTransformers(ctx, original, id, enforce = "default") {
|
|
240
|
+
if (original.includes(IGNORE_COMMENT))
|
|
241
|
+
return;
|
|
242
|
+
const transformers = (ctx.uno.config.transformers || []).filter((i) => (i.enforce || "default") === enforce);
|
|
243
|
+
if (!transformers.length)
|
|
244
|
+
return;
|
|
245
|
+
let code = original;
|
|
246
|
+
let s = new MagicString__default(code);
|
|
247
|
+
const maps = [];
|
|
248
|
+
for (const t of transformers) {
|
|
249
|
+
if (t.idFilter) {
|
|
250
|
+
if (!t.idFilter(id))
|
|
251
|
+
continue;
|
|
252
|
+
} else if (!ctx.filter(code, id)) {
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
await t.transform(s, id, ctx);
|
|
256
|
+
if (s.hasChanged()) {
|
|
257
|
+
code = s.toString();
|
|
258
|
+
maps.push(s.generateMap({ hires: true, source: id }));
|
|
259
|
+
s = new MagicString__default(code);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (code !== original) {
|
|
263
|
+
ctx.affectedModules.add(id);
|
|
264
|
+
return {
|
|
265
|
+
code,
|
|
266
|
+
map: remapping__default(maps, () => null)
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async function setupExtraContent(ctx, shouldWatch = false) {
|
|
272
|
+
const { extraContent } = await ctx.getConfig();
|
|
273
|
+
const { extract, tasks, root, filter } = ctx;
|
|
274
|
+
if (extraContent?.plain) {
|
|
275
|
+
await Promise.all(
|
|
276
|
+
extraContent.plain.map((code, idx) => {
|
|
277
|
+
return extract(code, `__extra_content_${idx}__`);
|
|
278
|
+
})
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
if (extraContent?.filesystem) {
|
|
282
|
+
const files = await fg__default(extraContent.filesystem, { cwd: root });
|
|
283
|
+
async function extractFile(file) {
|
|
284
|
+
const code = await fs__default.readFile(file, "utf-8");
|
|
285
|
+
if (!filter(code, file))
|
|
286
|
+
return;
|
|
287
|
+
const preTransform = await applyTransformers(ctx, code, file, "pre");
|
|
288
|
+
const defaultTransform = await applyTransformers(ctx, preTransform?.code || code, file);
|
|
289
|
+
await applyTransformers(ctx, defaultTransform?.code || preTransform?.code || code, file, "post");
|
|
290
|
+
return await extract(preTransform?.code || code, file);
|
|
291
|
+
}
|
|
292
|
+
if (shouldWatch) {
|
|
293
|
+
const { watch } = await import('chokidar');
|
|
294
|
+
const ignored = ["**/{.git,node_modules}/**"];
|
|
295
|
+
const watcher = watch(files, {
|
|
296
|
+
ignorePermissionErrors: true,
|
|
297
|
+
ignored,
|
|
298
|
+
cwd: root,
|
|
299
|
+
ignoreInitial: true
|
|
300
|
+
});
|
|
301
|
+
watcher.on("all", (type, file) => {
|
|
302
|
+
if (type === "add" || type === "change") {
|
|
303
|
+
const absolutePath = path.resolve(root, file);
|
|
304
|
+
tasks.push(extractFile(absolutePath));
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
await Promise.all(files.map(extractFile));
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function GlobalModeBuildPlugin(ctx) {
|
|
313
|
+
const { uno, ready, extract, tokens, filter, getConfig, tasks, flushTasks } = ctx;
|
|
228
314
|
const vfsLayers = /* @__PURE__ */ new Set();
|
|
229
315
|
const layerImporterMap = /* @__PURE__ */ new Map();
|
|
230
|
-
let tasks = [];
|
|
231
316
|
let viteConfig;
|
|
232
317
|
const cssPostPlugins = /* @__PURE__ */ new Map();
|
|
233
318
|
const cssPlugins = /* @__PURE__ */ new Map();
|
|
234
|
-
async function applyCssTransform(css, id, dir,
|
|
319
|
+
async function applyCssTransform(css, id, dir, ctx2) {
|
|
235
320
|
const {
|
|
236
321
|
postcss = true
|
|
237
322
|
} = await getConfig();
|
|
238
323
|
if (!cssPlugins.get(dir) || !postcss)
|
|
239
324
|
return css;
|
|
240
|
-
const result = await cssPlugins.get(dir).transform.call(
|
|
325
|
+
const result = await cssPlugins.get(dir).transform.call(ctx2, css, id);
|
|
241
326
|
if (!result)
|
|
242
327
|
return css;
|
|
243
328
|
if (typeof result === "string")
|
|
@@ -250,20 +335,22 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
250
335
|
let lastTokenSize = 0;
|
|
251
336
|
let lastResult;
|
|
252
337
|
async function generateAll() {
|
|
253
|
-
await
|
|
338
|
+
await flushTasks();
|
|
254
339
|
if (lastResult && lastTokenSize === tokens.size)
|
|
255
340
|
return lastResult;
|
|
256
341
|
lastResult = await uno.generate(tokens, { minify: true });
|
|
257
342
|
lastTokenSize = tokens.size;
|
|
258
343
|
return lastResult;
|
|
259
344
|
}
|
|
345
|
+
let replaced = false;
|
|
260
346
|
return [
|
|
261
347
|
{
|
|
262
348
|
name: "unocss:global:build:scan",
|
|
263
349
|
apply: "build",
|
|
264
350
|
enforce: "pre",
|
|
265
|
-
buildStart() {
|
|
266
|
-
|
|
351
|
+
async buildStart() {
|
|
352
|
+
vfsLayers.clear();
|
|
353
|
+
tasks.length = 0;
|
|
267
354
|
lastTokenSize = 0;
|
|
268
355
|
lastResult = void 0;
|
|
269
356
|
},
|
|
@@ -334,6 +421,16 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
334
421
|
return null;
|
|
335
422
|
}
|
|
336
423
|
},
|
|
424
|
+
{
|
|
425
|
+
name: "unocss:global:content",
|
|
426
|
+
enforce: "pre",
|
|
427
|
+
configResolved(config) {
|
|
428
|
+
viteConfig = config;
|
|
429
|
+
},
|
|
430
|
+
buildStart() {
|
|
431
|
+
tasks.push(setupExtraContent(ctx, viteConfig.command === "serve"));
|
|
432
|
+
}
|
|
433
|
+
},
|
|
337
434
|
{
|
|
338
435
|
name: "unocss:global:build:generate",
|
|
339
436
|
apply: "build",
|
|
@@ -358,9 +455,6 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
358
455
|
{
|
|
359
456
|
name: "unocss:global:build:bundle",
|
|
360
457
|
apply: "build",
|
|
361
|
-
configResolved(config) {
|
|
362
|
-
viteConfig = config;
|
|
363
|
-
},
|
|
364
458
|
enforce: "post",
|
|
365
459
|
async generateBundle(options, bundle) {
|
|
366
460
|
const checkJs = ["umd", "amd", "iife"].includes(options.format);
|
|
@@ -368,11 +462,12 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
368
462
|
if (!files.length)
|
|
369
463
|
return;
|
|
370
464
|
if (!vfsLayers.size) {
|
|
465
|
+
if (replaced)
|
|
466
|
+
return;
|
|
371
467
|
const msg = "[unocss] entry module not found, have you add `import 'uno.css'` in your main entry?";
|
|
372
468
|
this.warn(msg);
|
|
373
469
|
return;
|
|
374
470
|
}
|
|
375
|
-
let replaced = false;
|
|
376
471
|
const getLayer = (layer, input, replace = false) => {
|
|
377
472
|
const re = new RegExp(`#--unocss-layer-start--${layer}--\\{start:${layer}\\}([\\s\\S]*?)#--unocss-layer-end--${layer}--\\{end:${layer}\\}`, "g");
|
|
378
473
|
if (replace)
|
|
@@ -411,7 +506,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
411
506
|
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";
|
|
412
507
|
else
|
|
413
508
|
msg += "\nThis is likely an internal bug of unocss vite plugin";
|
|
414
|
-
this.error(
|
|
509
|
+
this.error(msg);
|
|
415
510
|
}
|
|
416
511
|
}
|
|
417
512
|
}
|
|
@@ -421,10 +516,9 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
421
516
|
const WARN_TIMEOUT = 2e4;
|
|
422
517
|
const WS_EVENT_PREFIX = "unocss:hmr";
|
|
423
518
|
const HASH_LENGTH = 6;
|
|
424
|
-
function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extract, filter }) {
|
|
519
|
+
function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules, onInvalidate, extract, filter }) {
|
|
425
520
|
const servers = [];
|
|
426
521
|
let base = "";
|
|
427
|
-
const tasks = [];
|
|
428
522
|
const entries = /* @__PURE__ */ new Set();
|
|
429
523
|
let invalidateTimer;
|
|
430
524
|
const lastServedHash = /* @__PURE__ */ new Map();
|
|
@@ -432,7 +526,7 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
|
|
|
432
526
|
let resolved = false;
|
|
433
527
|
let resolvedWarnTimer;
|
|
434
528
|
async function generateCSS(layer) {
|
|
435
|
-
await
|
|
529
|
+
await flushTasks();
|
|
436
530
|
let result;
|
|
437
531
|
let tokensSize = tokens.size;
|
|
438
532
|
do {
|
|
@@ -889,6 +983,7 @@ function SvelteScopedPlugin({ ready, uno }) {
|
|
|
889
983
|
function ShadowDomModuleModePlugin({ uno }) {
|
|
890
984
|
const partExtractorRegex = /^part-\[(.+)]:/;
|
|
891
985
|
const nameRegexp = /<([^\s^!>]+)\s*([^>]*)>/;
|
|
986
|
+
const vueSFCStyleRE = new RegExp(`<style.*>[\\s\\S]*${CSS_PLACEHOLDER}[\\s\\S]*<\\/style>`);
|
|
892
987
|
const checkElement = (useParts, idxResolver, element) => {
|
|
893
988
|
if (!element)
|
|
894
989
|
return null;
|
|
@@ -921,7 +1016,7 @@ function ShadowDomModuleModePlugin({ uno }) {
|
|
|
921
1016
|
}
|
|
922
1017
|
};
|
|
923
1018
|
};
|
|
924
|
-
const transformWebComponent = async (code) => {
|
|
1019
|
+
const transformWebComponent = async (code, id) => {
|
|
925
1020
|
if (!code.match(CSS_PLACEHOLDER))
|
|
926
1021
|
return code;
|
|
927
1022
|
let { css, matched } = await uno.generate(code, {
|
|
@@ -966,19 +1061,21 @@ function ShadowDomModuleModePlugin({ uno }) {
|
|
|
966
1061
|
}
|
|
967
1062
|
}
|
|
968
1063
|
}
|
|
1064
|
+
if (id.includes("?vue&type=style") || id.endsWith(".vue") && vueSFCStyleRE.test(code))
|
|
1065
|
+
return code.replace(new RegExp(`(\\/\\*\\s*)?${CSS_PLACEHOLDER}(\\s*\\*\\/)?`), css || "");
|
|
969
1066
|
return code.replace(CSS_PLACEHOLDER, css?.replace(/\\/g, "\\\\") ?? "");
|
|
970
1067
|
};
|
|
971
1068
|
return {
|
|
972
1069
|
name: "unocss:shadow-dom",
|
|
973
1070
|
enforce: "pre",
|
|
974
|
-
async transform(code) {
|
|
975
|
-
return transformWebComponent(code);
|
|
1071
|
+
async transform(code, id) {
|
|
1072
|
+
return transformWebComponent(code, id);
|
|
976
1073
|
},
|
|
977
1074
|
handleHotUpdate(ctx) {
|
|
978
1075
|
const read = ctx.read;
|
|
979
1076
|
ctx.read = async () => {
|
|
980
1077
|
const code = await read();
|
|
981
|
-
return await transformWebComponent(code);
|
|
1078
|
+
return await transformWebComponent(code, ctx.file);
|
|
982
1079
|
};
|
|
983
1080
|
}
|
|
984
1081
|
};
|
|
@@ -1010,38 +1107,6 @@ function ConfigHMRPlugin(ctx) {
|
|
|
1010
1107
|
};
|
|
1011
1108
|
}
|
|
1012
1109
|
|
|
1013
|
-
async function applyTransformers(ctx, original, id, enforce = "default") {
|
|
1014
|
-
if (original.includes(IGNORE_COMMENT))
|
|
1015
|
-
return;
|
|
1016
|
-
const transformers = (ctx.uno.config.transformers || []).filter((i) => (i.enforce || "default") === enforce);
|
|
1017
|
-
if (!transformers.length)
|
|
1018
|
-
return;
|
|
1019
|
-
let code = original;
|
|
1020
|
-
let s = new MagicString__default(code);
|
|
1021
|
-
const maps = [];
|
|
1022
|
-
for (const t of transformers) {
|
|
1023
|
-
if (t.idFilter) {
|
|
1024
|
-
if (!t.idFilter(id))
|
|
1025
|
-
continue;
|
|
1026
|
-
} else if (!ctx.filter(code, id)) {
|
|
1027
|
-
continue;
|
|
1028
|
-
}
|
|
1029
|
-
await t.transform(s, id, ctx);
|
|
1030
|
-
if (s.hasChanged()) {
|
|
1031
|
-
code = s.toString();
|
|
1032
|
-
maps.push(s.generateMap({ hires: true, source: id }));
|
|
1033
|
-
s = new MagicString__default(code);
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
if (code !== original) {
|
|
1037
|
-
ctx.affectedModules.add(id);
|
|
1038
|
-
return {
|
|
1039
|
-
code,
|
|
1040
|
-
map: remapping__default(maps, () => null)
|
|
1041
|
-
};
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
|
|
1045
1110
|
function createTransformerPlugins(ctx) {
|
|
1046
1111
|
const enforces = ["default", "pre", "post"];
|
|
1047
1112
|
return enforces.map((enforce) => ({
|
|
@@ -1169,7 +1234,7 @@ function createDevtoolsPlugin(ctx) {
|
|
|
1169
1234
|
if (id === DEVTOOLS_PATH) {
|
|
1170
1235
|
if (!clientCode) {
|
|
1171
1236
|
clientCode = [
|
|
1172
|
-
await fs__default.promises.readFile(path.resolve(_dirname, "client.mjs"), "utf-8"),
|
|
1237
|
+
await fs__default$1.promises.readFile(path.resolve(_dirname, "client.mjs"), "utf-8"),
|
|
1173
1238
|
`import('${MOCK_CLASSES_MODULE_ID}')`,
|
|
1174
1239
|
`import('${DEVTOOLS_CSS_PATH}')`
|
|
1175
1240
|
].join("\n").replace("__POST_PATH__", (config.server?.origin ?? "") + POST_PATH);
|
package/dist/index.d.ts
CHANGED
|
@@ -39,9 +39,9 @@ interface VitePluginConfig<Theme extends {} = {}> extends UserConfig<Theme> {
|
|
|
39
39
|
|
|
40
40
|
declare function ChunkModeBuildPlugin({ uno, filter }: UnocssPluginContext): Plugin;
|
|
41
41
|
|
|
42
|
-
declare function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[];
|
|
42
|
+
declare function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[];
|
|
43
43
|
|
|
44
|
-
declare function GlobalModeBuildPlugin(
|
|
44
|
+
declare function GlobalModeBuildPlugin(ctx: UnocssPluginContext<VitePluginConfig>): Plugin[];
|
|
45
45
|
|
|
46
46
|
declare function GlobalModePlugin(ctx: UnocssPluginContext): vite.Plugin[];
|
|
47
47
|
|
package/dist/index.mjs
CHANGED
|
@@ -5,8 +5,10 @@ import { loadConfig } from '@unocss/config';
|
|
|
5
5
|
import { createHash } from 'crypto';
|
|
6
6
|
import MagicString from 'magic-string';
|
|
7
7
|
import { resolve, dirname } from 'path';
|
|
8
|
+
import fs from 'fs/promises';
|
|
9
|
+
import fg from 'fast-glob';
|
|
8
10
|
import remapping from '@ampproject/remapping';
|
|
9
|
-
import fs from 'fs';
|
|
11
|
+
import fs$1 from 'fs';
|
|
10
12
|
import { fileURLToPath } from 'url';
|
|
11
13
|
|
|
12
14
|
const defaultExclude = [cssIdRE];
|
|
@@ -57,6 +59,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
57
59
|
const reloadListeners = [];
|
|
58
60
|
const modules = new BetterMap();
|
|
59
61
|
const tokens = /* @__PURE__ */ new Set();
|
|
62
|
+
const tasks = [];
|
|
60
63
|
const affectedModules = /* @__PURE__ */ new Set();
|
|
61
64
|
let ready = reloadConfig();
|
|
62
65
|
async function reloadConfig() {
|
|
@@ -106,15 +109,20 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
106
109
|
if (tokens.size > len)
|
|
107
110
|
invalidate();
|
|
108
111
|
}
|
|
109
|
-
|
|
112
|
+
function filter(code, id) {
|
|
110
113
|
if (code.includes(IGNORE_COMMENT))
|
|
111
114
|
return false;
|
|
112
115
|
return code.includes(INCLUDE_COMMENT) || code.includes(CSS_PLACEHOLDER) || rollupFilter(id.replace(/\?v=\w+$/, ""));
|
|
113
|
-
}
|
|
116
|
+
}
|
|
114
117
|
async function getConfig() {
|
|
115
118
|
await ready;
|
|
116
119
|
return rawConfig;
|
|
117
120
|
}
|
|
121
|
+
async function flushTasks() {
|
|
122
|
+
const _tasks = [...tasks];
|
|
123
|
+
await Promise.all(_tasks);
|
|
124
|
+
tasks.splice(0, _tasks.length);
|
|
125
|
+
}
|
|
118
126
|
return {
|
|
119
127
|
get ready() {
|
|
120
128
|
return ready;
|
|
@@ -122,6 +130,8 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
|
|
|
122
130
|
tokens,
|
|
123
131
|
modules,
|
|
124
132
|
affectedModules,
|
|
133
|
+
tasks,
|
|
134
|
+
flushTasks,
|
|
125
135
|
invalidate,
|
|
126
136
|
onInvalidate(fn) {
|
|
127
137
|
invalidations.push(fn);
|
|
@@ -213,20 +223,93 @@ function ChunkModeBuildPlugin({ uno, filter }) {
|
|
|
213
223
|
};
|
|
214
224
|
}
|
|
215
225
|
|
|
216
|
-
function
|
|
226
|
+
async function applyTransformers(ctx, original, id, enforce = "default") {
|
|
227
|
+
if (original.includes(IGNORE_COMMENT))
|
|
228
|
+
return;
|
|
229
|
+
const transformers = (ctx.uno.config.transformers || []).filter((i) => (i.enforce || "default") === enforce);
|
|
230
|
+
if (!transformers.length)
|
|
231
|
+
return;
|
|
232
|
+
let code = original;
|
|
233
|
+
let s = new MagicString(code);
|
|
234
|
+
const maps = [];
|
|
235
|
+
for (const t of transformers) {
|
|
236
|
+
if (t.idFilter) {
|
|
237
|
+
if (!t.idFilter(id))
|
|
238
|
+
continue;
|
|
239
|
+
} else if (!ctx.filter(code, id)) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
await t.transform(s, id, ctx);
|
|
243
|
+
if (s.hasChanged()) {
|
|
244
|
+
code = s.toString();
|
|
245
|
+
maps.push(s.generateMap({ hires: true, source: id }));
|
|
246
|
+
s = new MagicString(code);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (code !== original) {
|
|
250
|
+
ctx.affectedModules.add(id);
|
|
251
|
+
return {
|
|
252
|
+
code,
|
|
253
|
+
map: remapping(maps, () => null)
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async function setupExtraContent(ctx, shouldWatch = false) {
|
|
259
|
+
const { extraContent } = await ctx.getConfig();
|
|
260
|
+
const { extract, tasks, root, filter } = ctx;
|
|
261
|
+
if (extraContent?.plain) {
|
|
262
|
+
await Promise.all(
|
|
263
|
+
extraContent.plain.map((code, idx) => {
|
|
264
|
+
return extract(code, `__extra_content_${idx}__`);
|
|
265
|
+
})
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
if (extraContent?.filesystem) {
|
|
269
|
+
const files = await fg(extraContent.filesystem, { cwd: root });
|
|
270
|
+
async function extractFile(file) {
|
|
271
|
+
const code = await fs.readFile(file, "utf-8");
|
|
272
|
+
if (!filter(code, file))
|
|
273
|
+
return;
|
|
274
|
+
const preTransform = await applyTransformers(ctx, code, file, "pre");
|
|
275
|
+
const defaultTransform = await applyTransformers(ctx, preTransform?.code || code, file);
|
|
276
|
+
await applyTransformers(ctx, defaultTransform?.code || preTransform?.code || code, file, "post");
|
|
277
|
+
return await extract(preTransform?.code || code, file);
|
|
278
|
+
}
|
|
279
|
+
if (shouldWatch) {
|
|
280
|
+
const { watch } = await import('chokidar');
|
|
281
|
+
const ignored = ["**/{.git,node_modules}/**"];
|
|
282
|
+
const watcher = watch(files, {
|
|
283
|
+
ignorePermissionErrors: true,
|
|
284
|
+
ignored,
|
|
285
|
+
cwd: root,
|
|
286
|
+
ignoreInitial: true
|
|
287
|
+
});
|
|
288
|
+
watcher.on("all", (type, file) => {
|
|
289
|
+
if (type === "add" || type === "change") {
|
|
290
|
+
const absolutePath = resolve(root, file);
|
|
291
|
+
tasks.push(extractFile(absolutePath));
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
await Promise.all(files.map(extractFile));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function GlobalModeBuildPlugin(ctx) {
|
|
300
|
+
const { uno, ready, extract, tokens, filter, getConfig, tasks, flushTasks } = ctx;
|
|
217
301
|
const vfsLayers = /* @__PURE__ */ new Set();
|
|
218
302
|
const layerImporterMap = /* @__PURE__ */ new Map();
|
|
219
|
-
let tasks = [];
|
|
220
303
|
let viteConfig;
|
|
221
304
|
const cssPostPlugins = /* @__PURE__ */ new Map();
|
|
222
305
|
const cssPlugins = /* @__PURE__ */ new Map();
|
|
223
|
-
async function applyCssTransform(css, id, dir,
|
|
306
|
+
async function applyCssTransform(css, id, dir, ctx2) {
|
|
224
307
|
const {
|
|
225
308
|
postcss = true
|
|
226
309
|
} = await getConfig();
|
|
227
310
|
if (!cssPlugins.get(dir) || !postcss)
|
|
228
311
|
return css;
|
|
229
|
-
const result = await cssPlugins.get(dir).transform.call(
|
|
312
|
+
const result = await cssPlugins.get(dir).transform.call(ctx2, css, id);
|
|
230
313
|
if (!result)
|
|
231
314
|
return css;
|
|
232
315
|
if (typeof result === "string")
|
|
@@ -239,20 +322,22 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
239
322
|
let lastTokenSize = 0;
|
|
240
323
|
let lastResult;
|
|
241
324
|
async function generateAll() {
|
|
242
|
-
await
|
|
325
|
+
await flushTasks();
|
|
243
326
|
if (lastResult && lastTokenSize === tokens.size)
|
|
244
327
|
return lastResult;
|
|
245
328
|
lastResult = await uno.generate(tokens, { minify: true });
|
|
246
329
|
lastTokenSize = tokens.size;
|
|
247
330
|
return lastResult;
|
|
248
331
|
}
|
|
332
|
+
let replaced = false;
|
|
249
333
|
return [
|
|
250
334
|
{
|
|
251
335
|
name: "unocss:global:build:scan",
|
|
252
336
|
apply: "build",
|
|
253
337
|
enforce: "pre",
|
|
254
|
-
buildStart() {
|
|
255
|
-
|
|
338
|
+
async buildStart() {
|
|
339
|
+
vfsLayers.clear();
|
|
340
|
+
tasks.length = 0;
|
|
256
341
|
lastTokenSize = 0;
|
|
257
342
|
lastResult = void 0;
|
|
258
343
|
},
|
|
@@ -323,6 +408,16 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
323
408
|
return null;
|
|
324
409
|
}
|
|
325
410
|
},
|
|
411
|
+
{
|
|
412
|
+
name: "unocss:global:content",
|
|
413
|
+
enforce: "pre",
|
|
414
|
+
configResolved(config) {
|
|
415
|
+
viteConfig = config;
|
|
416
|
+
},
|
|
417
|
+
buildStart() {
|
|
418
|
+
tasks.push(setupExtraContent(ctx, viteConfig.command === "serve"));
|
|
419
|
+
}
|
|
420
|
+
},
|
|
326
421
|
{
|
|
327
422
|
name: "unocss:global:build:generate",
|
|
328
423
|
apply: "build",
|
|
@@ -347,9 +442,6 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
347
442
|
{
|
|
348
443
|
name: "unocss:global:build:bundle",
|
|
349
444
|
apply: "build",
|
|
350
|
-
configResolved(config) {
|
|
351
|
-
viteConfig = config;
|
|
352
|
-
},
|
|
353
445
|
enforce: "post",
|
|
354
446
|
async generateBundle(options, bundle) {
|
|
355
447
|
const checkJs = ["umd", "amd", "iife"].includes(options.format);
|
|
@@ -357,11 +449,12 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
357
449
|
if (!files.length)
|
|
358
450
|
return;
|
|
359
451
|
if (!vfsLayers.size) {
|
|
452
|
+
if (replaced)
|
|
453
|
+
return;
|
|
360
454
|
const msg = "[unocss] entry module not found, have you add `import 'uno.css'` in your main entry?";
|
|
361
455
|
this.warn(msg);
|
|
362
456
|
return;
|
|
363
457
|
}
|
|
364
|
-
let replaced = false;
|
|
365
458
|
const getLayer = (layer, input, replace = false) => {
|
|
366
459
|
const re = new RegExp(`#--unocss-layer-start--${layer}--\\{start:${layer}\\}([\\s\\S]*?)#--unocss-layer-end--${layer}--\\{end:${layer}\\}`, "g");
|
|
367
460
|
if (replace)
|
|
@@ -400,7 +493,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
400
493
|
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";
|
|
401
494
|
else
|
|
402
495
|
msg += "\nThis is likely an internal bug of unocss vite plugin";
|
|
403
|
-
this.error(
|
|
496
|
+
this.error(msg);
|
|
404
497
|
}
|
|
405
498
|
}
|
|
406
499
|
}
|
|
@@ -410,10 +503,9 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
|
|
|
410
503
|
const WARN_TIMEOUT = 2e4;
|
|
411
504
|
const WS_EVENT_PREFIX = "unocss:hmr";
|
|
412
505
|
const HASH_LENGTH = 6;
|
|
413
|
-
function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extract, filter }) {
|
|
506
|
+
function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedModules, onInvalidate, extract, filter }) {
|
|
414
507
|
const servers = [];
|
|
415
508
|
let base = "";
|
|
416
|
-
const tasks = [];
|
|
417
509
|
const entries = /* @__PURE__ */ new Set();
|
|
418
510
|
let invalidateTimer;
|
|
419
511
|
const lastServedHash = /* @__PURE__ */ new Map();
|
|
@@ -421,7 +513,7 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
|
|
|
421
513
|
let resolved = false;
|
|
422
514
|
let resolvedWarnTimer;
|
|
423
515
|
async function generateCSS(layer) {
|
|
424
|
-
await
|
|
516
|
+
await flushTasks();
|
|
425
517
|
let result;
|
|
426
518
|
let tokensSize = tokens.size;
|
|
427
519
|
do {
|
|
@@ -878,6 +970,7 @@ function SvelteScopedPlugin({ ready, uno }) {
|
|
|
878
970
|
function ShadowDomModuleModePlugin({ uno }) {
|
|
879
971
|
const partExtractorRegex = /^part-\[(.+)]:/;
|
|
880
972
|
const nameRegexp = /<([^\s^!>]+)\s*([^>]*)>/;
|
|
973
|
+
const vueSFCStyleRE = new RegExp(`<style.*>[\\s\\S]*${CSS_PLACEHOLDER}[\\s\\S]*<\\/style>`);
|
|
881
974
|
const checkElement = (useParts, idxResolver, element) => {
|
|
882
975
|
if (!element)
|
|
883
976
|
return null;
|
|
@@ -910,7 +1003,7 @@ function ShadowDomModuleModePlugin({ uno }) {
|
|
|
910
1003
|
}
|
|
911
1004
|
};
|
|
912
1005
|
};
|
|
913
|
-
const transformWebComponent = async (code) => {
|
|
1006
|
+
const transformWebComponent = async (code, id) => {
|
|
914
1007
|
if (!code.match(CSS_PLACEHOLDER))
|
|
915
1008
|
return code;
|
|
916
1009
|
let { css, matched } = await uno.generate(code, {
|
|
@@ -955,19 +1048,21 @@ function ShadowDomModuleModePlugin({ uno }) {
|
|
|
955
1048
|
}
|
|
956
1049
|
}
|
|
957
1050
|
}
|
|
1051
|
+
if (id.includes("?vue&type=style") || id.endsWith(".vue") && vueSFCStyleRE.test(code))
|
|
1052
|
+
return code.replace(new RegExp(`(\\/\\*\\s*)?${CSS_PLACEHOLDER}(\\s*\\*\\/)?`), css || "");
|
|
958
1053
|
return code.replace(CSS_PLACEHOLDER, css?.replace(/\\/g, "\\\\") ?? "");
|
|
959
1054
|
};
|
|
960
1055
|
return {
|
|
961
1056
|
name: "unocss:shadow-dom",
|
|
962
1057
|
enforce: "pre",
|
|
963
|
-
async transform(code) {
|
|
964
|
-
return transformWebComponent(code);
|
|
1058
|
+
async transform(code, id) {
|
|
1059
|
+
return transformWebComponent(code, id);
|
|
965
1060
|
},
|
|
966
1061
|
handleHotUpdate(ctx) {
|
|
967
1062
|
const read = ctx.read;
|
|
968
1063
|
ctx.read = async () => {
|
|
969
1064
|
const code = await read();
|
|
970
|
-
return await transformWebComponent(code);
|
|
1065
|
+
return await transformWebComponent(code, ctx.file);
|
|
971
1066
|
};
|
|
972
1067
|
}
|
|
973
1068
|
};
|
|
@@ -999,38 +1094,6 @@ function ConfigHMRPlugin(ctx) {
|
|
|
999
1094
|
};
|
|
1000
1095
|
}
|
|
1001
1096
|
|
|
1002
|
-
async function applyTransformers(ctx, original, id, enforce = "default") {
|
|
1003
|
-
if (original.includes(IGNORE_COMMENT))
|
|
1004
|
-
return;
|
|
1005
|
-
const transformers = (ctx.uno.config.transformers || []).filter((i) => (i.enforce || "default") === enforce);
|
|
1006
|
-
if (!transformers.length)
|
|
1007
|
-
return;
|
|
1008
|
-
let code = original;
|
|
1009
|
-
let s = new MagicString(code);
|
|
1010
|
-
const maps = [];
|
|
1011
|
-
for (const t of transformers) {
|
|
1012
|
-
if (t.idFilter) {
|
|
1013
|
-
if (!t.idFilter(id))
|
|
1014
|
-
continue;
|
|
1015
|
-
} else if (!ctx.filter(code, id)) {
|
|
1016
|
-
continue;
|
|
1017
|
-
}
|
|
1018
|
-
await t.transform(s, id, ctx);
|
|
1019
|
-
if (s.hasChanged()) {
|
|
1020
|
-
code = s.toString();
|
|
1021
|
-
maps.push(s.generateMap({ hires: true, source: id }));
|
|
1022
|
-
s = new MagicString(code);
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
if (code !== original) {
|
|
1026
|
-
ctx.affectedModules.add(id);
|
|
1027
|
-
return {
|
|
1028
|
-
code,
|
|
1029
|
-
map: remapping(maps, () => null)
|
|
1030
|
-
};
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
1097
|
function createTransformerPlugins(ctx) {
|
|
1035
1098
|
const enforces = ["default", "pre", "post"];
|
|
1036
1099
|
return enforces.map((enforce) => ({
|
|
@@ -1158,7 +1221,7 @@ function createDevtoolsPlugin(ctx) {
|
|
|
1158
1221
|
if (id === DEVTOOLS_PATH) {
|
|
1159
1222
|
if (!clientCode) {
|
|
1160
1223
|
clientCode = [
|
|
1161
|
-
await fs.promises.readFile(resolve(_dirname, "client.mjs"), "utf-8"),
|
|
1224
|
+
await fs$1.promises.readFile(resolve(_dirname, "client.mjs"), "utf-8"),
|
|
1162
1225
|
`import('${MOCK_CLASSES_MODULE_ID}')`,
|
|
1163
1226
|
`import('${DEVTOOLS_CSS_PATH}')`
|
|
1164
1227
|
].join("\n").replace("__POST_PATH__", (config.server?.origin ?? "") + POST_PATH);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/vite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.48.0",
|
|
4
4
|
"description": "The Vite plugin for UnoCSS",
|
|
5
5
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,21 +39,23 @@
|
|
|
39
39
|
"dist"
|
|
40
40
|
],
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"vite": "^2.9.0 || ^3.0.0-0"
|
|
42
|
+
"vite": "^2.9.0 || ^3.0.0-0 || ^4.0.0"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@ampproject/remapping": "^2.2.0",
|
|
46
46
|
"@rollup/pluginutils": "^5.0.2",
|
|
47
|
-
"@unocss/config": "0.
|
|
48
|
-
"@unocss/core": "0.
|
|
49
|
-
"@unocss/inspector": "0.
|
|
50
|
-
"@unocss/scope": "0.
|
|
51
|
-
"@unocss/transformer-directives": "0.
|
|
52
|
-
"
|
|
47
|
+
"@unocss/config": "0.48.0",
|
|
48
|
+
"@unocss/core": "0.48.0",
|
|
49
|
+
"@unocss/inspector": "0.48.0",
|
|
50
|
+
"@unocss/scope": "0.48.0",
|
|
51
|
+
"@unocss/transformer-directives": "0.48.0",
|
|
52
|
+
"chokidar": "^3.5.3",
|
|
53
|
+
"fast-glob": "^3.2.12",
|
|
54
|
+
"magic-string": "^0.27.0"
|
|
53
55
|
},
|
|
54
56
|
"devDependencies": {
|
|
55
|
-
"@unocss/shared-integration": "0.
|
|
56
|
-
"vite": "^
|
|
57
|
+
"@unocss/shared-integration": "0.48.0",
|
|
58
|
+
"vite": "^4.0.0"
|
|
57
59
|
},
|
|
58
60
|
"scripts": {
|
|
59
61
|
"build": "unbuild",
|