Skip to content

Commit a0f5a65

Browse files
antfudominikgkaisermann
authored
feat: use sveltePreprocess options from vite plugins if presents (#13)
* feat: use `sveltePreprocess` options from vite plugins if present solves windicss compatibility problem Co-authored-by: dominikg <dominik.goepel@gmx.de> Co-authored-by: Christian Kaisermann <christian@kaisermann.me>
1 parent d38c4f7 commit a0f5a65

File tree

9 files changed

+106
-93
lines changed

9 files changed

+106
-93
lines changed

.changeset/fresh-penguins-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
Allow other vite plugins to define preprocessors

.eslintrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ module.exports = {
143143
node: true,
144144
browser: false
145145
}
146+
},
147+
{
148+
/* required because App.svelte contains windicss rule that breaks the parser */
149+
files: ['packages/playground/windicss/src/App.svelte'],
150+
settings: {
151+
'svelte3/ignore-styles': () => true
152+
}
146153
}
147154
]
148155
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"name": "playground-windicss",
3-
"private": true,
3+
"private": true,
44
"version": "0.0.0",
55
"scripts": {
66
"dev": "vite",
77
"build": "vite build",
88
"serve": "vite preview"
99
},
1010
"dependencies": {
11-
"windicss": "^2.5.2"
11+
"windicss": "^2.5.4"
1212
},
1313
"devDependencies": {
1414
"@sveltejs/vite-plugin-svelte": "workspace:*",
1515
"svelte": "^3.35.0",
16-
"svelte-hmr": "^0.13.0",
16+
"svelte-hmr": "^0.14.0",
1717
"vite": "^2.1.2",
18-
"vite-plugin-windicss": "^0.9.4"
18+
"vite-plugin-windicss": "^0.9.9"
1919
}
2020
}

packages/playground/windicss/src/App.svelte

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,21 @@
99

1010
<style>
1111
h1 {
12-
@apply text-svelte-500 p-2 bg-green-200;
12+
@apply text-svelte-500 p-2 bg-green-200 hover:(bg-blue-400 text-white);
1313
}
14-
15-
/* works */
1614
@screen sm {
1715
h1 {
1816
background-color: rebeccapurple;
1917
}
2018
}
21-
22-
/* does not work */
23-
/*
24-
@screen sm {
25-
h1 {
26-
@apply text-blue-500;
27-
}
28-
}
29-
*/
30-
/* does not work either */
31-
/*
32-
@media (min-width: 640px) {
33-
h1 {
34-
@apply text-blue-500;
35-
}
36-
}
37-
*/
19+
@screen sm {
20+
h1 {
21+
@apply text-blue-500;
22+
}
23+
}
24+
@media (min-width: 640px) {
25+
h1 {
26+
@apply text-blue-500;
27+
}
28+
}
3829
</style>

