Skip to content

Commit 6488e28

Browse files
authored
vite: Fix incorrect bundling of imported assets when base is set in vite config (#1583)
1 parent 9241939 commit 6488e28

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

.changeset/large-glasses-travel.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@vanilla-extract/compiler': patch
3+
'@vanilla-extract/vite-plugin': patch
4+
---
5+
6+
Ignore Vite `base` config
7+
8+
Fixes a bug where imported asset URLs would be transformed by the compiler when setting the `base` option in Vite, resulting in incorrect resolution and bundling of these assets during the build process.

packages/compiler/src/compiler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ const createViteServer = async ({
101101

102102
const server = await vite.createServer({
103103
...viteConfig,
104+
// The vite-node server should not rewrite imported asset URLs within VE stylesheets.
105+
// Doing so interferes with Vite's resolution and bundling of these assets at build time.
106+
base: undefined,
104107
configFile: false,
105108
root,
106109
server: {

tests/compiler/compiler.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ describe('compiler', () => {
2323
| 'shortIdentifiers'
2424
| 'viteResolve'
2525
| 'vitePlugins'
26-
| 'tsconfigPaths',
26+
| 'tsconfigPaths'
27+
| 'basePath',
2728
ReturnType<typeof createCompiler>
2829
>;
2930

@@ -72,6 +73,12 @@ describe('compiler', () => {
7273
root: __dirname,
7374
vitePlugins: [tsconfigPaths()],
7475
}),
76+
basePath: createCompiler({
77+
root: __dirname,
78+
viteConfig: {
79+
base: '/assets/',
80+
},
81+
}),
7582
};
7683
});
7784

@@ -521,4 +528,27 @@ describe('compiler', () => {
521528
}
522529
`);
523530
});
531+
532+
test('base path should be ignored', async () => {
533+
const compiler = compilers.basePath;
534+
535+
const cssPath = path.join(__dirname, 'fixtures/vite-config/image.css.ts');
536+
const output = await compiler.processVanillaFile(cssPath);
537+
const { css } = compiler.getCssForFile(cssPath);
538+
539+
expect(output.source).toMatchInlineSnapshot(`
540+
import '{{__dirname}}/fixtures/vite-config/image.css.ts.vanilla.css';
541+
export var imageStyle1 = 'image_imageStyle1__1lseqzh0';
542+
export var imageStyle2 = 'image_imageStyle2__1lseqzh1';
543+
`);
544+
545+
expect(css).toMatchInlineSnapshot(`
546+
.image_imageStyle1__1lseqzh0 {
547+
background-image: url('/fixtures/vite-config/test.jpg');
548+
}
549+
.image_imageStyle2__1lseqzh1 {
550+
background-image: url('/fixtures/vite-config/test.jpg');
551+
}
552+
`);
553+
});
524554
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { style } from '@vanilla-extract/css';
2+
// @ts-expect-error No type definition for image assets
3+
import testImage from './test.jpg';
4+
5+
export const imageStyle1 = style({
6+
backgroundImage: `url('${testImage}')`,
7+
});
8+
9+
export const imageStyle2 = style({
10+
backgroundImage: `url('/fixtures/vite-config/test.jpg')`,
11+
});
21.1 KB
Loading

0 commit comments

Comments
 (0)