@unocss/vite 0.11.6 → 0.12.4
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 +98 -96
- package/dist/index.mjs +99 -97
- package/package.json +6 -6
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) {
|
|
@@ -195,7 +208,12 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
195
208
|
if (layer)
|
|
196
209
|
return getLayerPlaceholder(layer);
|
|
197
210
|
},
|
|
211
|
+
configResolved(config) {
|
|
212
|
+
cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
213
|
+
},
|
|
198
214
|
async renderChunk(_, chunk) {
|
|
215
|
+
if (!cssPlugin)
|
|
216
|
+
return null;
|
|
199
217
|
const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
|
|
200
218
|
if (!chunks.length)
|
|
201
219
|
return null;
|
|
@@ -223,9 +241,6 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
223
241
|
var _a;
|
|
224
242
|
return command === "build" && !((_a = options.build) == null ? void 0 : _a.ssr);
|
|
225
243
|
},
|
|
226
|
-
configResolved(config2) {
|
|
227
|
-
cssPlugin = config2.plugins.find((i) => i.name === "vite:css-post");
|
|
228
|
-
},
|
|
229
244
|
enforce: "post",
|
|
230
245
|
async generateBundle(_, bundle) {
|
|
231
246
|
const files = Object.keys(bundle);
|
|
@@ -251,17 +266,13 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
251
266
|
];
|
|
252
267
|
}
|
|
253
268
|
|
|
254
|
-
// src/modes/global/dev.ts
|
|
255
|
-
var import_pluginutils3 = __toModule(require("@rollup/pluginutils"));
|
|
256
|
-
|
|
257
269
|
// src/modes/global/shared.ts
|
|
258
270
|
var READY_CALLBACK_DEFAULT = "/__unocss_ready";
|
|
259
271
|
|
|
260
272
|
// src/modes/global/dev.ts
|
|
261
273
|
var WARN_TIMEOUT = 2e3;
|
|
262
|
-
function GlobalModeDevPlugin({
|
|
274
|
+
function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
263
275
|
const servers = [];
|
|
264
|
-
const filter = (0, import_pluginutils3.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
265
276
|
const tasks = [];
|
|
266
277
|
const entries = new Map();
|
|
267
278
|
let invalidateTimer;
|
|
@@ -332,14 +343,14 @@ function GlobalModeDevPlugin({ config, uno, tokens, onInvalidate, scan }) {
|
|
|
332
343
|
});
|
|
333
344
|
},
|
|
334
345
|
transform(code, id) {
|
|
335
|
-
if (filter(id))
|
|
336
|
-
|
|
346
|
+
if (filter(code, id))
|
|
347
|
+
extract(code, id);
|
|
337
348
|
return null;
|
|
338
349
|
},
|
|
339
350
|
transformIndexHtml: {
|
|
340
351
|
enforce: "pre",
|
|
341
352
|
transform(code, { filename }) {
|
|
342
|
-
|
|
353
|
+
extract(code, filename);
|
|
343
354
|
}
|
|
344
355
|
},
|
|
345
356
|
resolveId(id) {
|
|
@@ -362,9 +373,9 @@ function GlobalModeDevPlugin({ config, uno, tokens, onInvalidate, scan }) {
|
|
|
362
373
|
},
|
|
363
374
|
{
|
|
364
375
|
name: "unocss:global:post",
|
|
365
|
-
apply(
|
|
376
|
+
apply(config, env) {
|
|
366
377
|
var _a;
|
|
367
|
-
return env.command === "serve" && !((_a =
|
|
378
|
+
return env.command === "serve" && !((_a = config.build) == null ? void 0 : _a.ssr);
|
|
368
379
|
},
|
|
369
380
|
enforce: "post",
|
|
370
381
|
transform(code, id) {
|
|
@@ -385,13 +396,11 @@ function GlobalModePlugin(ctx) {
|
|
|
385
396
|
}
|
|
386
397
|
|
|
387
398
|
// src/modes/per-module.ts
|
|
388
|
-
var import_pluginutils4 = __toModule(require("@rollup/pluginutils"));
|
|
389
399
|
var VIRTUAL_PREFIX = "/@unocss/";
|
|
390
400
|
var SCOPE_IMPORT_RE = / from (['"])(@unocss\/scope)\1/;
|
|
391
|
-
function PerModuleModePlugin({ uno,
|
|
401
|
+
function PerModuleModePlugin({ uno, filter }) {
|
|
392
402
|
const moduleMap = new Map();
|
|
393
403
|
let server;
|
|
394
|
-
const filter = (0, import_pluginutils4.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
395
404
|
const invalidate = (hash) => {
|
|
396
405
|
if (!server)
|
|
397
406
|
return;
|
|
@@ -417,7 +426,7 @@ function PerModuleModePlugin({ uno, config }) {
|
|
|
417
426
|
server = _server;
|
|
418
427
|
},
|
|
419
428
|
async transform(code, id) {
|
|
420
|
-
if (!filter(id))
|
|
429
|
+
if (!filter(code, id))
|
|
421
430
|
return;
|
|
422
431
|
const hash = getHash(id);
|
|
423
432
|
const hasScope = code.match(SCOPE_IMPORT_RE);
|
|
@@ -451,9 +460,9 @@ ${css}`;
|
|
|
451
460
|
}
|
|
452
461
|
|
|
453
462
|
// src/modes/vue-scoped.ts
|
|
454
|
-
var
|
|
463
|
+
var import_pluginutils2 = __toModule(require("@rollup/pluginutils"));
|
|
455
464
|
function VueScopedPlugin({ uno, config }) {
|
|
456
|
-
const filter = (0,
|
|
465
|
+
const filter = (0, import_pluginutils2.createFilter)(config.include || [/\.vue$/], config.exclude || defaultExclude);
|
|
457
466
|
async function transformSFC(code) {
|
|
458
467
|
const { css } = await uno.generate(code);
|
|
459
468
|
if (!css)
|
|
@@ -482,23 +491,18 @@ function VueScopedPlugin({ uno, config }) {
|
|
|
482
491
|
}
|
|
483
492
|
|
|
484
493
|
// src/config-hmr.ts
|
|
485
|
-
|
|
486
|
-
function ConfigHMRPlugin({ uno, configFilepath: filepath, invalidate, tokens, modules }) {
|
|
494
|
+
function ConfigHMRPlugin({ uno, configFilepath, reloadConfig }) {
|
|
487
495
|
return {
|
|
488
496
|
name: "unocss:config",
|
|
489
497
|
configureServer(server) {
|
|
490
498
|
uno.config.envMode = "dev";
|
|
491
|
-
if (!
|
|
499
|
+
if (!configFilepath)
|
|
492
500
|
return;
|
|
493
|
-
server.watcher.add(
|
|
501
|
+
server.watcher.add(configFilepath);
|
|
494
502
|
server.watcher.on("change", async (p) => {
|
|
495
|
-
if (p !==
|
|
503
|
+
if (p !== configFilepath)
|
|
496
504
|
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();
|
|
505
|
+
reloadConfig();
|
|
502
506
|
server.ws.send({
|
|
503
507
|
type: "custom",
|
|
504
508
|
event: "unocss:config-changed"
|
|
@@ -513,11 +517,9 @@ function defineConfig(config) {
|
|
|
513
517
|
return config;
|
|
514
518
|
}
|
|
515
519
|
function UnocssPlugin(configOrPath, defaults = {}) {
|
|
516
|
-
|
|
517
|
-
const { config
|
|
518
|
-
const mode =
|
|
519
|
-
const uno = (0, import_core2.createGenerator)(config, defaults);
|
|
520
|
-
const ctx = createContext(uno, config, filepath);
|
|
520
|
+
const ctx = createContext(configOrPath, defaults);
|
|
521
|
+
const { config } = ctx;
|
|
522
|
+
const mode = config.mode ?? "global";
|
|
521
523
|
const plugins = [
|
|
522
524
|
ConfigHMRPlugin(ctx)
|
|
523
525
|
];
|
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) {
|
|
@@ -161,7 +174,12 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
161
174
|
if (layer)
|
|
162
175
|
return getLayerPlaceholder(layer);
|
|
163
176
|
},
|
|
177
|
+
configResolved(config) {
|
|
178
|
+
cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
|
|
179
|
+
},
|
|
164
180
|
async renderChunk(_, chunk) {
|
|
181
|
+
if (!cssPlugin)
|
|
182
|
+
return null;
|
|
165
183
|
const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
|
|
166
184
|
if (!chunks.length)
|
|
167
185
|
return null;
|
|
@@ -189,9 +207,6 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
189
207
|
var _a;
|
|
190
208
|
return command === "build" && !((_a = options.build) == null ? void 0 : _a.ssr);
|
|
191
209
|
},
|
|
192
|
-
configResolved(config2) {
|
|
193
|
-
cssPlugin = config2.plugins.find((i) => i.name === "vite:css-post");
|
|
194
|
-
},
|
|
195
210
|
enforce: "post",
|
|
196
211
|
async generateBundle(_, bundle) {
|
|
197
212
|
const files = Object.keys(bundle);
|
|
@@ -217,17 +232,13 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
|
|
|
217
232
|
];
|
|
218
233
|
}
|
|
219
234
|
|
|
220
|
-
// src/modes/global/dev.ts
|
|
221
|
-
import { createFilter as createFilter3 } from "@rollup/pluginutils";
|
|
222
|
-
|
|
223
235
|
// src/modes/global/shared.ts
|
|
224
236
|
var READY_CALLBACK_DEFAULT = "/__unocss_ready";
|
|
225
237
|
|
|
226
238
|
// src/modes/global/dev.ts
|
|
227
239
|
var WARN_TIMEOUT = 2e3;
|
|
228
|
-
function GlobalModeDevPlugin({
|
|
240
|
+
function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
|
|
229
241
|
const servers = [];
|
|
230
|
-
const filter = createFilter3(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
231
242
|
const tasks = [];
|
|
232
243
|
const entries = new Map();
|
|
233
244
|
let invalidateTimer;
|
|
@@ -298,14 +309,14 @@ function GlobalModeDevPlugin({ config, uno, tokens, onInvalidate, scan }) {
|
|
|
298
309
|
});
|
|
299
310
|
},
|
|
300
311
|
transform(code, id) {
|
|
301
|
-
if (filter(id))
|
|
302
|
-
|
|
312
|
+
if (filter(code, id))
|
|
313
|
+
extract(code, id);
|
|
303
314
|
return null;
|
|
304
315
|
},
|
|
305
316
|
transformIndexHtml: {
|
|
306
317
|
enforce: "pre",
|
|
307
318
|
transform(code, { filename }) {
|
|
308
|
-
|
|
319
|
+
extract(code, filename);
|
|
309
320
|
}
|
|
310
321
|
},
|
|
311
322
|
resolveId(id) {
|
|
@@ -328,9 +339,9 @@ function GlobalModeDevPlugin({ config, uno, tokens, onInvalidate, scan }) {
|
|
|
328
339
|
},
|
|
329
340
|
{
|
|
330
341
|
name: "unocss:global:post",
|
|
331
|
-
apply(
|
|
342
|
+
apply(config, env) {
|
|
332
343
|
var _a;
|
|
333
|
-
return env.command === "serve" && !((_a =
|
|
344
|
+
return env.command === "serve" && !((_a = config.build) == null ? void 0 : _a.ssr);
|
|
334
345
|
},
|
|
335
346
|
enforce: "post",
|
|
336
347
|
transform(code, id) {
|
|
@@ -351,13 +362,11 @@ function GlobalModePlugin(ctx) {
|
|
|
351
362
|
}
|
|
352
363
|
|
|
353
364
|
// src/modes/per-module.ts
|
|
354
|
-
import { createFilter as createFilter4 } from "@rollup/pluginutils";
|
|
355
365
|
var VIRTUAL_PREFIX = "/@unocss/";
|
|
356
366
|
var SCOPE_IMPORT_RE = / from (['"])(@unocss\/scope)\1/;
|
|
357
|
-
function PerModuleModePlugin({ uno,
|
|
367
|
+
function PerModuleModePlugin({ uno, filter }) {
|
|
358
368
|
const moduleMap = new Map();
|
|
359
369
|
let server;
|
|
360
|
-
const filter = createFilter4(config.include || defaultInclude, config.exclude || defaultExclude);
|
|
361
370
|
const invalidate = (hash) => {
|
|
362
371
|
if (!server)
|
|
363
372
|
return;
|
|
@@ -383,7 +392,7 @@ function PerModuleModePlugin({ uno, config }) {
|
|
|
383
392
|
server = _server;
|
|
384
393
|
},
|
|
385
394
|
async transform(code, id) {
|
|
386
|
-
if (!filter(id))
|
|
395
|
+
if (!filter(code, id))
|
|
387
396
|
return;
|
|
388
397
|
const hash = getHash(id);
|
|
389
398
|
const hasScope = code.match(SCOPE_IMPORT_RE);
|
|
@@ -417,9 +426,9 @@ ${css}`;
|
|
|
417
426
|
}
|
|
418
427
|
|
|
419
428
|
// src/modes/vue-scoped.ts
|
|
420
|
-
import { createFilter as
|
|
429
|
+
import { createFilter as createFilter2 } from "@rollup/pluginutils";
|
|
421
430
|
function VueScopedPlugin({ uno, config }) {
|
|
422
|
-
const filter =
|
|
431
|
+
const filter = createFilter2(config.include || [/\.vue$/], config.exclude || defaultExclude);
|
|
423
432
|
async function transformSFC(code) {
|
|
424
433
|
const { css } = await uno.generate(code);
|
|
425
434
|
if (!css)
|
|
@@ -448,23 +457,18 @@ function VueScopedPlugin({ uno, config }) {
|
|
|
448
457
|
}
|
|
449
458
|
|
|
450
459
|
// src/config-hmr.ts
|
|
451
|
-
|
|
452
|
-
function ConfigHMRPlugin({ uno, configFilepath: filepath, invalidate, tokens, modules }) {
|
|
460
|
+
function ConfigHMRPlugin({ uno, configFilepath, reloadConfig }) {
|
|
453
461
|
return {
|
|
454
462
|
name: "unocss:config",
|
|
455
463
|
configureServer(server) {
|
|
456
464
|
uno.config.envMode = "dev";
|
|
457
|
-
if (!
|
|
465
|
+
if (!configFilepath)
|
|
458
466
|
return;
|
|
459
|
-
server.watcher.add(
|
|
467
|
+
server.watcher.add(configFilepath);
|
|
460
468
|
server.watcher.on("change", async (p) => {
|
|
461
|
-
if (p !==
|
|
469
|
+
if (p !== configFilepath)
|
|
462
470
|
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();
|
|
471
|
+
reloadConfig();
|
|
468
472
|
server.ws.send({
|
|
469
473
|
type: "custom",
|
|
470
474
|
event: "unocss:config-changed"
|
|
@@ -479,11 +483,9 @@ function defineConfig(config) {
|
|
|
479
483
|
return config;
|
|
480
484
|
}
|
|
481
485
|
function UnocssPlugin(configOrPath, defaults = {}) {
|
|
482
|
-
|
|
483
|
-
const { config
|
|
484
|
-
const mode =
|
|
485
|
-
const uno = createGenerator(config, defaults);
|
|
486
|
-
const ctx = createContext(uno, config, filepath);
|
|
486
|
+
const ctx = createContext(configOrPath, defaults);
|
|
487
|
+
const { config } = ctx;
|
|
488
|
+
const mode = config.mode ?? "global";
|
|
487
489
|
const plugins = [
|
|
488
490
|
ConfigHMRPlugin(ctx)
|
|
489
491
|
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/vite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.4",
|
|
4
4
|
"description": "The Vite plugin for UnoCSS",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"unocss",
|
|
@@ -34,13 +34,13 @@
|
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@rollup/pluginutils": "^4.1.1",
|
|
37
|
-
"@unocss/config": "0.
|
|
38
|
-
"@unocss/core": "0.
|
|
39
|
-
"@unocss/
|
|
40
|
-
"@unocss/
|
|
37
|
+
"@unocss/config": "0.12.4",
|
|
38
|
+
"@unocss/core": "0.12.4",
|
|
39
|
+
"@unocss/inspector": "0.12.4",
|
|
40
|
+
"@unocss/scope": "0.12.4"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"vite": "^2.6.
|
|
43
|
+
"vite": "^2.6.14"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "tsup",
|