packages/playground/windicss/vite.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ module.exports = defineConfig(({ command, mode }) => {
66
const isProduction = mode === 'production';
77
return {
88
plugins: [
9-
// uses enforce: pre
109
svelte(),
11-
vitePluginWindicss({
12-
transformCSS: 'pre'
13-
})
10+
vitePluginWindicss()
1411
],
1512
build: {
1613
minify: isProduction

packages/vite-plugin-svelte/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ It supports all options from rollup-plugin-svelte and some additional options to
3838

3939
For more Information check [options.ts](src/utils/options.ts)
4040

41+
## Integrations for other vite plugins
42+
43+
### Add an extra preprocessor
44+
45+
vite-plugin-svelte uses the svelte compiler to split `.svelte` files into js and css and the svelte compiler requires that the css passed to it is already plain css.
46+
If you are building a plugin for vite that transforms css and want it to work out of the box with vite-plugin-svelte, you can add a `sveltePreprocess: PreprocessorGroup` to your vite plugin definition and vite-plugin-svelte will pick it up and add it to the list of svelte preprocessors used at runtime.
47+
48+
```js
49+
const vitePluginCoolCss = {
50+
name: 'vite-plugin-coolcss',
51+
sveltePreprocess: {
52+
/* your PreprocessorGroup here */
53+
}
54+
/*... your cool css plugin implementation here .. */
55+
};
56+
```
57+
58+
Check out [windicss](https://github.com/windicss/vite-plugin-windicss/blob/517eca0cebc879d931c6578a08accadfb112157c/packages/vite-plugin-windicss/src/index.ts#L167)
59+
4160
## License
4261

4362
MIT

packages/vite-plugin-svelte/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { handleHotUpdate } from './handleHotUpdate';
88
import { log } from './utils/log';
99
import { createCompileSvelte } from './utils/compile';
1010
import { buildIdParser, IdParser } from './utils/id';
11-
import { validateInlineOptions, Options, ResolvedOptions, resolveOptions } from './utils/options';
11+
import { validateInlineOptions, Options, ResolvedOptions, resolveOptions, PreprocessorGroup } from './utils/options';
1212
import { VitePluginSvelteCache } from './utils/VitePluginSvelteCache';
1313

1414
import { SVELTE_IMPORTS, SVELTE_RESOLVE_MAIN_FIELDS } from './utils/contants';
@@ -25,6 +25,14 @@ export {
2525
Processed
2626
} from './utils/options';
2727

28+
// extend the Vite plugin interface to be able to have `sveltePreprocess` injection
29+
declare module 'vite' {
30+
// eslint-disable-next-line no-unused-vars
31+
interface Plugin {
32+
sveltePreprocess?: PreprocessorGroup
33+
}
34+
}
35+
2836
const pkg_export_errors = new Set();
2937

3038
export default function vitePluginSvelte(inlineOptions?: Partial<Options>): Plugin {
Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ResolvedConfig, TransformResult } from 'vite';
22
import { Preprocessor, PreprocessorGroup, ResolvedOptions } from './options';
33
import { TransformPluginContext } from 'rollup';
4-
// import type { WindiPluginUtils } from '@windicss/plugin-utils'
4+
import { log } from './log';
55
const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss'];
66

77
const supportedScriptLangs = ['ts'];
@@ -58,63 +58,24 @@ export function createVitePreprocessorGroup(
5858
} as PreprocessorGroup;
5959
}
6060

61-
/*
62-
function createWindicssStylePreprocessorFromVite(
63-
windiPlugin: Plugin
64-
): PreprocessorGroup {
65-
const cssTransform = windiPlugin.transform!.bind(
66-
(null as unknown) as TransformPluginContext
67-
)
68-
return {
69-
style: async ({attributes,content, filename }) => {
70-
const lang = attributes.lang as string || 'css'
71-
const transformResult: string = (await cssTransform(
72-
content,
73-
`${filename}.${lang}`
74-
)) as unknown as string
75-
76-
77-
return {
78-
code: transformResult
79-
}
80-
}
81-
} as PreprocessorGroup
82-
}
83-
84-
85-
86-
function createWindicssApiStylePreprocessorFromVite(
87-
windiPlugin: Plugin
88-
): PreprocessorGroup {
89-
const windiAPI = windiPlugin.api as WindiPluginUtils
90-
91-
return {
92-
style: async ({ content, filename }) => {
93-
windiAPI.extractFile(content,filename,false);
94-
const transformResult = await windiAPI.transformGroupsWithSourcemap(content)
95-
if(transformResult) {
96-
return {
97-
code: transformResult.code,
98-
map: transformResult.map as object
99-
}
100-
}
101-
}
102-
} as PreprocessorGroup
103-
}
104-
105-
*/
106-
10761
export function buildExtraPreprocessors(options: ResolvedOptions, config: ResolvedConfig) {
10862
const extraPreprocessors = [];
10963
if (options.useVitePreprocess) {
64+
log.debug('adding vite preprocessor');
11065
extraPreprocessors.push(createVitePreprocessorGroup(config, options));
11166
}
112-
// TODO
113-
/*
114-
const windiCssPlugin = config.plugins.find(p => p.name === 'vite-plugin-windicss:css');
115-
if (windiCssPlugin) {
116-
extraPreprocessors.unshift(createWindicssStylePreprocessorFromVite(windiCssPlugin))
117-
}
118-
*/
67+
68+
const pluginsWithPreprocessors = config.plugins.filter((p) => p?.sveltePreprocess);
69+
if (pluginsWithPreprocessors.length > 0) {
70+
log.debug(
71+
`adding preprocessors from other vite plugins: ${pluginsWithPreprocessors
72+
.map((p) => p.name)
73+
.join(', ')}`
74+
);
75+
extraPreprocessors.push(
76+
...pluginsWithPreprocessors.map((p) => p.sveltePreprocess as PreprocessorGroup)
77+
);
78+
}
79+
11980
return extraPreprocessors;
12081
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)