Skip to content

Commit e373b51

Browse files
Add esbuild configuration (#668)
Co-authored-by: mattcompiles <[email protected]>
1 parent a10aaca commit e373b51

File tree

9 files changed

+82
-8
lines changed

9 files changed

+82
-8
lines changed

.changeset/clever-ghosts-roll.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@vanilla-extract/esbuild-plugin": minor
3+
"@vanilla-extract/integration": major
4+
"@vanilla-extract/vite-plugin": minor
5+
"@vanilla-extract/rollup-plugin": minor
6+
---
7+
8+
Add esbuild configurations to vite/esbuild/rollup plugins

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,13 @@ Different formatting of identifiers (e.g. class names, keyframes, CSS Vars, etc)
393393

394394
Each integration will set a default value based on the configuration options passed to the bundler.
395395

396+
### esbuildOptions
397+
> Only for `esbuild`, `vite` and `rollup` plugins
398+
399+
The `vite`, `esbuild` and `rollup` plugins use esbuild internally to compile `.css.ts` files before running them to extract styles. You can pass additional options here to customize that process.
400+
401+
Accepts a subset of esbuild build options (`plugins`, `external`, `define` and `loader`), see https://esbuild.github.io/api/#build-api. The options are deep merged with the default options provided by Vanilla Extract.
402+
396403
---
397404

398405
## Styling API

packages/esbuild-plugin/src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,31 @@ import {
99
vanillaExtractFilescopePlugin,
1010
IdentifierOption,
1111
} from '@vanilla-extract/integration';
12-
import type { Plugin } from 'esbuild';
12+
import type { Plugin, BuildOptions as EsbuildOptions } from 'esbuild';
1313

1414
const vanillaCssNamespace = 'vanilla-extract-css-ns';
1515

1616
interface VanillaExtractPluginOptions {
1717
outputCss?: boolean;
18+
/**
19+
* @deprecated Use `esbuildOptions.external` instead.
20+
*/
1821
externals?: Array<string>;
1922
runtime?: boolean;
2023
processCss?: (css: string) => Promise<string>;
2124
identifiers?: IdentifierOption;
25+
esbuildOptions?: Pick<
26+
EsbuildOptions,
27+
'plugins' | 'external' | 'define' | 'loader'
28+
>;
2229
}
2330
export function vanillaExtractPlugin({
2431
outputCss,
2532
externals = [],
2633
runtime = false,
2734
processCss,
2835
identifiers,
36+
esbuildOptions,
2937
}: VanillaExtractPluginOptions = {}): Plugin {
3038
if (runtime) {
3139
// If using runtime CSS then just apply fileScopes to code
@@ -64,10 +72,22 @@ export function vanillaExtractPlugin({
6472
);
6573

6674
build.onLoad({ filter: cssFileFilter }, async ({ path }) => {
75+
const combinedEsbuildOptions = { ...esbuildOptions } ?? {};
76+
77+
// To avoid a breaking change this combines the `external` option from
78+
// esbuildOptions with the pre-existing externals option.
79+
if (externals) {
80+
if (combinedEsbuildOptions.external) {
81+
combinedEsbuildOptions.external.push(...externals);
82+
} else {
83+
combinedEsbuildOptions.external = externals;
84+
}
85+
}
86+
6787
const { source, watchFiles } = await compile({
6888
filePath: path,
69-
externals,
7089
cwd: build.initialOptions.absWorkingDir,
90+
esbuildOptions: combinedEsbuildOptions,
7191
});
7292

7393
const contents = await processVanillaFile({

packages/integration/src/compile.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { dirname, join } from 'path';
22
import { promises as fs } from 'fs';
33

4-
import { build as esbuild, Plugin } from 'esbuild';
4+
import {
5+
build as esbuild,
6+
Plugin,
7+
BuildOptions as EsbuildOptions,
8+
} from 'esbuild';
59

610
import { cssFileFilter } from './filters';
711
import { addFileScope } from './addFileScope';
@@ -34,22 +38,30 @@ export const vanillaExtractFilescopePlugin = (): Plugin => ({
3438
interface CompileOptions {
3539
filePath: string;
3640
cwd?: string;
37-
externals?: Array<string>;
41+
esbuildOptions?: Pick<
42+
EsbuildOptions,
43+
'plugins' | 'external' | 'define' | 'loader'
44+
>;
3845
}
3946
export async function compile({
4047
filePath,
4148
cwd = process.cwd(),
42-
externals = [],
49+
esbuildOptions,
4350
}: CompileOptions) {
4451
const result = await esbuild({
4552
entryPoints: [filePath],
4653
metafile: true,
4754
bundle: true,
48-
external: ['@vanilla-extract', ...externals],
55+
external: ['@vanilla-extract', ...(esbuildOptions?.external ?? [])],
4956
platform: 'node',
5057
write: false,
51-
plugins: [vanillaExtractFilescopePlugin()],
58+
plugins: [
59+
vanillaExtractFilescopePlugin(),
60+
...(esbuildOptions?.plugins ?? []),
61+
],
5262
absWorkingDir: cwd,
63+
loader: esbuildOptions?.loader,
64+
define: esbuildOptions?.define,
5365
});
5466

5567
const { outputFiles, metafile } = result;

packages/rollup-plugin/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ import {
77
getSourceFromVirtualCssFile,
88
virtualCssFileFilter,
99
} from '@vanilla-extract/integration';
10+
import { BuildOptions as EsbuildOptions } from 'esbuild';
1011
import { posix } from 'path';
1112

1213
const { relative, normalize, dirname } = posix;
1314

1415
interface Options {
1516
identifiers?: IdentifierOption;
1617
cwd?: string;
18+
esbuildOptions?: Pick<
19+
EsbuildOptions,
20+
'plugins' | 'external' | 'define' | 'loader'
21+
>;
1722
}
1823
export function vanillaExtractPlugin({
1924
identifiers,
2025
cwd = process.cwd(),
26+
esbuildOptions,
2127
}: Options = {}): Plugin {
2228
const emittedFiles = new Map<string, string>();
2329
const isProduction = process.env.NODE_ENV === 'production';
@@ -38,6 +44,7 @@ export function vanillaExtractPlugin({
3844
const { source, watchFiles } = await compile({
3945
filePath,
4046
cwd,
47+
esbuildOptions,
4148
});
4249

4350
for (const file of watchFiles) {

packages/vite-plugin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"postcss-load-config": "^3.1.0"
2222
},
2323
"devDependencies": {
24+
"esbuild": "^0.11.16",
2425
"vite": "^2.7.0"
2526
},
2627
"peerDependencies": {

packages/vite-plugin/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ import {
1212
getPackageInfo,
1313
} from '@vanilla-extract/integration';
1414
import { PostCSSConfigResult, resolvePostcssConfig } from './postcss';
15+
import { BuildOptions as EsbuildOptions } from 'esbuild';
1516

1617
const styleUpdateEvent = (fileId: string) =>
1718
`vanilla-extract-style-update:${fileId}`;
1819

1920
interface Options {
2021
identifiers?: IdentifierOption;
22+
esbuildOptions?: Pick<
23+
EsbuildOptions,
24+
'plugins' | 'external' | 'define' | 'loader'
25+
>;
2126
}
22-
export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
27+
export function vanillaExtractPlugin({
28+
identifiers,
29+
esbuildOptions,
30+
}: Options = {}): Plugin {
2331
let config: ResolvedConfig;
2432
let server: ViteDevServer;
2533
let postCssConfig: PostCSSConfigResult | null;
@@ -138,6 +146,7 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
138146
const { source, watchFiles } = await compile({
139147
filePath: validId,
140148
cwd: config.root,
149+
esbuildOptions,
141150
});
142151

143152
for (const file of watchFiles) {

pnpm-lock.yaml

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

site/docs/setup.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,11 @@ Different formatting of identifiers (e.g. class names, keyframes, CSS Vars, etc)
306306
- `debug` identifiers contain human readable prefixes representing the owning filename and a potential rule level debug name. e.g. `myfile_mystyle_hnw5tz3`
307307
308308
Each integration will set a default value based on the configuration options passed to the bundler.
309+
310+
### esbuildOptions
311+
312+
> Only for `esbuild`, `vite` and `rollup` plugins
313+
314+
The `vite`, `esbuild` and `rollup` plugins use esbuild internally to compile `.css.ts` files before running them to extract styles. You can pass additional options here to customize that process.
315+
316+
Accepts a subset of esbuild build options (`plugins`, `external`, `define` and `loader`), see https://esbuild.github.io/api/#build-api. The options are deep merged with the default options provided by Vanilla Extract.

0 commit comments

Comments
 (0)