Skip to content

Commit d97fc47

Browse files
committed
release v2.0.0
2 parents 8ed4433 + d15e8c8 commit d97fc47

File tree

6 files changed

+69
-32
lines changed

6 files changed

+69
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ Interpretation mode will not modify your classes, it will only compile the origi
287287

288288
* States attribute: such as `sm md lg xl xxl focus hover dark ...` after applid media rules then also will be merged into the `class` attribute. (Attributes like `sm:hover` are not available because they may be rarely used and slow down the parsing speed.)
289289

290-
* Customize: Customize font or color, etc. (In development)
290+
* Customize: support `tailwind.config.js`.
291291

292292
* For more details, please refer to [windicss](https://github.com/voorjaar/windicss).
293293

example/svelte/rollup.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ export default {
4040
svelte({
4141
preprocess: {
4242
markup: require('../../src/index').preprocess({
43+
config: 'tailwind.config.js',
4344
compile: false,
45+
prefix: 'windi-',
4446
globalPreflight: true,
4547
globalUtility: true,
46-
prefix: 'windi-',
4748
})
4849
},
4950
compilerOptions: {

example/svelte/tailwind.config.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// tailwind.config.js
2+
const colors = require('windicss/colors')
3+
4+
module.exports = {
5+
theme: {
6+
screens: {
7+
sm: '480px',
8+
md: '768px',
9+
lg: '976px',
10+
xl: '1440px',
11+
},
12+
colors: {
13+
gray: colors.coolGray,
14+
blue: colors.lightBlue,
15+
red: colors.rose,
16+
pink: colors.fuchsia,
17+
},
18+
fontFamily: {
19+
sans: ['Graphik', 'sans-serif'],
20+
serif: ['Merriweather', 'serif'],
21+
},
22+
extend: {
23+
spacing: {
24+
'128': '32rem',
25+
'144': '36rem',
26+
},
27+
borderRadius: {
28+
'4xl': '2rem',
29+
}
30+
}
31+
}
32+
}

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-windicss-preprocess",
3-
"version": "1.0.9",
3+
"version": "2.0.0",
44
"description": "A Svelte Preprocessor to compile tailwindcss at build time based on windicss compiler.",
55
"main": "src/index.js",
66
"types": "src/index.d.ts",
@@ -24,7 +24,7 @@
2424
"dependencies": {
2525
"magic-string": "^0.25.7",
2626
"svelte": "^3.31.2",
27-
"windicss": "1.2.2"
27+
"windicss": "^2.0.6"
2828
},
2929
"devDependencies": {
3030
"@types/node": "^14.14.14",

src/index.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import MagicString from 'magic-string';
2-
import { variants } from 'windicss/src/processor/variants';
3-
42
import { walk, parse } from 'svelte/compiler';
5-
import { StyleSheet } from 'windicss/src/utils/style';
6-
import { compile, interpret, preflight } from 'windicss/src';
7-
import { CSSParser } from 'windicss/src/utils/css';
8-
import { optimizeBuild } from 'windicss/src/utils/algorithm';
9-
3+
import { StyleSheet } from 'windicss/utils/style';
4+
import { Processor } from 'windicss/lib';
5+
import { CSSParser } from 'windicss/utils/parser';
6+
import type { Config } from 'windicss/types/interfaces';
107
import type { TemplateNode } from 'svelte/types/compiler/interfaces'
118
import type { Preprocessor } from 'svelte/types/compiler/preprocess';
129

@@ -17,21 +14,27 @@ interface ChildNode {
1714
name?:string;
1815
}
1916

20-
const mode = process.env.NODE_ENV;
21-
const dev = mode === 'development';
22-
const variantKeys = [...Object.keys(variants), 'xxl'];
17+
const DEV = process.env.NODE_ENV === 'development';
2318

19+
let PROCESSOR:Processor;
20+
let VARIANTS:string[] = [];
2421
let IGNORED_CLASSES:string[] = [];
2522
let STYLESHEETS:StyleSheet[] = [];
2623
let DIRECTIVES:StyleSheet[] = [];
2724
let FILES:(string|undefined)[] = [];
2825

2926
let TAGNAMES:{[key:string]:string|undefined} = {};
30-
let OPTIONS = {
27+
let OPTIONS:{
28+
config?: Config | string,
29+
compile?: boolean,
30+
prefix?: string,
31+
globalPreflight?: boolean,
32+
globalUtility?: boolean,
33+
} = {
3134
compile: true,
32-
globalPreflight: true,
33-
globalUtility: true,
3435
prefix: 'windi-',
36+
globalPreflight: true,
37+
globalUtility: true,
3538
};
3639

3740
function combineStyleList(stylesheets:StyleSheet[]) {
@@ -49,14 +52,14 @@ function globalStyleSheet(styleSheet:StyleSheet) {
4952
}
5053

5154
function compilation(classNames:string) {
52-
const utility = compile(classNames, OPTIONS.prefix, false);
55+
const utility = PROCESSOR.compile(classNames, OPTIONS.prefix, false);
5356
IGNORED_CLASSES = [...IGNORED_CLASSES, ...utility.ignored];
5457
STYLESHEETS.push(OPTIONS.globalUtility?globalStyleSheet(utility.styleSheet):utility.styleSheet);
5558
return utility.className ? [utility.className, ...utility.ignored].join(' ') : utility.ignored.join(' '); // return new className
5659
}
5760

5861
function interpretation(classNames:string) {
59-
const utility = interpret(classNames);
62+
const utility = PROCESSOR.interpret(classNames);
6063
IGNORED_CLASSES = [...IGNORED_CLASSES, ...utility.ignored];
6164
let styleSheet = utility.styleSheet;
6265
STYLESHEETS.push(OPTIONS.globalUtility?globalStyleSheet(styleSheet):styleSheet);
@@ -85,7 +88,7 @@ const _preprocess:Preprocessor = ({content, filename}) => {
8588
if (style) {
8689
// handle tailwind directives ...
8790
style = style.replace(/<\/?style[^>]*>/g, '');
88-
STYLESHEETS.push(new CSSParser(style).parse(undefined, true));
91+
STYLESHEETS.push(new CSSParser(style, PROCESSOR).parse(undefined));
8992
content = content.replace(styleRegex, '');
9093
}
9194
const parsed = parse(content);
@@ -107,7 +110,7 @@ const _preprocess:Preprocessor = ({content, filename}) => {
107110
classStart = i.start;
108111
classes = [...classes, ...i.value.map(({data}:ChildNode)=>data)];
109112
code.overwrite(i.start, i.end, '');
110-
} else if (variantKeys.includes(i.name)) {
113+
} else if (VARIANTS.includes(i.name)) {
111114
// handle variant properties
112115
classStart = i.start;
113116
classes = [...classes, ...i.value.map(({data}:ChildNode)=>addVariant(data, i.name === 'xxl' ? '2xl' : i.name))];
@@ -127,15 +130,15 @@ const _preprocess:Preprocessor = ({content, filename}) => {
127130

128131
if (node.type === 'Class') {
129132
// handle class directive
130-
const utility = interpret(node.name);
133+
const utility = PROCESSOR.interpret(node.name);
131134
IGNORED_CLASSES = [...IGNORED_CLASSES, ...utility.ignored];
132135
DIRECTIVES.push(utility.styleSheet);
133136
// make sure directives add after all classes.
134137
}
135138

136139
if (node.type==="ConditionalExpression") {
137140
// handle inline conditional expression
138-
const utility = interpret(`${getReduceValue(node, 'consequent')} ${getReduceValue(node, 'alternate')}`);
141+
const utility = PROCESSOR.interpret(`${getReduceValue(node, 'consequent')} ${getReduceValue(node, 'alternate')}`);
139142
IGNORED_CLASSES = [...IGNORED_CLASSES, ...utility.ignored];
140143
DIRECTIVES.push(utility.styleSheet);
141144
}
@@ -151,13 +154,12 @@ const _preprocess:Preprocessor = ({content, filename}) => {
151154
};
152155

153156
// preflights might lost when refresh
154-
const preflights = preflight(dev?Object.keys(TAGNAMES):updatedTags, FILES.length === 0 || FILES.indexOf(filename) === 0); // only enable global styles for first file
157+
const preflights = PROCESSOR.preflight(DEV?Object.keys(TAGNAMES):updatedTags, FILES.length === 0 || FILES.indexOf(filename) === 0); // only enable global styles for first file
155158

156-
const outputCSS = optimizeBuild(
157-
(OPTIONS.globalPreflight? globalStyleSheet(preflights) : preflights)
159+
const outputCSS = (OPTIONS.globalPreflight? globalStyleSheet(preflights) : preflights)
158160
.extend(combineStyleList(STYLESHEETS))
159161
.extend(combineStyleList(DIRECTIVES))
160-
);
162+
.build();
161163

162164
code.trimEnd().append(`\n\n<style>\n${outputCSS}</style>`);
163165

@@ -170,7 +172,9 @@ const _preprocess:Preprocessor = ({content, filename}) => {
170172
return {code: code.toString()};
171173
}
172174

173-
export function preprocess(options={}) {
175+
export function preprocess(options: typeof OPTIONS = {}) {
174176
OPTIONS = {...OPTIONS, ...options}; // change global settings here;
177+
PROCESSOR = new Processor(OPTIONS.config);
178+
VARIANTS = [...Object.keys(PROCESSOR.resolveVariants()), 'xxl'];
175179
return _preprocess;
176180
}

0 commit comments

Comments
 (0)