Skip to content

Commit e531c41

Browse files
authored
Use the new compiler in Vite plugin (#1264)
1 parent 9511311 commit e531c41

File tree

20 files changed

+596
-718
lines changed

20 files changed

+596
-718
lines changed

.changeset/integration-deps.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/integration': patch
3+
---
4+
5+
Update dependencies
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@vanilla-extract/integration': minor
3+
---
4+
5+
Export a `normalizePath` function that converts path separators to forward slashes
6+
7+
**Example usage**
8+
9+
```ts
10+
import { normalizePath } from '@vanilla-extract/integration';
11+
12+
normalizePath('foo\\bar'); // 'foo/bar'
13+
normalizePath('foo/bar'); // 'foo/bar'
14+
```

.changeset/vite-plugin-compiler.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@vanilla-extract/vite-plugin': major
3+
---
4+
5+
The Vite plugin now uses a newer, faster, Vite-based compiler by default.
6+
7+
The new compiler uses [`vite-node`](https://github.com/vitest-dev/vitest/tree/main/packages/vite-node) to parse and extract CSS from `.css.ts` files. This comes with all the benefits of using Vite, faster builds and the ability to use Vite plugins.
8+
9+
The new compiler has been used in Remix ever since support for Vanilla Extract was introduced.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@vanilla-extract/vite-plugin': major
3+
---
4+
5+
The behaviour previously known as `emitCssInSsr` has been turned on by default. The `emitCssInSsr` option has been removed.
6+
7+
This means that the CSS emitted by the plugin is now processed by Vite, so the plugin no longer needs to resolve PostCSS plugins and process the CSS output itself.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/vite-plugin': major
3+
---
4+
5+
The `esbuildOptions` option has been removed as we are no longer using esbuild internally
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/vite-plugin': major
3+
---
4+
5+
Drop support for Vite < 4

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@swc/core": "^1.2.112",
3838
"@testing-library/jest-dom": "^5.11.9",
3939
"@types/jest": "^29.2.5",
40+
"@types/node": "^20.9.5",
4041
"@types/testing-library__jest-dom": "^5.14.5",
4142
"@vanilla-extract/jest-transform": "*",
4243
"babel-jest": "^27.3.1",

packages/integration/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
"find-up": "^5.0.0",
2424
"javascript-stringify": "^2.0.1",
2525
"lodash": "^4.17.21",
26-
"mlly": "^1.1.0",
26+
"mlly": "^1.4.2",
2727
"outdent": "^0.8.0",
28-
"vite": "^5.0.10",
29-
"vite-node": "^1.1.0"
28+
"vite": "^5.0.11",
29+
"vite-node": "^1.2.0"
3030
},
3131
"devDependencies": {
3232
"@types/babel__core": "^7.1.20",

packages/integration/src/addFileScope.test.ts

Lines changed: 88 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { outdent } from 'outdent';
2-
import { addFileScope } from './addFileScope';
2+
import { sep, posix, win32 } from 'path';
3+
4+
import { addFileScope, normalizePath } from './addFileScope';
5+
6+
const raw = String.raw;
7+
8+
// remove quotes around the snapshot
9+
expect.addSnapshotSerializer({
10+
test: (val) => typeof val === 'string',
11+
print: (val) => (val as string).trim(),
12+
});
313

414
describe('ESM', () => {
515
test('should add missing fileScope', () => {
@@ -17,16 +27,12 @@ describe('ESM', () => {
1727
packageName: 'my-package',
1828
}),
1929
).toMatchInlineSnapshot(`
20-
"
21-
22-
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
23-
setFileScope("app/app.css.ts", "my-package");
24-
import {style} from '@vanilla-extract/css';
30+
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
31+
setFileScope("app/app.css.ts", "my-package");
32+
import {style} from '@vanilla-extract/css';
2533
2634
export const myStyle = style({});
27-
endFileScope();
28-
29-
"
35+
endFileScope();
3036
`);
3137
});
3238

@@ -46,19 +52,15 @@ describe('ESM', () => {
4652
globalAdapterIdentifier: 'MY_GLOBAL_ADAPTER',
4753
}),
4854
).toMatchInlineSnapshot(`
49-
"
50-
51-
import * as __vanilla_css_adapter__ from "@vanilla-extract/css/adapter";
52-
__vanilla_css_adapter__.setAdapter(MY_GLOBAL_ADAPTER);
53-
54-
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
55-
setFileScope("app/app.css.ts", "my-package");
56-
import {style} from '@vanilla-extract/css';
55+
import * as __vanilla_css_adapter__ from "@vanilla-extract/css/adapter";
56+
__vanilla_css_adapter__.setAdapter(MY_GLOBAL_ADAPTER);
57+
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
58+
setFileScope("app/app.css.ts", "my-package");
59+
import {style} from '@vanilla-extract/css';
5760
5861
export const myStyle = style({});
59-
endFileScope();
60-
__vanilla_css_adapter__.removeAdapter();
61-
"
62+
endFileScope();
63+
__vanilla_css_adapter__.removeAdapter();
6264
`);
6365
});
6466

@@ -80,12 +82,12 @@ describe('ESM', () => {
8082
packageName: 'my-package',
8183
}),
8284
).toMatchInlineSnapshot(`
83-
"import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
85+
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
8486
setFileScope("app/app.css.ts", "my-package");
8587
import {style} from '@vanilla-extract/css';
8688
8789
export const myStyle = style({});
88-
endFileScope();"
90+
endFileScope();
8991
`);
9092
});
9193

@@ -110,12 +112,12 @@ describe('ESM', () => {
110112
packageName: 'my-package',
111113
}),
112114
).toMatchInlineSnapshot(`
113-
"import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
115+
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
114116
setFileScope("app/app.css.ts", "my-package");
115117
import {style} from '@vanilla-extract/css';
116118
117119
export const myStyle = style({});
118-
endFileScope();"
120+
endFileScope();
119121
`);
120122
});
121123

