Skip to content

Commit e8a1084

Browse files
authored
Force Vite plugin to use one renderer (#58)
1 parent 81f962c commit e8a1084

File tree

4 files changed

+55
-24
lines changed

4 files changed

+55
-24
lines changed

.changeset/thirty-mayflies-juggle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@navita/vite-plugin': patch
3+
---
4+
5+
Force the vite-plugin to use one renderer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { style } from "@navita/css";
2+
3+
const container = style({
4+
width: 500,
5+
height: 500,
6+
background: 'green',
7+
8+
'@media (min-width: 600px)': {
9+
background: 'red',
10+
},
11+
});
12+
13+
export const Specificity = () => (
14+
<div className={container} />
15+
);

examples/with-remix/app/routes/_index.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { MetaFunction } from "@remix-run/node";
21
import { style } from "@navita/css";
3-
import { background } from "~/consts";
4-
import { Button } from "~/components/button";
2+
import type { MetaFunction } from "@remix-run/node";
53
import { Box } from "~/components/box";
4+
import { Button } from "~/components/button";
5+
import { Specificity } from "~/components/specificity";
6+
import { background } from "~/consts";
67

78
export const meta: MetaFunction = () => {
89
return [
@@ -17,9 +18,12 @@ const x = style({
1718
});
1819

1920
export default function Index() {
21+
22+
2023
return (
2124
<div className={x}>
2225
Testing
26+
<Specificity />
2327
<Button>Testing</Button>
2428

2529
<Box>Hello</Box>

packages/vite-plugin/src/index.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { createRenderer } from "@navita/core/createRenderer";
44
import { importMap as defaultImportMap } from "@navita/css";
55
import type { Plugin, ViteDevServer } from "vite";
66

7-
let renderer: Renderer;
8-
97
/*
108
Some information for anyone wondering why we have duplicate css for the initial load
119
during development in remix.
1210
https://github.com/remix-run/remix/discussions/8070#discussioncomment-7625870
1311
*/
1412

15-
const VIRTUAL_CSS_NAME = '\0virtual:navita.css';
13+
let renderer: Renderer;
14+
15+
const VIRTUAL_MODULE_ID = 'virtual:navita.css';
16+
const RESOLVED_VIRTUAL_MODULE_ID = '\0' + VIRTUAL_MODULE_ID;
1617

1718
interface Options {
1819
importMap?: ImportMap;
@@ -26,12 +27,18 @@ export function navita(options?: Options) {
2627
let lastCssContent: string | undefined;
2728
let context: string;
2829
let isSSR = false;
30+
let isDEV = true;
2931

3032
return {
31-
name: "navita",
3233
enforce: "pre",
33-
config() {
34+
name: "navita",
35+
config(_, env) {
36+
isDEV = env.command === 'serve';
37+
3438
return {
39+
optimizeDeps: {
40+
include: isDEV ? ['@navita/css'] : [],
41+
},
3542
ssr: {
3643
external: [
3744
'@navita/css',
@@ -49,9 +56,13 @@ export function navita(options?: Options) {
4956
server = _server;
5057
},
5158
async buildStart() {
59+
if (renderer) {
60+
return;
61+
}
62+
5263
const defaultEngineOptions = {
53-
enableSourceMaps: !!server,
54-
enableDebugIdentifiers: !!server,
64+
enableSourceMaps: isDEV,
65+
enableDebugIdentifiers: isDEV,
5566
...(options?.engineOptions || {}),
5667
};
5768

@@ -68,16 +79,14 @@ export function navita(options?: Options) {
6879
},
6980
});
7081
},
71-
async resolveId(id) {
72-
if (id === VIRTUAL_CSS_NAME) {
73-
return VIRTUAL_CSS_NAME;
82+
resolveId(source) {
83+
if (source === VIRTUAL_MODULE_ID) {
84+
return RESOLVED_VIRTUAL_MODULE_ID;
7485
}
75-
76-
return;
7786
},
78-
async load(id) {
79-
if (id === VIRTUAL_CSS_NAME) {
80-
return lastCssContent;
87+
async load(source) {
88+
if (source === RESOLVED_VIRTUAL_MODULE_ID) {
89+
return renderer.engine.renderCssToString();
8190
}
8291

8392
return;
@@ -97,9 +106,7 @@ export function navita(options?: Options) {
97106
const newCssContent = renderer.engine.renderCssToString();
98107

99108
if (lastCssContent !== newCssContent) {
100-
invalidateModule(VIRTUAL_CSS_NAME);
101-
this.addWatchFile(VIRTUAL_CSS_NAME);
102-
109+
invalidateModule(RESOLVED_VIRTUAL_MODULE_ID);
103110
lastCssContent = newCssContent;
104111

105112
for (const file of dependencies) {
@@ -112,17 +119,17 @@ export function navita(options?: Options) {
112119
}
113120

114121
return {
115-
code: `${result} import "${VIRTUAL_CSS_NAME}";`,
122+
code: `${result}\nimport "${VIRTUAL_MODULE_ID}";`,
116123
map: sourceMap,
117124
};
118125
},
119-
renderChunk(_, chunk, third) {
126+
renderChunk(_, chunk) {
120127
if (isSSR) {
121128
return;
122129
}
123130

124131
for (const id of Object.keys(chunk.modules)) {
125-
if (id.startsWith(VIRTUAL_CSS_NAME)) {
132+
if (id.startsWith(RESOLVED_VIRTUAL_MODULE_ID)) {
126133
delete chunk.modules[id];
127134
}
128135
}

0 commit comments

Comments
 (0)