Skip to content

Commit a6383e8

Browse files
Vite: Mark .vanilla.js and .vanilla.css modules as virtual. (#470)
1 parent ae1090d commit a6383e8

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

.changeset/calm-ravens-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/vite-plugin': patch
3+
---
4+
5+
Prefix `.vanilla.js` and `.vanilla.css` virtual modules with `virtual:vanilla-extract:`.

packages/vite-plugin/src/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
const styleUpdateEvent = (fileId: string) =>
1717
`vanilla-extract-style-update:${fileId}`;
1818

19+
const virtualPrefix = 'virtual:vanilla-extract:';
20+
1921
interface Options {
2022
identifiers?: IdentifierOption;
2123
}
@@ -56,15 +58,16 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
5658
packageInfo = getPackageInfo(config.root);
5759
},
5860
resolveId(id) {
59-
if (id.indexOf(virtualExt) > 0) {
61+
if (id.indexOf(virtualPrefix) === 0) {
6062
return id;
6163
}
6264
},
6365
load(id) {
64-
const extensionIndex = id.indexOf(virtualExt);
65-
66-
if (extensionIndex > 0) {
67-
const fileScopeId = id.substring(0, extensionIndex);
66+
if (id.indexOf(virtualPrefix) === 0) {
67+
const fileScopeId = id.slice(
68+
virtualPrefix.length,
69+
id.indexOf(virtualExt),
70+
);
6871

6972
if (!cssMap.has(fileScopeId)) {
7073
throw new Error(`Unable to locate ${fileScopeId} in the CSS map.`);
@@ -132,7 +135,7 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
132135
identifiers ?? (config.mode === 'production' ? 'short' : 'debug'),
133136
serializeVirtualCssPath: ({ fileScope, source }) => {
134137
const fileId = stringifyFileScope(fileScope);
135-
const id = `${fileId}${virtualExt}`;
138+
const id = `${virtualPrefix}${fileId}${virtualExt}`;
136139

137140
if (server && cssMap.has(fileId) && cssMap.get(fileId) !== source) {
138141
const { moduleGraph } = server;

0 commit comments

Comments
 (0)