@@ -137,13 +139,13 @@ describe('ESM', () => {
137139
packageName: 'my-package',
138140
}),
139141
).toMatchInlineSnapshot(`
140-
"import * as vanillaFileScope from "@vanilla-extract/css/fileScope";
141-
vanillaFileScope.setFileScope("app/app.css.ts", "my-package");
142-
import {style} from '@vanilla-extract/css';
142+
import * as vanillaFileScope from "@vanilla-extract/css/fileScope";
143+
vanillaFileScope.setFileScope("app/app.css.ts", "my-package");
144+
import {style} from '@vanilla-extract/css';
143145
144-
export const myStyle = style({});
145-
vanillaFileScope.endFileScope();"
146-
`);
146+
export const myStyle = style({});
147+
vanillaFileScope.endFileScope();
148+
`);
147149
});
148150
});
149151

@@ -164,17 +166,13 @@ describe('CJS', () => {
164166
packageName: 'my-package',
165167
}),
166168
).toMatchInlineSnapshot(`
167-
"
168-
169-
const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
170-
__vanilla_filescope__.setFileScope("app/app.css.ts", "my-package");
171-
const _css = require('@vanilla-extract/css');
169+
const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
170+
__vanilla_filescope__.setFileScope("app/app.css.ts", "my-package");
171+
const _css = require('@vanilla-extract/css');
172172
173173
var myStyle = _css.style({});
174174
exports.myStyle = myStyle;
175-
__vanilla_filescope__.endFileScope();
176-
;
177-
"
175+
__vanilla_filescope__.endFileScope();
178176
`);
179177
});
180178

@@ -195,20 +193,16 @@ describe('CJS', () => {
195193
globalAdapterIdentifier: 'MY_GLOBAL_ADAPTER',
196194
}),
197195
).toMatchInlineSnapshot(`
198-
"
199-
200-
const __vanilla_css_adapter__ = require("@vanilla-extract/css/adapter");
201-
__vanilla_css_adapter__.setAdapter(MY_GLOBAL_ADAPTER);
202-
203-
const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
204-
__vanilla_filescope__.setFileScope("app/app.css.ts", "my-package");
205-
const _css = require('@vanilla-extract/css');
196+
const __vanilla_css_adapter__ = require("@vanilla-extract/css/adapter");
197+
__vanilla_css_adapter__.setAdapter(MY_GLOBAL_ADAPTER);
198+
const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
199+
__vanilla_filescope__.setFileScope("app/app.css.ts", "my-package");
200+
const _css = require('@vanilla-extract/css');
206201
207202
var myStyle = _css.style({});
208203
exports.myStyle = myStyle;
209-
__vanilla_filescope__.endFileScope();
210-
__vanilla_css_adapter__.removeAdapter();;
211-
"
204+
__vanilla_filescope__.endFileScope();
205+
__vanilla_css_adapter__.removeAdapter();
212206
`);
213207
});
214208

@@ -231,13 +225,13 @@ describe('CJS', () => {
231225
packageName: 'my-package',
232226
}),
233227
).toMatchInlineSnapshot(`
234-
"const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
228+
const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
235229
__vanilla_filescope__.setFileScope("app/app.css.ts", "my-package");
236230
const _css = require('@vanilla-extract/css');
237231
238232
var myStyle = _css.style({});
239233
exports.myStyle = myStyle;
240-
__vanilla_filescope__.endFileScope();"
234+
__vanilla_filescope__.endFileScope();
241235
`);
242236
});
243237

@@ -263,13 +257,54 @@ describe('CJS', () => {
263257
packageName: 'my-package',
264258
}),
265259
).toMatchInlineSnapshot(`
266-
"const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
260+
const __vanilla_filescope__ = require("@vanilla-extract/css/fileScope");
267261
__vanilla_filescope__.setFileScope("app/app.css.ts", "my-package");
268262
const _css = require('@vanilla-extract/css');
269263
270264
const myStyle = _css.style({});
271265
exports.myStyle = myStyle;
272-
__vanilla_filescope__.endFileScope();"
266+
__vanilla_filescope__.endFileScope();
273267
`);
274268
});
275269
});
270+
271+
test('platform-specific relative path', () => {
272+
const { rootPath, filePath } = {
273+
[posix.sep]: {
274+
rootPath: '/the-root',
275+
filePath: '/the-root/app/app.css.ts',
276+
},
277+
[win32.sep]: {
278+
rootPath: raw`D:\the-root`,
279+
filePath: raw`D:\the-root\app\app.css.ts`,
280+
},
281+
}[sep];
282+
283+
const source = outdent`
284+
import { style } from '@vanilla-extract/css';
285+
286+
export const myStyle = style({});
287+
`;
288+
289+
// The snapshot should be the same for either platform
290+
expect(
291+
addFileScope({
292+
source,
293+
rootPath,
294+
filePath,
295+
packageName: 'my-package',
296+
}),
297+
).toMatchInlineSnapshot(`
298+
import { setFileScope, endFileScope } from "@vanilla-extract/css/fileScope";
299+
setFileScope("app/app.css.ts", "my-package");
300+
import { style } from '@vanilla-extract/css';
301+
302+
export const myStyle = style({});
303+
endFileScope();
304+
`);
305+
});
306+
307+
test('normalizePath()', () => {
308+
expect(normalizePath(raw`foo\bar`)).toMatchInlineSnapshot(`foo/bar`);
309+
expect(normalizePath(raw`foo/bar`)).toMatchInlineSnapshot(`foo/bar`);
310+
});

0 commit comments

Comments
 (0)