@unocss/vite 0.11.6 → 0.12.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/dist/index.d.ts +18 -17
- package/dist/index.js +94 -93
- package/dist/index.mjs +95 -94
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Plugin } from 'vite';
|
|
|
3
3
|
import { UserConfig, UnoGenerator, BetterMap, UserConfigDefaults } from '@unocss/core';
|
|
4
4
|
|
|
5
5
|
declare type FilterPattern = ReadonlyArray<string | RegExp> | string | RegExp | null;
|
|
6
|
-
interface
|
|
6
|
+
interface PluginConfig<Theme extends {} = {}> extends UserConfig<Theme> {
|
|
7
7
|
/**
|
|
8
8
|
* Patterns that filter the files being extracted.
|
|
9
9
|
*/
|
|
@@ -14,7 +14,7 @@ interface PluginOptions<Theme extends {} = {}> extends UserConfig<Theme> {
|
|
|
14
14
|
exclude?: FilterPattern;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
interface
|
|
17
|
+
interface VitePluginConfig<Theme extends {} = {}> extends PluginConfig<Theme> {
|
|
18
18
|
/**
|
|
19
19
|
* Enable UnoCSS inspector
|
|
20
20
|
*
|
|
@@ -34,31 +34,32 @@ interface VitePluginOptions<Theme extends {} = {}> extends PluginOptions<Theme>
|
|
|
34
34
|
mode?: 'global' | 'per-module' | 'vue-scoped' | 'dist-chunk';
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
interface UnocssPluginContext<Config extends PluginConfig = PluginConfig> {
|
|
38
|
+
uno: UnoGenerator;
|
|
39
|
+
config: Config;
|
|
38
40
|
tokens: Set<string>;
|
|
39
41
|
modules: BetterMap<string, string>;
|
|
42
|
+
filter: (code: string, id: string) => boolean;
|
|
43
|
+
reloadConfig: () => Promise<void>;
|
|
44
|
+
extract: (code: string, id?: string) => Promise<void>;
|
|
45
|
+
configFilepath?: string;
|
|
40
46
|
invalidate: () => void;
|
|
41
|
-
onInvalidate(fn: () => void)
|
|
42
|
-
|
|
43
|
-
scan: (code: string, id?: string | undefined) => Promise<void>;
|
|
44
|
-
config: VitePluginOptions<{}>;
|
|
45
|
-
configFilepath: string | undefined;
|
|
46
|
-
};
|
|
47
|
-
declare type UnocssPluginContext = ReturnType<typeof createContext>;
|
|
47
|
+
onInvalidate: (fn: () => void) => void;
|
|
48
|
+
}
|
|
48
49
|
|
|
49
|
-
declare function ChunkModeBuildPlugin({ uno,
|
|
50
|
+
declare function ChunkModeBuildPlugin({ uno, filter }: UnocssPluginContext): Plugin;
|
|
50
51
|
|
|
51
|
-
declare function GlobalModeDevPlugin({
|
|
52
|
+
declare function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[];
|
|
52
53
|
|
|
53
|
-
declare function GlobalModeBuildPlugin({ uno,
|
|
54
|
+
declare function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }: UnocssPluginContext): Plugin[];
|
|
54
55
|
|
|
55
56
|
declare function GlobalModePlugin(ctx: UnocssPluginContext): vite.Plugin[];
|
|
56
57
|
|
|
57
|
-
declare function PerModuleModePlugin({ uno,
|
|
58
|
+
declare function PerModuleModePlugin({ uno, filter }: UnocssPluginContext): Plugin;
|
|
58
59
|
|
|
59
60
|
declare function VueScopedPlugin({ uno, config }: UnocssPluginContext): Plugin;
|
|
60
61
|
|
|
61
|
-
declare function defineConfig<Theme extends {}>(config:
|
|
62
|
-
declare function UnocssPlugin(configOrPath?:
|
|
62
|
+
declare function defineConfig<Theme extends {}>(config: VitePluginConfig<Theme>): VitePluginConfig<Theme>;
|
|
63
|
+
declare function UnocssPlugin(configOrPath?: VitePluginConfig | string, defaults?: UserConfigDefaults): Plugin[];
|
|
63
64
|
|
|
64
|
-
export { ChunkModeBuildPlugin, GlobalModeBuildPlugin, GlobalModeDevPlugin, GlobalModePlugin, PerModuleModePlugin, UnocssPluginContext,
|
|
65
|
+
export { ChunkModeBuildPlugin, GlobalModeBuildPlugin, GlobalModeDevPlugin, GlobalModePlugin, PerModuleModePlugin, UnocssPluginContext, VitePluginConfig, VueScopedPlugin, UnocssPlugin as default, defineConfig };
|
package/dist/index.js
CHANGED
|
@@ -33,16 +33,54 @@ __export(exports, {
|
|
|
33
33
|
default: () => UnocssPlugin,
|
|
34
34
|
defineConfig: () => defineConfig
|
|
35
35
|
});
|
|
36
|
-
var import_core2 = __toModule(require("@unocss/core"));
|
|
37
|
-
var import_config2 = __toModule(require("@unocss/config"));
|
|
38
36
|
var import_inspector = __toModule(require("@unocss/inspector"));
|
|
39
37
|
|
|
40
|
-
//
|
|
38
|
+
// ../plugins-common/defaults.ts
|
|
39
|
+
var defaultExclude = [/\.(css|postcss|sass|scss|less|stylus|styl)$/];
|
|
40
|
+
var defaultInclude = [/\.vue$/, /\.vue\?vue/, /\.svelte$/, /\.[jt]sx$/, /\.mdx?$/, /\.astro$/];
|
|
41
|
+
|
|
42
|
+
// ../plugins-common/layers.ts
|
|
43
|
+
var VIRTUAL_ENTRY_ALIAS = [
|
|
44
|
+
/^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
|
|
45
|
+
];
|
|
46
|
+
var LAYER_MARK_ALL = "__ALL__";
|
|
47
|
+
function resolveId(id) {
|
|
48
|
+
for (const alias of VIRTUAL_ENTRY_ALIAS) {
|
|
49
|
+
const match = id.match(alias);
|
|
50
|
+
if (match) {
|
|
51
|
+
return match[1] ? {
|
|
52
|
+
id: `/__uno_${match[1]}.css`,
|
|
53
|
+
layer: match[1]
|
|
54
|
+
} : {
|
|
55
|
+
id: "/__uno.css",
|
|
56
|
+
layer: LAYER_MARK_ALL
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
var LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
|
|
62
|
+
function getLayerPlaceholder(layer) {
|
|
63
|
+
return `#--unocss--{layer:${layer}}`;
|
|
64
|
+
}
|
|
65
|
+
var HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*"(.+?)";?\s*}/g;
|
|
66
|
+
function getHashPlaceholder(hash) {
|
|
67
|
+
return `#--unocss-hash--{content:"${hash}"}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ../plugins-common/constants.ts
|
|
71
|
+
var INCLUDE_COMMENT = "@unocss-include";
|
|
72
|
+
|
|
73
|
+
// ../plugins-common/context.ts
|
|
74
|
+
var import_pluginutils = __toModule(require("@rollup/pluginutils"));
|
|
75
|
+
var import_config = __toModule(require("@unocss/config"));
|
|
41
76
|
var import_core = __toModule(require("@unocss/core"));
|
|
42
|
-
function createContext(
|
|
77
|
+
function createContext(configOrPath, defaults = {}) {
|
|
78
|
+
const { config = {}, filepath } = (0, import_config.loadConfig)(configOrPath);
|
|
79
|
+
const uno = (0, import_core.createGenerator)(config, defaults);
|
|
43
80
|
const invalidations = [];
|
|
44
81
|
const modules = new import_core.BetterMap();
|
|
45
82
|
const tokens = new Set();
|
|
83
|
+
let rollupFilter = (0, import_pluginutils.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
46
84
|
function invalidate() {
|
|
47
85
|
invalidations.forEach((cb) => cb());
|
|
48
86
|
}
|
|
@@ -52,6 +90,19 @@ function createContext(uno, config, configFilepath) {
|
|
|
52
90
|
await uno.applyExtractors(code, id, tokens);
|
|
53
91
|
invalidate();
|
|
54
92
|
}
|
|
93
|
+
async function reloadConfig() {
|
|
94
|
+
if (!filepath)
|
|
95
|
+
return;
|
|
96
|
+
uno.setConfig((0, import_config.loadConfig)(filepath).config);
|
|
97
|
+
uno.config.envMode = "dev";
|
|
98
|
+
rollupFilter = (0, import_pluginutils.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
99
|
+
tokens.clear();
|
|
100
|
+
await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
|
|
101
|
+
invalidate();
|
|
102
|
+
}
|
|
103
|
+
const filter = (code, id) => {
|
|
104
|
+
return code.includes(INCLUDE_COMMENT) || rollupFilter(id);
|
|
105
|
+
};
|
|
55
106
|
return {
|
|
56
107
|
tokens,
|
|
57
108
|
modules,
|
|
@@ -59,34 +110,37 @@ function createContext(uno, config, configFilepath) {
|
|
|
59
110
|
onInvalidate(fn) {
|
|
60
111
|
invalidations.push(fn);
|
|
61
112
|
},
|
|
113
|
+
filter,
|
|
114
|
+
reloadConfig,
|
|
62
115
|
uno,
|
|
63
|
-
scan,
|
|
116
|
+
extract: scan,
|
|
64
117
|
config,
|
|
65
|
-
configFilepath
|
|
118
|
+
configFilepath: filepath
|
|
66
119
|
};
|
|
67
120
|
}
|
|
68
121
|
|
|
69
|
-
//
|
|
70
|
-
var
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
122
|
+
// ../plugins-common/utils.ts
|
|
123
|
+
var import_crypto = __toModule(require("crypto"));
|
|
124
|
+
function getHash(input, length = 8) {
|
|
125
|
+
return (0, import_crypto.createHash)("sha256").update(input).digest("hex").substr(0, length);
|
|
126
|
+
}
|
|
127
|
+
function getPath(id) {
|
|
128
|
+
return id.replace(/\?.*$/, "");
|
|
129
|
+
}
|
|
75
130
|
|
|
76
131
|
// src/modes/chunk-build.ts
|
|
77
|
-
function ChunkModeBuildPlugin({ uno,
|
|
132
|
+
function ChunkModeBuildPlugin({ uno, filter }) {
|
|
78
133
|
let cssPlugin;
|
|
79
|
-
const filter = (0, import_pluginutils.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
80
134
|
const files = {};
|
|
81
135
|
return {
|
|
82
136
|
name: "unocss:chunk",
|
|
83
137
|
apply: "build",
|
|
84
138
|
enforce: "pre",
|
|
85
|
-
configResolved(
|
|
86
|
-
cssPlugin =
|
|
139
|
+
configResolved(config) {
|
|
140
|
+
cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
87
141
|
},
|
|
88
142
|
transform(code, id) {
|
|
89
|
-
if (!filter(id))
|
|
143
|
+
if (!filter(code, id))
|
|
90
144
|
return;
|
|
91
145
|
files[id] = code;
|
|
92
146
|
return null;
|
|
@@ -118,48 +172,7 @@ function ChunkModeBuildPlugin({ uno, config }) {
|
|
|
118
172
|
}
|
|
119
173
|
|
|
120
174
|
// src/modes/global/build.ts
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
// ../plugins-common/utils.ts
|
|
124
|
-
var import_crypto = __toModule(require("crypto"));
|
|
125
|
-
function getHash(input, length = 8) {
|
|
126
|
-
return (0, import_crypto.createHash)("sha256").update(input).digest("hex").substr(0, length);
|
|
127
|
-
}
|
|
128
|
-
function getPath(id) {
|
|
129
|
-
return id.replace(/\?.*$/, "");
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// ../plugins-common/layers.ts
|
|
133
|
-
var VIRTUAL_ENTRY_ALIAS = [
|
|
134
|
-
/^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
|
|
135
|
-
];
|
|
136
|
-
var LAYER_MARK_ALL = "__ALL__";
|
|
137
|
-
function resolveId(id) {
|
|
138
|
-
for (const alias of VIRTUAL_ENTRY_ALIAS) {
|
|
139
|
-
const match = id.match(alias);
|
|
140
|
-
if (match) {
|
|
141
|
-
return match[1] ? {
|
|
142
|
-
id: `/__uno_${match[1]}.css`,
|
|
143
|
-
layer: match[1]
|
|
144
|
-
} : {
|
|
145
|
-
id: "/__uno.css",
|
|
146
|
-
layer: LAYER_MARK_ALL
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
var LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
|
|
152
|
-
function getLayerPlaceholder(layer) {
|
|
153
|
-
return `#--unocss--{layer:${layer}}`;
|
|
154
|
-
}
|
|
155
|
-
var HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*"(.+?)";?\s*}/g;
|
|
156
|
-
function getHashPlaceholder(hash) {
|
|
157
|
-
return `#--unocss-hash--{content:"${hash}"}`;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// src/modes/global/build.ts
|
|
161
|
-
function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
162
|
-
const filter = (0, import_pluginutils2.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
175
|
+
function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }) {
|
|
163
176
|
const vfsLayerMap = new Map();
|
|
164
177
|
let tasks = [];
|
|
165
178
|
let cssPlugin;
|
|
@@ -173,14 +186,14 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
173
186
|
vfsLayerMap.clear();
|
|
174
187
|
},
|
|
175
188
|
transform(code, id) {
|
|
176
|
-
if (filter(id))
|
|
177
|
-
tasks.push(
|
|
189
|
+
if (filter(code, id))
|
|
190
|
+
tasks.push(extract(code, id));
|
|
178
191
|
return null;
|
|
179
192
|
},
|
|
180
193
|
transformIndexHtml: {
|
|
181
194
|
enforce: "pre",
|
|
182
195
|
transform(code, { filename }) {
|
|
183
|
-
tasks.push(
|
|
196
|
+
tasks.push(extract(code, filename));
|
|
184
197
|
}
|
|
185
198
|
},
|
|
186
199
|
resolveId(id) {
|
|
@@ -223,8 +236,8 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
223
236
|
var _a;
|
|
224
237
|
return command === "build" && !((_a = options.build) == null ? void 0 : _a.ssr);
|
|
225
238
|
},
|
|
226
|
-
configResolved(
|
|
227
|
-
cssPlugin =
|
|
239
|
+
configResolved(config) {
|
|
240
|
+
cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
228
241
|
},
|
|
229
242
|
enforce: "post",
|
|
230
243
|
async generateBundle(_, bundle) {
|
|
@@ -251,17 +264,13 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
251
264
|
];
|
|
252
265
|
}
|
|
253
266
|
|
|
254
|
-
// src/modes/global/dev.ts
|
|
255
|
-
var import_pluginutils3 = __toModule(require("@rollup/pluginutils"));
|
|
256
|
-
|
|
257
267
|
// src/modes/global/shared.ts
|
|
258
268
|
var READY_CALLBACK_DEFAULT = "/__unocss_ready";
|
|
259
269
|
|
|
260
270
|
// src/modes/global/dev.ts
|
|
261
271
|
var WARN_TIMEOUT = 2e3;
|
|
262
|
-
function GlobalModeDevPlugin({
|
|
272
|
+
function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
263
273
|
const servers = [];
|
|
264
|
-
const filter = (0, import_pluginutils3.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
265
274
|
const tasks = [];
|
|
266
275
|
const entries = new Map();
|
|
267
276
|
let invalidateTimer;
|
|
@@ -332,14 +341,14 @@ function GlobalModeDevPlugin({ config, uno, tokens, onInvalidate, scan }) {
|
|
|
332
341
|
});
|
|
333
342
|
},
|
|
334
343
|
transform(code, id) {
|
|
335
|
-
if (filter(id))
|
|
336
|
-
|
|
344
|
+
if (filter(code, id))
|
|
345
|
+
extract(code, id);
|
|
337
346
|
return null;
|
|
338
347
|
},
|
|
339
348
|
transformIndexHtml: {
|
|
340
349
|
enforce: "pre",
|
|
341
350
|
transform(code, { filename }) {
|
|
342
|
-
|
|
351
|
+
extract(code, filename);
|
|
343
352
|
}
|
|
344
353
|
},
|
|
345
354
|
resolveId(id) {
|
|
@@ -362,9 +371,9 @@ function GlobalModeDevPlugin({ config, uno, tokens, onInvalidate, scan }) {
|
|
|
362
371
|
},
|
|
363
372
|
{
|
|
364
373
|
name: "unocss:global:post",
|
|
365
|
-
apply(
|
|
374
|
+
apply(config, env) {
|
|
366
375
|
var _a;
|
|
367
|
-
return env.command === "serve" && !((_a =
|
|
376
|
+
return env.command === "serve" && !((_a = config.build) == null ? void 0 : _a.ssr);
|
|
368
377
|
},
|
|
369
378
|
enforce: "post",
|
|
370
379
|
transform(code, id) {
|
|
@@ -385,13 +394,11 @@ function GlobalModePlugin(ctx) {
|
|
|
385
394
|
}
|
|
386
395
|
|
|
387
396
|
// src/modes/per-module.ts
|
|
388
|
-
var import_pluginutils4 = __toModule(require("@rollup/pluginutils"));
|
|
389
397
|
var VIRTUAL_PREFIX = "/@unocss/";
|
|
390
398
|
var SCOPE_IMPORT_RE = / from (['"])(@unocss\/scope)\1/;
|
|
391
|
-
function PerModuleModePlugin({ uno,
|
|
399
|
+
function PerModuleModePlugin({ uno, filter }) {
|
|
392
400
|
const moduleMap = new Map();
|
|
393
401
|
let server;
|
|
394
|
-
const filter = (0, import_pluginutils4.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
395
402
|
const invalidate = (hash) => {
|
|
396
403
|
if (!server)
|
|
397
404
|
return;
|
|
@@ -417,7 +424,7 @@ function PerModuleModePlugin({ uno, config }) {
|
|
|
417
424
|
server = _server;
|
|
418
425
|
},
|
|
419
426
|
async transform(code, id) {
|
|
420
|
-
if (!filter(id))
|
|
427
|
+
if (!filter(code, id))
|
|
421
428
|
return;
|
|
422
429
|
const hash = getHash(id);
|
|
423
430
|
const hasScope = code.match(SCOPE_IMPORT_RE);
|
|
@@ -451,9 +458,9 @@ ${css}`;
|
|
|
451
458
|
}
|
|
452
459
|
|
|
453
460
|
// src/modes/vue-scoped.ts
|
|
454
|
-
var
|
|
461
|
+
var import_pluginutils2 = __toModule(require("@rollup/pluginutils"));
|
|
455
462
|
function VueScopedPlugin({ uno, config }) {
|
|
456
|
-
const filter = (0,
|
|
463
|
+
const filter = (0, import_pluginutils2.createFilter)(config.include || [/\.vue$/], config.exclude || defaultExclude);
|
|
457
464
|
async function transformSFC(code) {
|
|
458
465
|
const { css } = await uno.generate(code);
|
|
459
466
|
if (!css)
|
|
@@ -482,23 +489,18 @@ function VueScopedPlugin({ uno, config }) {
|
|
|
482
489
|
}
|
|
483
490
|
|
|
484
491
|
// src/config-hmr.ts
|
|
485
|
-
|
|
486
|
-
function ConfigHMRPlugin({ uno, configFilepath: filepath, invalidate, tokens, modules }) {
|
|
492
|
+
function ConfigHMRPlugin({ uno, configFilepath, reloadConfig }) {
|
|
487
493
|
return {
|
|
488
494
|
name: "unocss:config",
|
|
489
495
|
configureServer(server) {
|
|
490
496
|
uno.config.envMode = "dev";
|
|
491
|
-
if (!
|
|
497
|
+
if (!configFilepath)
|
|
492
498
|
return;
|
|
493
|
-
server.watcher.add(
|
|
499
|
+
server.watcher.add(configFilepath);
|
|
494
500
|
server.watcher.on("change", async (p) => {
|
|
495
|
-
if (p !==
|
|
501
|
+
if (p !== configFilepath)
|
|
496
502
|
return;
|
|
497
|
-
|
|
498
|
-
uno.config.envMode = "dev";
|
|
499
|
-
tokens.clear();
|
|
500
|
-
await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
|
|
501
|
-
invalidate();
|
|
503
|
+
reloadConfig();
|
|
502
504
|
server.ws.send({
|
|
503
505
|
type: "custom",
|
|
504
506
|
event: "unocss:config-changed"
|
|
@@ -514,10 +516,9 @@ function defineConfig(config) {
|
|
|
514
516
|
}
|
|
515
517
|
function UnocssPlugin(configOrPath, defaults = {}) {
|
|
516
518
|
var _a;
|
|
517
|
-
const
|
|
519
|
+
const ctx = createContext(configOrPath, defaults);
|
|
520
|
+
const { config } = ctx;
|
|
518
521
|
const mode = (_a = config.mode) != null ? _a : "global";
|
|
519
|
-
const uno = (0, import_core2.createGenerator)(config, defaults);
|
|
520
|
-
const ctx = createContext(uno, config, filepath);
|
|
521
522
|
const plugins = [
|
|
522
523
|
ConfigHMRPlugin(ctx)
|
|
523
524
|
];
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,52 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { createGenerator } from "@unocss/core";
|
|
3
|
-
import { loadConfig as loadConfig2 } from "@unocss/config";
|
|
4
2
|
import UnocssInspector from "@unocss/inspector";
|
|
5
3
|
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
// ../plugins-common/defaults.ts
|
|
5
|
+
var defaultExclude = [/\.(css|postcss|sass|scss|less|stylus|styl)$/];
|
|
6
|
+
var defaultInclude = [/\.vue$/, /\.vue\?vue/, /\.svelte$/, /\.[jt]sx$/, /\.mdx?$/, /\.astro$/];
|
|
7
|
+
|
|
8
|
+
// ../plugins-common/layers.ts
|
|
9
|
+
var VIRTUAL_ENTRY_ALIAS = [
|
|
10
|
+
/^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
|
|
11
|
+
];
|
|
12
|
+
var LAYER_MARK_ALL = "__ALL__";
|
|
13
|
+
function resolveId(id) {
|
|
14
|
+
for (const alias of VIRTUAL_ENTRY_ALIAS) {
|
|
15
|
+
const match = id.match(alias);
|
|
16
|
+
if (match) {
|
|
17
|
+
return match[1] ? {
|
|
18
|
+
id: `/__uno_${match[1]}.css`,
|
|
19
|
+
layer: match[1]
|
|
20
|
+
} : {
|
|
21
|
+
id: "/__uno.css",
|
|
22
|
+
layer: LAYER_MARK_ALL
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
var LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
|
|
28
|
+
function getLayerPlaceholder(layer) {
|
|
29
|
+
return `#--unocss--{layer:${layer}}`;
|
|
30
|
+
}
|
|
31
|
+
var HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*"(.+?)";?\s*}/g;
|
|
32
|
+
function getHashPlaceholder(hash) {
|
|
33
|
+
return `#--unocss-hash--{content:"${hash}"}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ../plugins-common/constants.ts
|
|
37
|
+
var INCLUDE_COMMENT = "@unocss-include";
|
|
38
|
+
|
|
39
|
+
// ../plugins-common/context.ts
|
|
40
|
+
import { createFilter } from "@rollup/pluginutils";
|
|
41
|
+
import { loadConfig } from "@unocss/config";
|
|
42
|
+
import { BetterMap, createGenerator } from "@unocss/core";
|
|
43
|
+
function createContext(configOrPath, defaults = {}) {
|
|
44
|
+
const { config = {}, filepath } = loadConfig(configOrPath);
|
|
45
|
+
const uno = createGenerator(config, defaults);
|
|
9
46
|
const invalidations = [];
|
|
10
47
|
const modules = new BetterMap();
|
|
11
48
|
const tokens = new Set();
|
|
49
|
+
let rollupFilter = createFilter(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
12
50
|
function invalidate() {
|
|
13
51
|
invalidations.forEach((cb) => cb());
|
|
14
52
|
}
|
|
@@ -18,6 +56,19 @@ function createContext(uno, config, configFilepath) {
|
|
|
18
56
|
await uno.applyExtractors(code, id, tokens);
|
|
19
57
|
invalidate();
|
|
20
58
|
}
|
|
59
|
+
async function reloadConfig() {
|
|
60
|
+
if (!filepath)
|
|
61
|
+
return;
|
|
62
|
+
uno.setConfig(loadConfig(filepath).config);
|
|
63
|
+
uno.config.envMode = "dev";
|
|
64
|
+
rollupFilter = createFilter(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
65
|
+
tokens.clear();
|
|
66
|
+
await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
|
|
67
|
+
invalidate();
|
|
68
|
+
}
|
|
69
|
+
const filter = (code, id) => {
|
|
70
|
+
return code.includes(INCLUDE_COMMENT) || rollupFilter(id);
|
|
71
|
+
};
|
|
21
72
|
return {
|
|
22
73
|
tokens,
|
|
23
74
|
modules,
|
|
@@ -25,34 +76,37 @@ function createContext(uno, config, configFilepath) {
|
|
|
25
76
|
onInvalidate(fn) {
|
|
26
77
|
invalidations.push(fn);
|
|
27
78
|
},
|
|
79
|
+
filter,
|
|
80
|
+
reloadConfig,
|
|
28
81
|
uno,
|
|
29
|
-
scan,
|
|
82
|
+
extract: scan,
|
|
30
83
|
config,
|
|
31
|
-
configFilepath
|
|
84
|
+
configFilepath: filepath
|
|
32
85
|
};
|
|
33
86
|
}
|
|
34
87
|
|
|
35
|
-
//
|
|
36
|
-
import {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
88
|
+
// ../plugins-common/utils.ts
|
|
89
|
+
import { createHash } from "crypto";
|
|
90
|
+
function getHash(input, length = 8) {
|
|
91
|
+
return createHash("sha256").update(input).digest("hex").substr(0, length);
|
|
92
|
+
}
|
|
93
|
+
function getPath(id) {
|
|
94
|
+
return id.replace(/\?.*$/, "");
|
|
95
|
+
}
|
|
41
96
|
|
|
42
97
|
// src/modes/chunk-build.ts
|
|
43
|
-
function ChunkModeBuildPlugin({ uno,
|
|
98
|
+
function ChunkModeBuildPlugin({ uno, filter }) {
|
|
44
99
|
let cssPlugin;
|
|
45
|
-
const filter = createFilter(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
46
100
|
const files = {};
|
|
47
101
|
return {
|
|
48
102
|
name: "unocss:chunk",
|
|
49
103
|
apply: "build",
|
|
50
104
|
enforce: "pre",
|
|
51
|
-
configResolved(
|
|
52
|
-
cssPlugin =
|
|
105
|
+
configResolved(config) {
|
|
106
|
+
cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
53
107
|
},
|
|
54
108
|
transform(code, id) {
|
|
55
|
-
if (!filter(id))
|
|
109
|
+
if (!filter(code, id))
|
|
56
110
|
return;
|
|
57
111
|
files[id] = code;
|
|
58
112
|
return null;
|
|
@@ -84,48 +138,7 @@ function ChunkModeBuildPlugin({ uno, config }) {
|
|
|
84
138
|
}
|
|
85
139
|
|
|
86
140
|
// src/modes/global/build.ts
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
// ../plugins-common/utils.ts
|
|
90
|
-
import { createHash } from "crypto";
|
|
91
|
-
function getHash(input, length = 8) {
|
|
92
|
-
return createHash("sha256").update(input).digest("hex").substr(0, length);
|
|
93
|
-
}
|
|
94
|
-
function getPath(id) {
|
|
95
|
-
return id.replace(/\?.*$/, "");
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// ../plugins-common/layers.ts
|
|
99
|
-
var VIRTUAL_ENTRY_ALIAS = [
|
|
100
|
-
/^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
|
|
101
|
-
];
|
|
102
|
-
var LAYER_MARK_ALL = "__ALL__";
|
|
103
|
-
function resolveId(id) {
|
|
104
|
-
for (const alias of VIRTUAL_ENTRY_ALIAS) {
|
|
105
|
-
const match = id.match(alias);
|
|
106
|
-
if (match) {
|
|
107
|
-
return match[1] ? {
|
|
108
|
-
id: `/__uno_${match[1]}.css`,
|
|
109
|
-
layer: match[1]
|
|
110
|
-
} : {
|
|
111
|
-
id: "/__uno.css",
|
|
112
|
-
layer: LAYER_MARK_ALL
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
var LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
|
|
118
|
-
function getLayerPlaceholder(layer) {
|
|
119
|
-
return `#--unocss--{layer:${layer}}`;
|
|
120
|
-
}
|
|
121
|
-
var HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*"(.+?)";?\s*}/g;
|
|
122
|
-
function getHashPlaceholder(hash) {
|
|
123
|
-
return `#--unocss-hash--{content:"${hash}"}`;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// src/modes/global/build.ts
|
|
127
|
-
function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
128
|
-
const filter = createFilter2(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
141
|
+
function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }) {
|
|
129
142
|
const vfsLayerMap = new Map();
|
|
130
143
|
let tasks = [];
|
|
131
144
|
let cssPlugin;
|
|
@@ -139,14 +152,14 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
139
152
|
vfsLayerMap.clear();
|
|
140
153
|
},
|
|
141
154
|
transform(code, id) {
|
|
142
|
-
if (filter(id))
|
|
143
|
-
tasks.push(
|
|
155
|
+
if (filter(code, id))
|
|
156
|
+
tasks.push(extract(code, id));
|
|
144
157
|
return null;
|
|
145
158
|
},
|
|
146
159
|
transformIndexHtml: {
|
|
147
160
|
enforce: "pre",
|
|
148
161
|
transform(code, { filename }) {
|
|
149
|
-
tasks.push(
|
|
162
|
+
tasks.push(extract(code, filename));
|
|
150
163
|
}
|
|
151
164
|
},
|
|
152
165
|
resolveId(id) {
|
|
@@ -189,8 +202,8 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
189
202
|
var _a;
|
|
190
203
|
return command === "build" && !((_a = options.build) == null ? void 0 : _a.ssr);
|
|
191
204
|
},
|
|
192
|
-
configResolved(
|
|
193
|
-
cssPlugin =
|
|
205
|
+
configResolved(config) {
|
|
206
|
+
cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
194
207
|
},
|
|
195
208
|
enforce: "post",
|
|
196
209
|
async generateBundle(_, bundle) {
|
|
@@ -217,17 +230,13 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
217
230
|
];
|
|
218
231
|
}
|
|
219
232
|
|
|
220
|
-
// src/modes/global/dev.ts
|
|
221
|
-
import { createFilter as createFilter3 } from "@rollup/pluginutils";
|
|
222
|
-
|
|
223
233
|
// src/modes/global/shared.ts
|
|
224
234
|
var READY_CALLBACK_DEFAULT = "/__unocss_ready";
|
|
225
235
|
|
|
226
236
|
// src/modes/global/dev.ts
|
|
227
237
|
var WARN_TIMEOUT = 2e3;
|
|
228
|
-
function GlobalModeDevPlugin({
|
|
238
|
+
function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
229
239
|
const servers = [];
|
|
230
|
-
const filter = createFilter3(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
231
240
|
const tasks = [];
|
|
232
241
|
const entries = new Map();
|
|
233
242
|
let invalidateTimer;
|
|
@@ -298,14 +307,14 @@ function GlobalModeDevPlugin({ config, uno, tokens, onInvalidate, scan }) {
|
|
|
298
307
|
});
|
|
299
308
|
},
|
|
300
309
|
transform(code, id) {
|
|
301
|
-
if (filter(id))
|
|
302
|
-
|
|
310
|
+
if (filter(code, id))
|
|
311
|
+
extract(code, id);
|
|
303
312
|
return null;
|
|
304
313
|
},
|
|
305
314
|
transformIndexHtml: {
|
|
306
315
|
enforce: "pre",
|
|
307
316
|
transform(code, { filename }) {
|
|
308
|
-
|
|
317
|
+
extract(code, filename);
|
|
309
318
|
}
|
|
310
319
|
},
|
|
311
320
|
resolveId(id) {
|
|
@@ -328,9 +337,9 @@ function GlobalModeDevPlugin({ config, uno, tokens, onInvalidate, scan }) {
|
|
|
328
337
|
},
|
|
329
338
|
{
|
|
330
339
|
name: "unocss:global:post",
|
|
331
|
-
apply(
|
|
340
|
+
apply(config, env) {
|
|
332
341
|
var _a;
|
|
333
|
-
return env.command === "serve" && !((_a =
|
|
342
|
+
return env.command === "serve" && !((_a = config.build) == null ? void 0 : _a.ssr);
|
|
334
343
|
},
|
|
335
344
|
enforce: "post",
|
|
336
345
|
transform(code, id) {
|
|
@@ -351,13 +360,11 @@ function GlobalModePlugin(ctx) {
|
|
|
351
360
|
}
|
|
352
361
|
|
|
353
362
|
// src/modes/per-module.ts
|
|
354
|
-
import { createFilter as createFilter4 } from "@rollup/pluginutils";
|
|
355
363
|
var VIRTUAL_PREFIX = "/@unocss/";
|
|
356
364
|
var SCOPE_IMPORT_RE = / from (['"])(@unocss\/scope)\1/;
|
|
357
|
-
function PerModuleModePlugin({ uno,
|
|
365
|
+
function PerModuleModePlugin({ uno, filter }) {
|
|
358
366
|
const moduleMap = new Map();
|
|
359
367
|
let server;
|
|
360
|
-
const filter = createFilter4(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
361
368
|
const invalidate = (hash) => {
|
|
362
369
|
if (!server)
|
|
363
370
|
return;
|
|
@@ -383,7 +390,7 @@ function PerModuleModePlugin({ uno, config }) {
|
|
|
383
390
|
server = _server;
|
|
384
391
|
},
|
|
385
392
|
async transform(code, id) {
|
|
386
|
-
if (!filter(id))
|
|
393
|
+
if (!filter(code, id))
|
|
387
394
|
return;
|
|
388
395
|
const hash = getHash(id);
|
|
389
396
|
const hasScope = code.match(SCOPE_IMPORT_RE);
|
|
@@ -417,9 +424,9 @@ ${css}`;
|
|
|
417
424
|
}
|
|
418
425
|
|
|
419
426
|
// src/modes/vue-scoped.ts
|
|
420
|
-
import { createFilter as
|
|
427
|
+
import { createFilter as createFilter2 } from "@rollup/pluginutils";
|
|
421
428
|
function VueScopedPlugin({ uno, config }) {
|
|
422
|
-
const filter =
|
|
429
|
+
const filter = createFilter2(config.include || [/\.vue$/], config.exclude || defaultExclude);
|
|
423
430
|
async function transformSFC(code) {
|
|
424
431
|
const { css } = await uno.generate(code);
|
|
425
432
|
if (!css)
|
|
@@ -448,23 +455,18 @@ function VueScopedPlugin({ uno, config }) {
|
|
|
448
455
|
}
|
|
449
456
|
|
|
450
457
|
// src/config-hmr.ts
|
|
451
|
-
|
|
452
|
-
function ConfigHMRPlugin({ uno, configFilepath: filepath, invalidate, tokens, modules }) {
|
|
458
|
+
function ConfigHMRPlugin({ uno, configFilepath, reloadConfig }) {
|
|
453
459
|
return {
|
|
454
460
|
name: "unocss:config",
|
|
455
461
|
configureServer(server) {
|
|
456
462
|
uno.config.envMode = "dev";
|
|
457
|
-
if (!
|
|
463
|
+
if (!configFilepath)
|
|
458
464
|
return;
|
|
459
|
-
server.watcher.add(
|
|
465
|
+
server.watcher.add(configFilepath);
|
|
460
466
|
server.watcher.on("change", async (p) => {
|
|
461
|
-
if (p !==
|
|
467
|
+
if (p !== configFilepath)
|
|
462
468
|
return;
|
|
463
|
-
|
|
464
|
-
uno.config.envMode = "dev";
|
|
465
|
-
tokens.clear();
|
|
466
|
-
await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
|
|
467
|
-
invalidate();
|
|
469
|
+
reloadConfig();
|
|
468
470
|
server.ws.send({
|
|
469
471
|
type: "custom",
|
|
470
472
|
event: "unocss:config-changed"
|
|
@@ -480,10 +482,9 @@ function defineConfig(config) {
|
|
|
480
482
|
}
|
|
481
483
|
function UnocssPlugin(configOrPath, defaults = {}) {
|
|
482
484
|
var _a;
|
|
483
|
-
const
|
|
485
|
+
const ctx = createContext(configOrPath, defaults);
|
|
486
|
+
const { config } = ctx;
|
|
484
487
|
const mode = (_a = config.mode) != null ? _a : "global";
|
|
485
|
-
const uno = createGenerator(config, defaults);
|
|
486
|
-
const ctx = createContext(uno, config, filepath);
|
|
487
488
|
const plugins = [
|
|
488
489
|
ConfigHMRPlugin(ctx)
|
|
489
490
|
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/vite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "The Vite plugin for UnoCSS",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"unocss",
|
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@rollup/pluginutils": "^4.1.1",
|
|
37
|
-
"@unocss/config": "0.
|
|
38
|
-
"@unocss/core": "0.
|
|
39
|
-
"@unocss/scope": "0.
|
|
40
|
-
"@unocss/inspector": "0.
|
|
37
|
+
"@unocss/config": "0.12.0",
|
|
38
|
+
"@unocss/core": "0.12.0",
|
|
39
|
+
"@unocss/scope": "0.12.0",
|
|
40
|
+
"@unocss/inspector": "0.12.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"vite": "^2.6.13"
|