@unocss/vite 0.45.30 → 0.46.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 +22 -2
- package/dist/index.cjs +158 -14
- package/dist/index.d.ts +34 -3
- package/dist/index.mjs +159 -16
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -273,7 +273,11 @@ You have a `SvelteKit` example project on [examples/sveltekit](https://github.co
|
|
|
273
273
|
|
|
274
274
|
### Svelte/SvelteKit Scoped Mode
|
|
275
275
|
|
|
276
|
-
Adding `mode: 'svelte-scoped'` to your UnoCSS config options will place styles right inside of each component's style block instead of in a global `uno.css` file.
|
|
276
|
+
Adding `mode: 'svelte-scoped'` to your UnoCSS config options will place styles right inside of each component's style block instead of in a global `uno.css` file. Due to automatic class name compilation, classes that depend on attributes in parent components (like `dir="rtl"` or `.dark`) will just work. Also, you can pass class to children components as long as you pass them using a prop named `class`, e.g. `class="text-lg bg-red-100"`.
|
|
277
|
+
|
|
278
|
+
Support for `class:foo` and `class:foo={bar}` is already included. There is no need to add the `extractorSvelte` when using `svelte-scoped` mode.
|
|
279
|
+
|
|
280
|
+
Because there is no `import 'uno.css'` in your root `+layout.svelte` preflights and safelist classes have no where to be placed. Add the `uno:preflights` or `uno:safelist` attributes to the style block of any component where you want to place them. To use both globally, add the following to your root `+layout.svelte`:
|
|
277
281
|
|
|
278
282
|
```html
|
|
279
283
|
<style uno:preflights uno:safelist global></style>
|
|
@@ -281,8 +285,24 @@ Adding `mode: 'svelte-scoped'` to your UnoCSS config options will place styles r
|
|
|
281
285
|
|
|
282
286
|
Alternatively, if you only want them to apply to a specific component just add them to that component's `style` tag and don't add the `global` attribute.
|
|
283
287
|
|
|
284
|
-
|
|
288
|
+
```ts
|
|
289
|
+
// vite.config.js
|
|
290
|
+
import { sveltekit } from '@sveltejs/kit/vite'
|
|
291
|
+
import UnoCSS from 'unocss/vite'
|
|
292
|
+
|
|
293
|
+
/** @type {import('vite').UserConfig} */
|
|
294
|
+
const config = {
|
|
295
|
+
plugins: [
|
|
296
|
+
UnoCSS({
|
|
297
|
+
mode: 'svelte-scoped',
|
|
298
|
+
/* options */
|
|
299
|
+
}),
|
|
300
|
+
sveltekit(),
|
|
301
|
+
],
|
|
302
|
+
}
|
|
303
|
+
```
|
|
285
304
|
|
|
305
|
+
There is a `SvelteKit scoped` example project in the [examples/sveltekit-scoped](https://github.com/unocss/unocss/tree/main/examples/sveltekit-scoped#readme) directory with more detailed explanation of how this mode works.
|
|
286
306
|
|
|
287
307
|
### Web Components
|
|
288
308
|
|
package/dist/index.cjs
CHANGED
|
@@ -663,23 +663,166 @@ function VueScopedPlugin({ uno, ready }) {
|
|
|
663
663
|
};
|
|
664
664
|
}
|
|
665
665
|
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
666
|
+
const SELECTOR_REGEX = /(?<![\d(])([[\.][\S\s]+?)({[\S\s]+?})/g;
|
|
667
|
+
function wrapSelectorsWithGlobal(css) {
|
|
668
|
+
return css.replace(SELECTOR_REGEX, ":global($1)$2");
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
function hash(str) {
|
|
672
|
+
let i;
|
|
673
|
+
let l;
|
|
674
|
+
let hval = 2166136261;
|
|
675
|
+
for (i = 0, l = str.length; i < l; i++) {
|
|
676
|
+
hval ^= str.charCodeAt(i);
|
|
677
|
+
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
|
|
678
|
+
}
|
|
679
|
+
return `00000${(hval >>> 0).toString(36)}`.slice(-6);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const classesRE = /class=(["'\`])([\S\s]+?)\1/g;
|
|
683
|
+
const classesDirectivesRE = /class:([\S]+?)={/g;
|
|
684
|
+
const classDirectivesShorthandRE = /class:([^=>\s/]+)[{>\s/]/g;
|
|
685
|
+
const classesFromInlineConditionalsRE = /'([\S\s]+?)'/g;
|
|
686
|
+
async function transformSvelteSFC(code, id, uno, options = {}) {
|
|
687
|
+
const {
|
|
688
|
+
classPrefix = "uno-",
|
|
689
|
+
combine = true,
|
|
690
|
+
hashFn = hash
|
|
691
|
+
} = options;
|
|
692
|
+
let styles = "";
|
|
693
|
+
let map;
|
|
694
|
+
const alreadyHasStyles = code.match(/<style[^>]*>[\s\S]*?<\/style\s*>/);
|
|
695
|
+
const preflights = code.includes("uno:preflights");
|
|
696
|
+
const safelist = code.includes("uno:safelist");
|
|
697
|
+
if (preflights || safelist) {
|
|
698
|
+
const { css: css2 } = await uno.generate("", { preflights, safelist });
|
|
699
|
+
styles = css2;
|
|
700
|
+
}
|
|
701
|
+
const classes = [...code.matchAll(classesRE)];
|
|
702
|
+
const classDirectives = [...code.matchAll(classesDirectivesRE)];
|
|
703
|
+
const classDirectivesShorthand = [...code.matchAll(classDirectivesShorthandRE)];
|
|
704
|
+
if (!classes.length && !classDirectives.length && !classDirectivesShorthand.length) {
|
|
705
|
+
if (preflights || safelist) {
|
|
706
|
+
if (alreadyHasStyles) {
|
|
707
|
+
return {
|
|
708
|
+
code: code.replace(/(<style[^>]*>)/, `$1${styles}`)
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
return { code: `${code}
|
|
712
|
+
<style>${styles}</style>` };
|
|
713
|
+
} else {
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
const originalShortcuts = uno.config.shortcuts;
|
|
718
|
+
const shortcuts = {};
|
|
719
|
+
const toGenerate = /* @__PURE__ */ new Set();
|
|
720
|
+
const s = new MagicString__default(code);
|
|
721
|
+
let idHash;
|
|
722
|
+
if (!combine)
|
|
723
|
+
idHash = hashFn(id);
|
|
724
|
+
function isOriginalOriginalShortcut(token) {
|
|
725
|
+
return !!originalShortcuts.find((s2) => s2[0] === token);
|
|
726
|
+
}
|
|
727
|
+
function queueCompiledClass(tokens) {
|
|
728
|
+
if (combine) {
|
|
729
|
+
const _shortcuts = tokens.filter((t) => isOriginalOriginalShortcut(t));
|
|
730
|
+
for (const s2 of _shortcuts)
|
|
731
|
+
toGenerate.add(s2);
|
|
732
|
+
const _tokens = tokens.filter((t) => !isOriginalOriginalShortcut(t));
|
|
733
|
+
if (!_tokens.length)
|
|
734
|
+
return _shortcuts.join(" ");
|
|
735
|
+
const hash2 = hashFn(_tokens.join(" ") + id);
|
|
736
|
+
const className = `${classPrefix}${hash2}`;
|
|
737
|
+
shortcuts[className] = _tokens;
|
|
738
|
+
toGenerate.add(className);
|
|
739
|
+
return [className, ..._shortcuts].join(" ");
|
|
740
|
+
} else {
|
|
741
|
+
return tokens.map((token) => {
|
|
742
|
+
if (isOriginalOriginalShortcut(token)) {
|
|
743
|
+
toGenerate.add(token);
|
|
744
|
+
return token;
|
|
745
|
+
}
|
|
746
|
+
const className = `_${token}_${idHash}`;
|
|
747
|
+
shortcuts[className] = [token];
|
|
748
|
+
toGenerate.add(className);
|
|
749
|
+
return className;
|
|
750
|
+
}).join(" ");
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
async function sortKnownAndUnknownClasses(str) {
|
|
754
|
+
const classArr = str.split(/\s+/);
|
|
755
|
+
const result = await Promise.all(classArr.filter(Boolean).map(async (t) => [t, !!await uno.parseToken(t)]));
|
|
756
|
+
const known = result.filter(([, matched]) => matched).map(([t]) => t).sort();
|
|
757
|
+
if (!known.length)
|
|
673
758
|
return null;
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
return
|
|
677
|
-
<style>${css}</style>`;
|
|
759
|
+
const replacements = result.filter(([, matched]) => !matched).map(([i]) => i);
|
|
760
|
+
const className = queueCompiledClass(known);
|
|
761
|
+
return [className, ...replacements].join(" ");
|
|
678
762
|
}
|
|
763
|
+
for (const match of classes) {
|
|
764
|
+
let body = core.expandVariantGroup(match[2].trim());
|
|
765
|
+
const inlineConditionals = [...body.matchAll(classesFromInlineConditionalsRE)];
|
|
766
|
+
for (const conditional of inlineConditionals) {
|
|
767
|
+
const replacement2 = await sortKnownAndUnknownClasses(conditional[1].trim());
|
|
768
|
+
if (replacement2)
|
|
769
|
+
body = body.replace(conditional[0], `'${replacement2}'`);
|
|
770
|
+
}
|
|
771
|
+
const replacement = await sortKnownAndUnknownClasses(body);
|
|
772
|
+
if (replacement) {
|
|
773
|
+
const start = match.index;
|
|
774
|
+
s.overwrite(start + 7, start + match[0].length - 1, replacement);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
for (const match of classDirectives) {
|
|
778
|
+
const token = match[1];
|
|
779
|
+
const result = !!await uno.parseToken(token);
|
|
780
|
+
if (!result)
|
|
781
|
+
continue;
|
|
782
|
+
const className = queueCompiledClass([token]);
|
|
783
|
+
const start = match.index + "class:".length;
|
|
784
|
+
s.overwrite(start, start + match[1].length, className);
|
|
785
|
+
}
|
|
786
|
+
for (const match of classDirectivesShorthand) {
|
|
787
|
+
const token = match[1];
|
|
788
|
+
const result = !!await uno.parseToken(token);
|
|
789
|
+
if (!result)
|
|
790
|
+
continue;
|
|
791
|
+
const className = queueCompiledClass([token]);
|
|
792
|
+
const start = match.index + "class:".length;
|
|
793
|
+
s.overwrite(start, start + match[1].length, `${className}={${token}}`);
|
|
794
|
+
}
|
|
795
|
+
uno.config.shortcuts = [...originalShortcuts, ...Object.entries(shortcuts)];
|
|
796
|
+
const { css } = await uno.generate(toGenerate, { preflights: false, safelist: false, minify: true });
|
|
797
|
+
styles += wrapSelectorsWithGlobal(css);
|
|
798
|
+
uno.config.shortcuts = originalShortcuts;
|
|
799
|
+
if (toGenerate.size > 0 || s.hasChanged()) {
|
|
800
|
+
code = s.toString();
|
|
801
|
+
map = s.generateMap({ hires: true, source: id });
|
|
802
|
+
} else {
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
if (alreadyHasStyles) {
|
|
806
|
+
return {
|
|
807
|
+
code: code.replace(/(<style[^>]*>)/, `$1${styles}`),
|
|
808
|
+
map
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
return {
|
|
812
|
+
code: `${code}
|
|
813
|
+
<style>${styles}</style>`,
|
|
814
|
+
map
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
function SvelteScopedPlugin({ ready, uno }) {
|
|
819
|
+
let viteConfig;
|
|
820
|
+
let filter = pluginutils.createFilter([/\.svelte$/], defaultExclude);
|
|
679
821
|
return {
|
|
680
822
|
name: "unocss:svelte-scoped",
|
|
681
823
|
enforce: "pre",
|
|
682
|
-
async configResolved() {
|
|
824
|
+
async configResolved(_viteConfig) {
|
|
825
|
+
viteConfig = _viteConfig;
|
|
683
826
|
const { config } = await ready;
|
|
684
827
|
filter = pluginutils.createFilter(
|
|
685
828
|
config.include || [/\.svelte$/],
|
|
@@ -689,14 +832,14 @@ function SvelteScopedPlugin({ uno, ready }) {
|
|
|
689
832
|
transform(code, id) {
|
|
690
833
|
if (!filter(id))
|
|
691
834
|
return;
|
|
692
|
-
return
|
|
835
|
+
return transformSvelteSFC(code, id, uno, { combine: viteConfig.command === "build" });
|
|
693
836
|
},
|
|
694
837
|
handleHotUpdate(ctx) {
|
|
695
838
|
const read = ctx.read;
|
|
696
839
|
if (filter(ctx.file)) {
|
|
697
840
|
ctx.read = async () => {
|
|
698
841
|
const code = await read();
|
|
699
|
-
return await
|
|
842
|
+
return (await transformSvelteSFC(code, ctx.file, uno, { combine: viteConfig.command === "build" }))?.code || code;
|
|
700
843
|
};
|
|
701
844
|
}
|
|
702
845
|
}
|
|
@@ -1047,3 +1190,4 @@ exports.SvelteScopedPlugin = SvelteScopedPlugin;
|
|
|
1047
1190
|
exports.VueScopedPlugin = VueScopedPlugin;
|
|
1048
1191
|
exports["default"] = UnocssPlugin;
|
|
1049
1192
|
exports.defineConfig = defineConfig;
|
|
1193
|
+
exports.transformSvelteSFC = transformSvelteSFC;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as vite from 'vite';
|
|
2
2
|
import { Plugin } from 'vite';
|
|
3
|
-
import { UserConfig, UnocssPluginContext, UserConfigDefaults } from '@unocss/core';
|
|
3
|
+
import { UserConfig, UnocssPluginContext, UnoGenerator, UserConfigDefaults } from '@unocss/core';
|
|
4
4
|
|
|
5
5
|
interface VitePluginConfig<Theme extends {} = {}> extends UserConfig<Theme> {
|
|
6
6
|
/**
|
|
@@ -49,9 +49,40 @@ declare function PerModuleModePlugin({ uno, filter }: UnocssPluginContext): Plug
|
|
|
49
49
|
|
|
50
50
|
declare function VueScopedPlugin({ uno, ready }: UnocssPluginContext): Plugin;
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
interface TransformSFCOptions {
|
|
53
|
+
/**
|
|
54
|
+
* Prefix for compiled class name
|
|
55
|
+
* @default 'uno-'
|
|
56
|
+
*/
|
|
57
|
+
classPrefix?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Add hash and combine recognized tokens (optimal for production); set false in dev mode for easy dev tools toggling to allow for design adjustments in the browser
|
|
60
|
+
* @default true
|
|
61
|
+
*/
|
|
62
|
+
combine?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Hash function
|
|
65
|
+
*/
|
|
66
|
+
hashFn?: (str: string) => string;
|
|
67
|
+
}
|
|
68
|
+
declare function transformSvelteSFC(code: string, id: string, uno: UnoGenerator, options?: TransformSFCOptions): Promise<{
|
|
69
|
+
code: string;
|
|
70
|
+
map?: SourceMap;
|
|
71
|
+
} | undefined>;
|
|
72
|
+
interface SourceMap {
|
|
73
|
+
file: string;
|
|
74
|
+
mappings: string;
|
|
75
|
+
names: string[];
|
|
76
|
+
sources: string[];
|
|
77
|
+
sourcesContent: string[];
|
|
78
|
+
version: number;
|
|
79
|
+
toString(): string;
|
|
80
|
+
toUrl(): string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
declare function SvelteScopedPlugin({ ready, uno }: UnocssPluginContext): Plugin;
|
|
53
84
|
|
|
54
85
|
declare function defineConfig<Theme extends {}>(config: VitePluginConfig<Theme>): VitePluginConfig<Theme>;
|
|
55
86
|
declare function UnocssPlugin<Theme extends {}>(configOrPath?: VitePluginConfig<Theme> | string, defaults?: UserConfigDefaults): Plugin[];
|
|
56
87
|
|
|
57
|
-
export { ChunkModeBuildPlugin, GlobalModeBuildPlugin, GlobalModeDevPlugin, GlobalModePlugin, PerModuleModePlugin, SvelteScopedPlugin, VitePluginConfig, VueScopedPlugin, UnocssPlugin as default, defineConfig };
|
|
88
|
+
export { ChunkModeBuildPlugin, GlobalModeBuildPlugin, GlobalModeDevPlugin, GlobalModePlugin, PerModuleModePlugin, SvelteScopedPlugin, TransformSFCOptions, VitePluginConfig, VueScopedPlugin, UnocssPlugin as default, defineConfig, transformSvelteSFC };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import UnocssInspector from '@unocss/inspector';
|
|
2
|
-
import { cssIdRE, createGenerator, BetterMap, notNull, toEscapedSelector } from '@unocss/core';
|
|
2
|
+
import { cssIdRE, createGenerator, BetterMap, notNull, expandVariantGroup, toEscapedSelector } from '@unocss/core';
|
|
3
3
|
import { createFilter } from '@rollup/pluginutils';
|
|
4
4
|
import { loadConfig } from '@unocss/config';
|
|
5
5
|
import { createHash } from 'crypto';
|
|
@@ -652,23 +652,166 @@ function VueScopedPlugin({ uno, ready }) {
|
|
|
652
652
|
};
|
|
653
653
|
}
|
|
654
654
|
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
655
|
+
const SELECTOR_REGEX = /(?<![\d(])([[\.][\S\s]+?)({[\S\s]+?})/g;
|
|
656
|
+
function wrapSelectorsWithGlobal(css) {
|
|
657
|
+
return css.replace(SELECTOR_REGEX, ":global($1)$2");
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function hash(str) {
|
|
661
|
+
let i;
|
|
662
|
+
let l;
|
|
663
|
+
let hval = 2166136261;
|
|
664
|
+
for (i = 0, l = str.length; i < l; i++) {
|
|
665
|
+
hval ^= str.charCodeAt(i);
|
|
666
|
+
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
|
|
667
|
+
}
|
|
668
|
+
return `00000${(hval >>> 0).toString(36)}`.slice(-6);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
const classesRE = /class=(["'\`])([\S\s]+?)\1/g;
|
|
672
|
+
const classesDirectivesRE = /class:([\S]+?)={/g;
|
|
673
|
+
const classDirectivesShorthandRE = /class:([^=>\s/]+)[{>\s/]/g;
|
|
674
|
+
const classesFromInlineConditionalsRE = /'([\S\s]+?)'/g;
|
|
675
|
+
async function transformSvelteSFC(code, id, uno, options = {}) {
|
|
676
|
+
const {
|
|
677
|
+
classPrefix = "uno-",
|
|
678
|
+
combine = true,
|
|
679
|
+
hashFn = hash
|
|
680
|
+
} = options;
|
|
681
|
+
let styles = "";
|
|
682
|
+
let map;
|
|
683
|
+
const alreadyHasStyles = code.match(/<style[^>]*>[\s\S]*?<\/style\s*>/);
|
|
684
|
+
const preflights = code.includes("uno:preflights");
|
|
685
|
+
const safelist = code.includes("uno:safelist");
|
|
686
|
+
if (preflights || safelist) {
|
|
687
|
+
const { css: css2 } = await uno.generate("", { preflights, safelist });
|
|
688
|
+
styles = css2;
|
|
689
|
+
}
|
|
690
|
+
const classes = [...code.matchAll(classesRE)];
|
|
691
|
+
const classDirectives = [...code.matchAll(classesDirectivesRE)];
|
|
692
|
+
const classDirectivesShorthand = [...code.matchAll(classDirectivesShorthandRE)];
|
|
693
|
+
if (!classes.length && !classDirectives.length && !classDirectivesShorthand.length) {
|
|
694
|
+
if (preflights || safelist) {
|
|
695
|
+
if (alreadyHasStyles) {
|
|
696
|
+
return {
|
|
697
|
+
code: code.replace(/(<style[^>]*>)/, `$1${styles}`)
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
return { code: `${code}
|
|
701
|
+
<style>${styles}</style>` };
|
|
702
|
+
} else {
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
const originalShortcuts = uno.config.shortcuts;
|
|
707
|
+
const shortcuts = {};
|
|
708
|
+
const toGenerate = /* @__PURE__ */ new Set();
|
|
709
|
+
const s = new MagicString(code);
|
|
710
|
+
let idHash;
|
|
711
|
+
if (!combine)
|
|
712
|
+
idHash = hashFn(id);
|
|
713
|
+
function isOriginalOriginalShortcut(token) {
|
|
714
|
+
return !!originalShortcuts.find((s2) => s2[0] === token);
|
|
715
|
+
}
|
|
716
|
+
function queueCompiledClass(tokens) {
|
|
717
|
+
if (combine) {
|
|
718
|
+
const _shortcuts = tokens.filter((t) => isOriginalOriginalShortcut(t));
|
|
719
|
+
for (const s2 of _shortcuts)
|
|
720
|
+
toGenerate.add(s2);
|
|
721
|
+
const _tokens = tokens.filter((t) => !isOriginalOriginalShortcut(t));
|
|
722
|
+
if (!_tokens.length)
|
|
723
|
+
return _shortcuts.join(" ");
|
|
724
|
+
const hash2 = hashFn(_tokens.join(" ") + id);
|
|
725
|
+
const className = `${classPrefix}${hash2}`;
|
|
726
|
+
shortcuts[className] = _tokens;
|
|
727
|
+
toGenerate.add(className);
|
|
728
|
+
return [className, ..._shortcuts].join(" ");
|
|
729
|
+
} else {
|
|
730
|
+
return tokens.map((token) => {
|
|
731
|
+
if (isOriginalOriginalShortcut(token)) {
|
|
732
|
+
toGenerate.add(token);
|
|
733
|
+
return token;
|
|
734
|
+
}
|
|
735
|
+
const className = `_${token}_${idHash}`;
|
|
736
|
+
shortcuts[className] = [token];
|
|
737
|
+
toGenerate.add(className);
|
|
738
|
+
return className;
|
|
739
|
+
}).join(" ");
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
async function sortKnownAndUnknownClasses(str) {
|
|
743
|
+
const classArr = str.split(/\s+/);
|
|
744
|
+
const result = await Promise.all(classArr.filter(Boolean).map(async (t) => [t, !!await uno.parseToken(t)]));
|
|
745
|
+
const known = result.filter(([, matched]) => matched).map(([t]) => t).sort();
|
|
746
|
+
if (!known.length)
|
|
662
747
|
return null;
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
return
|
|
666
|
-
<style>${css}</style>`;
|
|
748
|
+
const replacements = result.filter(([, matched]) => !matched).map(([i]) => i);
|
|
749
|
+
const className = queueCompiledClass(known);
|
|
750
|
+
return [className, ...replacements].join(" ");
|
|
667
751
|
}
|
|
752
|
+
for (const match of classes) {
|
|
753
|
+
let body = expandVariantGroup(match[2].trim());
|
|
754
|
+
const inlineConditionals = [...body.matchAll(classesFromInlineConditionalsRE)];
|
|
755
|
+
for (const conditional of inlineConditionals) {
|
|
756
|
+
const replacement2 = await sortKnownAndUnknownClasses(conditional[1].trim());
|
|
757
|
+
if (replacement2)
|
|
758
|
+
body = body.replace(conditional[0], `'${replacement2}'`);
|
|
759
|
+
}
|
|
760
|
+
const replacement = await sortKnownAndUnknownClasses(body);
|
|
761
|
+
if (replacement) {
|
|
762
|
+
const start = match.index;
|
|
763
|
+
s.overwrite(start + 7, start + match[0].length - 1, replacement);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
for (const match of classDirectives) {
|
|
767
|
+
const token = match[1];
|
|
768
|
+
const result = !!await uno.parseToken(token);
|
|
769
|
+
if (!result)
|
|
770
|
+
continue;
|
|
771
|
+
const className = queueCompiledClass([token]);
|
|
772
|
+
const start = match.index + "class:".length;
|
|
773
|
+
s.overwrite(start, start + match[1].length, className);
|
|
774
|
+
}
|
|
775
|
+
for (const match of classDirectivesShorthand) {
|
|
776
|
+
const token = match[1];
|
|
777
|
+
const result = !!await uno.parseToken(token);
|
|
778
|
+
if (!result)
|
|
779
|
+
continue;
|
|
780
|
+
const className = queueCompiledClass([token]);
|
|
781
|
+
const start = match.index + "class:".length;
|
|
782
|
+
s.overwrite(start, start + match[1].length, `${className}={${token}}`);
|
|
783
|
+
}
|
|
784
|
+
uno.config.shortcuts = [...originalShortcuts, ...Object.entries(shortcuts)];
|
|
785
|
+
const { css } = await uno.generate(toGenerate, { preflights: false, safelist: false, minify: true });
|
|
786
|
+
styles += wrapSelectorsWithGlobal(css);
|
|
787
|
+
uno.config.shortcuts = originalShortcuts;
|
|
788
|
+
if (toGenerate.size > 0 || s.hasChanged()) {
|
|
789
|
+
code = s.toString();
|
|
790
|
+
map = s.generateMap({ hires: true, source: id });
|
|
791
|
+
} else {
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
if (alreadyHasStyles) {
|
|
795
|
+
return {
|
|
796
|
+
code: code.replace(/(<style[^>]*>)/, `$1${styles}`),
|
|
797
|
+
map
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
return {
|
|
801
|
+
code: `${code}
|
|
802
|
+
<style>${styles}</style>`,
|
|
803
|
+
map
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
function SvelteScopedPlugin({ ready, uno }) {
|
|
808
|
+
let viteConfig;
|
|
809
|
+
let filter = createFilter([/\.svelte$/], defaultExclude);
|
|
668
810
|
return {
|
|
669
811
|
name: "unocss:svelte-scoped",
|
|
670
812
|
enforce: "pre",
|
|
671
|
-
async configResolved() {
|
|
813
|
+
async configResolved(_viteConfig) {
|
|
814
|
+
viteConfig = _viteConfig;
|
|
672
815
|
const { config } = await ready;
|
|
673
816
|
filter = createFilter(
|
|
674
817
|
config.include || [/\.svelte$/],
|
|
@@ -678,14 +821,14 @@ function SvelteScopedPlugin({ uno, ready }) {
|
|
|
678
821
|
transform(code, id) {
|
|
679
822
|
if (!filter(id))
|
|
680
823
|
return;
|
|
681
|
-
return
|
|
824
|
+
return transformSvelteSFC(code, id, uno, { combine: viteConfig.command === "build" });
|
|
682
825
|
},
|
|
683
826
|
handleHotUpdate(ctx) {
|
|
684
827
|
const read = ctx.read;
|
|
685
828
|
if (filter(ctx.file)) {
|
|
686
829
|
ctx.read = async () => {
|
|
687
830
|
const code = await read();
|
|
688
|
-
return await
|
|
831
|
+
return (await transformSvelteSFC(code, ctx.file, uno, { combine: viteConfig.command === "build" }))?.code || code;
|
|
689
832
|
};
|
|
690
833
|
}
|
|
691
834
|
}
|
|
@@ -1027,4 +1170,4 @@ function UnocssPlugin(configOrPath, defaults = {}) {
|
|
|
1027
1170
|
return plugins.filter(Boolean);
|
|
1028
1171
|
}
|
|
1029
1172
|
|
|
1030
|
-
export { ChunkModeBuildPlugin, GlobalModeBuildPlugin, GlobalModeDevPlugin, GlobalModePlugin, PerModuleModePlugin, SvelteScopedPlugin, VueScopedPlugin, UnocssPlugin as default, defineConfig };
|
|
1173
|
+
export { ChunkModeBuildPlugin, GlobalModeBuildPlugin, GlobalModeDevPlugin, GlobalModePlugin, PerModuleModePlugin, SvelteScopedPlugin, VueScopedPlugin, UnocssPlugin as default, defineConfig, transformSvelteSFC };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/vite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.0",
|
|
4
4
|
"description": "The Vite plugin for UnoCSS",
|
|
5
5
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,15 +44,15 @@
|
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@ampproject/remapping": "^2.2.0",
|
|
46
46
|
"@rollup/pluginutils": "^5.0.1",
|
|
47
|
-
"@unocss/config": "0.
|
|
48
|
-
"@unocss/core": "0.
|
|
49
|
-
"@unocss/inspector": "0.
|
|
50
|
-
"@unocss/scope": "0.
|
|
51
|
-
"@unocss/transformer-directives": "0.
|
|
47
|
+
"@unocss/config": "0.46.0",
|
|
48
|
+
"@unocss/core": "0.46.0",
|
|
49
|
+
"@unocss/inspector": "0.46.0",
|
|
50
|
+
"@unocss/scope": "0.46.0",
|
|
51
|
+
"@unocss/transformer-directives": "0.46.0",
|
|
52
52
|
"magic-string": "^0.26.7"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@unocss/shared-integration": "0.
|
|
55
|
+
"@unocss/shared-integration": "0.46.0",
|
|
56
56
|
"vite": "^3.1.8"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|