Skip to content

Commit 6721228

Browse files
Add processCss callback option for esbuild plugin (#183)
Co-authored-by: mattcompiles <[email protected]>
1 parent a612428 commit 6721228

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

.changeset/curly-bats-allow.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
'@vanilla-extract/esbuild-plugin': minor
3+
---
4+
5+
Add `processCss` plugin option to allow further processing of CSS while bundling.
6+
7+
**Example for postcss with autoprefixer:**
8+
9+
```js
10+
const { vanillaExtractPlugin } = require('@vanilla-extract/esbuild-plugin');
11+
const postcss = require('postcss');
12+
const autoprefixer = require('autoprefixer');
13+
14+
require('esbuild').build({
15+
entryPoints: ['app.ts'],
16+
bundle: true,
17+
plugins: [vanillaExtractPlugin({
18+
19+
processCss: async (css) => {
20+
return (await postcss([autoprefixer])
21+
.process(css, { from: undefined /* suppress source map warning */ })
22+
).css
23+
}
24+
25+
})],
26+
outfile: 'out.js',
27+
}).catch(() => process.exit(1))
28+
```

packages/esbuild-plugin/src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ interface VanillaExtractPluginOptions {
1414
outputCss?: boolean;
1515
externals?: Array<string>;
1616
runtime?: boolean;
17+
processCss?: (css: string) => Promise<string>;
1718
}
1819
export function vanillaExtractPlugin({
1920
outputCss,
2021
externals = [],
2122
runtime = false,
23+
processCss,
2224
}: VanillaExtractPluginOptions = {}): Plugin {
2325
if (runtime) {
2426
// If using runtime CSS then just apply fileScopes to code
@@ -37,8 +39,12 @@ export function vanillaExtractPlugin({
3739

3840
build.onLoad(
3941
{ filter: /.*/, namespace: vanillaCssNamespace },
40-
({ path }) => {
41-
const { source } = getSourceFromVirtualCssFile(path);
42+
async ({ path }) => {
43+
let { source } = getSourceFromVirtualCssFile(path);
44+
45+
if (typeof processCss === 'function') {
46+
source = await processCss(source);
47+
}
4248

4349
return {
4450
contents: source,

0 commit comments

Comments
 (0)