Skip to content

Commit a5efad1

Browse files
esbuild-plugin: Document processCss option (#185)
Co-authored-by: Michael Taranto <[email protected]>
1 parent 6721228 commit a5efad1

File tree

3 files changed

+102
-18
lines changed

3 files changed

+102
-18
lines changed

.changeset/curly-bats-allow.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,28 @@ Add `processCss` plugin option to allow further processing of CSS while bundling
77
**Example for postcss with autoprefixer:**
88

99
```js
10-
const { vanillaExtractPlugin } = require('@vanilla-extract/esbuild-plugin');
11-
const postcss = require('postcss');
12-
const autoprefixer = require('autoprefixer');
10+
const { vanillaExtractPlugin } = require("@vanilla-extract/esbuild-plugin");
11+
const postcss = require("postcss");
12+
const autoprefixer = require("autoprefixer");
1313

14-
require('esbuild').build({
15-
entryPoints: ['app.ts'],
16-
bundle: true,
17-
plugins: [vanillaExtractPlugin({
14+
async function processCss(css) {
15+
const result = await postcss([autoprefixer]).process(css, {
16+
from: undefined /* suppress source map warning */,
17+
});
1818

19-
processCss: async (css) => {
20-
return (await postcss([autoprefixer])
21-
.process(css, { from: undefined /* suppress source map warning */ })
22-
).css
23-
}
19+
return result.css;
20+
}
2421

25-
})],
26-
outfile: 'out.js',
27-
}).catch(() => process.exit(1))
22+
require("esbuild")
23+
.build({
24+
entryPoints: ["app.ts"],
25+
bundle: true,
26+
plugins: [
27+
vanillaExtractPlugin({
28+
processCss,
29+
}),
30+
],
31+
outfile: "out.js",
32+
})
33+
.catch(() => process.exit(1));
2834
```

README.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,44 @@ require('esbuild').build({
193193

194194
> Please note: There are currently no automatic readable class names during development. However, you can still manually provide a debug ID as the last argument to functions that generate scoped styles, e.g. `export const className = style({ ... }, 'className');`
195195
196+
3. Process CSS
197+
198+
As [esbuild](https://esbuild.github.io/) currently doesn't have a way to process the CSS generated by vanilla-extract, you can optionally use the `processCss` option.
199+
200+
For example, to run autoprefixer over the generated CSS.
201+
202+
```js
203+
const {
204+
vanillaExtractPlugin
205+
} = require('@vanilla-extract/esbuild-plugin');
206+
const postcss = require('postcss');
207+
const autoprefixer = require('autoprefixer');
208+
209+
async function processCss(css) {
210+
const result = await postcss([autoprefixer]).process(
211+
css,
212+
{
213+
from: undefined /* suppress source map warning */
214+
}
215+
);
216+
217+
return result.css;
218+
}
219+
220+
require('esbuild')
221+
.build({
222+
entryPoints: ['app.ts'],
223+
bundle: true,
224+
plugins: [
225+
vanillaExtractPlugin({
226+
processCss
227+
})
228+
],
229+
outfile: 'out.js'
230+
})
231+
.catch(() => process.exit(1));
232+
```
233+
196234
### Vite
197235

198236
> Warning: Currently the Vite plugin doesn't rebuild files when dependent files change, e.g. updating `theme.css.ts` should rebuild `styles.css.ts` which imports `theme.css.ts`. This is a limitation in the Vite Plugin API that will hopefully be resolved soon. You can track the Vite issue here: https://github.com/vitejs/vite/issues/3216
@@ -216,15 +254,15 @@ export default {
216254

217255
> Please note: There are currently no automatic readable class names during development. However, you can still manually provide a debug ID as the last argument to functions that generate scoped styles, e.g. `export const className = style({ ... }, 'className');`
218256
219-
## Snowpack
257+
### Snowpack
220258

221-
1/ Install the dependencies.
259+
1. Install the dependencies.
222260

223261
```bash
224262
$ npm install @vanilla-extract/css @vanilla-extract/snowpack-plugin
225263
```
226264

227-
2/ Add the [Snowpack](https://www.snowpack.dev/) plugin to your snowpack config.
265+
2. Add the [Snowpack](https://www.snowpack.dev/) plugin to your snowpack config.
228266

229267
```js
230268
// snowpack.config.json

site/docs/setup.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,46 @@ require('esbuild')
9393

9494
> Please note: There are currently no automatic readable class names during development. However, you can still manually provide a debug ID as the last argument to functions that generate scoped styles, e.g. `export const className = style({ ... }, 'className');`
9595
96+
3/ Process CSS
97+
98+
As [esbuild](https://esbuild.github.io/) currently doesn't have a way to process the CSS generated by vanilla-extract, you can optionally use the `processCss` option.
99+
100+
For example, to run autoprefixer over the generated CSS.
101+
102+
```js
103+
// bundle.js
104+
105+
const {
106+
vanillaExtractPlugin
107+
} = require('@vanilla-extract/esbuild-plugin');
108+
const postcss = require('postcss');
109+
const autoprefixer = require('autoprefixer');
110+
111+
async function processCss(css) {
112+
const result = await postcss([autoprefixer]).process(
113+
css,
114+
{
115+
from: undefined /* suppress source map warning */
116+
}
117+
);
118+
119+
return result.css;
120+
}
121+
122+
require('esbuild')
123+
.build({
124+
entryPoints: ['app.ts'],
125+
bundle: true,
126+
plugins: [
127+
vanillaExtractPlugin({
128+
processCss
129+
})
130+
],
131+
outfile: 'out.js'
132+
})
133+
.catch(() => process.exit(1));
134+
```
135+
96136
## Vite
97137

98138
> Warning: Currently the Vite plugin doesn't rebuild files when dependent files change, e.g. updating `theme.css.ts` should rebuild `styles.css.ts` which imports `theme.css.ts`. This is a [limitation in the Vite Plugin API](https://github.com/vitejs/vite/issues/3216) that will hopefully be resolved soon.

0 commit comments

Comments
 (0)