Skip to content

Commit 352fc19

Browse files
authored
vite: Honor build.assetsInlineLimit (#1665)
1 parent 0927572 commit 352fc19

File tree

7 files changed

+100
-1
lines changed

7 files changed

+100
-1
lines changed

.changeset/gentle-goats-guess.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@vanilla-extract/vite-plugin': patch
3+
---
4+
5+
Assets used in CSS generated by the plugin will now respect the [`build.assetsInlineLimit`] Vite configuration option
6+
7+
[`build.assetsInlineLimit`]: https://vite.dev/config/build-options#build-assetsinlinelimit

.changeset/hungry-radios-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/compiler': patch
3+
---
4+
5+
Pass through `viteConfig.build.assetsInlineLimit` to the compiler's `vite` server

packages/compiler/src/compiler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ const createViteServer = async ({
123123
// Can be removed once https://github.com/vitejs/vite/pull/19247 is released.
124124
exclude: [/node_modules/],
125125
},
126+
assetsInlineLimit: viteConfig.build?.assetsInlineLimit,
126127
},
127128
ssr: {
128129
noExternal: true,

tests/compiler/compiler.test.ts

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ describe('compiler', () => {
2525
| 'vitePlugins'
2626
| 'tsconfigPaths'
2727
| 'basePath'
28-
| 'getAllCss',
28+
| 'getAllCss'
29+
| 'assetsInlineLimit'
30+
| 'assetsNoInlineLimit',
2931
ReturnType<typeof createCompiler>
3032
>;
3133

@@ -83,6 +85,22 @@ describe('compiler', () => {
8385
getAllCss: createCompiler({
8486
root: __dirname,
8587
}),
88+
assetsInlineLimit: createCompiler({
89+
root: __dirname,
90+
viteConfig: {
91+
build: {
92+
assetsInlineLimit: 512,
93+
},
94+
},
95+
}),
96+
assetsNoInlineLimit: createCompiler({
97+
root: __dirname,
98+
viteConfig: {
99+
build: {
100+
assetsInlineLimit: 0,
101+
},
102+
},
103+
}),
86104
};
87105
});
88106

@@ -583,4 +601,54 @@ describe('compiler', () => {
583601
584602
`);
585603
});
604+
605+
test('setting build.assetsInlineLimit = 0 should disable inlining', async () => {
606+
const compiler = compilers.assetsNoInlineLimit;
607+
608+
const cssPath = path.join(
609+
__dirname,
610+
'fixtures/assets-inline-limit/styles.css.ts',
611+
);
612+
const output = await compiler.processVanillaFile(cssPath);
613+
const { css } = compiler.getCssForFile(cssPath);
614+
615+
expect(output.source).toMatchInlineSnapshot(`
616+
import '{{__dirname}}/fixtures/assets-inline-limit/styles.css.ts.vanilla.css';
617+
export var square = 'styles_square__upl4cj0';
618+
`);
619+
620+
expect(css).toMatchInlineSnapshot(`
621+
.styles_square__upl4cj0 {
622+
width: 100px;
623+
height: 100px;
624+
background-image: url("/fixtures/assets-inline-limit/square.svg");
625+
background-size: cover;
626+
}
627+
`);
628+
});
629+
630+
test('setting build.assetsInlineLimit = 512 should inline assets appropriately', async () => {
631+
const compiler = compilers.assetsInlineLimit;
632+
633+
const cssPath = path.join(
634+
__dirname,
635+
'fixtures/assets-inline-limit/styles.css.ts',
636+
);
637+
const output = await compiler.processVanillaFile(cssPath);
638+
const { css } = compiler.getCssForFile(cssPath);
639+
640+
expect(output.source).toMatchInlineSnapshot(`
641+
import '{{__dirname}}/fixtures/assets-inline-limit/styles.css.ts.vanilla.css';
642+
export var square = 'styles_square__upl4cj0';
643+
`);
644+
645+
expect(css).toMatchInlineSnapshot(`
646+
.styles_square__upl4cj0 {
647+
width: 100px;
648+
height: 100px;
649+
background-image: url("data:image/svg+xml,%3c?xml%20version='1.0'%20encoding='UTF-8'?%3e%3c!--%20Uploaded%20to:%20SVG%20Repo,%20www.svgrepo.com,%20Generator:%20SVG%20Repo%20Mixer%20Tools%20--%3e%3csvg%20width='800px'%20height='800px'%20viewBox='0%200%2016%2016'%20fill='none'%20xmlns='http://www.w3.org/2000/svg'%3e%3crect%20x='1'%20y='1'%20width='14'%20height='14'%20fill='%23000000'/%3e%3c/svg%3e");
650+
background-size: cover;
651+
}
652+
`);
653+
});
586654
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module '*.svg' {
2+
let ImageSrc: string;
3+
export default ImageSrc;
4+
}
Lines changed: 5 additions & 0 deletions
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { style } from '@vanilla-extract/css';
2+
import asset from './square.svg';
3+
4+
export const square = style({
5+
width: '100px',
6+
height: '100px',
7+
backgroundImage: `url("${asset}")`,
8+
backgroundSize: 'cover',
9+
});

0 commit comments

Comments
 (0)