@@ -4,15 +4,16 @@ import { createRenderer } from "@navita/core/createRenderer";
4
4
import { importMap as defaultImportMap } from "@navita/css" ;
5
5
import type { Plugin , ViteDevServer } from "vite" ;
6
6
7
- let renderer : Renderer ;
8
-
9
7
/*
10
8
Some information for anyone wondering why we have duplicate css for the initial load
11
9
during development in remix.
12
10
https://github.com/remix-run/remix/discussions/8070#discussioncomment-7625870
13
11
*/
14
12
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 ;
16
17
17
18
interface Options {
18
19
importMap ?: ImportMap ;
@@ -26,12 +27,18 @@ export function navita(options?: Options) {
26
27
let lastCssContent : string | undefined ;
27
28
let context : string ;
28
29
let isSSR = false ;
30
+ let isDEV = true ;
29
31
30
32
return {
31
- name : "navita" ,
32
33
enforce : "pre" ,
33
- config ( ) {
34
+ name : "navita" ,
35
+ config ( _ , env ) {
36
+ isDEV = env . command === 'serve' ;
37
+
34
38
return {
39
+ optimizeDeps : {
40
+ include : isDEV ? [ '@navita/css' ] : [ ] ,
41
+ } ,
35
42
ssr : {
36
43
external : [
37
44
'@navita/css' ,
@@ -49,9 +56,13 @@ export function navita(options?: Options) {
49
56
server = _server ;
50
57
} ,
51
58
async buildStart ( ) {
59
+ if ( renderer ) {
60
+ return ;
61
+ }
62
+
52
63
const defaultEngineOptions = {
53
- enableSourceMaps : ! ! server ,
54
- enableDebugIdentifiers : ! ! server ,
64
+ enableSourceMaps : isDEV ,
65
+ enableDebugIdentifiers : isDEV ,
55
66
...( options ?. engineOptions || { } ) ,
56
67
} ;
57
68
@@ -68,16 +79,14 @@ export function navita(options?: Options) {
68
79
} ,
69
80
} ) ;
70
81
} ,
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 ;
74
85
}
75
-
76
- return ;
77
86
} ,
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 ( ) ;
81
90
}
82
91
83
92
return ;
@@ -97,9 +106,7 @@ export function navita(options?: Options) {
97
106
const newCssContent = renderer . engine . renderCssToString ( ) ;
98
107
99
108
if ( lastCssContent !== newCssContent ) {
100
- invalidateModule ( VIRTUAL_CSS_NAME ) ;
101
- this . addWatchFile ( VIRTUAL_CSS_NAME ) ;
102
-
109
+ invalidateModule ( RESOLVED_VIRTUAL_MODULE_ID ) ;
103
110
lastCssContent = newCssContent ;
104
111
105
112
for ( const file of dependencies ) {
@@ -112,17 +119,17 @@ export function navita(options?: Options) {
112
119
}
113
120
114
121
return {
115
- code : `${ result } import "${ VIRTUAL_CSS_NAME } ";` ,
122
+ code : `${ result } \nimport "${ VIRTUAL_MODULE_ID } ";` ,
116
123
map : sourceMap ,
117
124
} ;
118
125
} ,
119
- renderChunk ( _ , chunk , third ) {
126
+ renderChunk ( _ , chunk ) {
120
127
if ( isSSR ) {
121
128
return ;
122
129
}
123
130
124
131
for ( const id of Object . keys ( chunk . modules ) ) {
125
- if ( id . startsWith ( VIRTUAL_CSS_NAME ) ) {
132
+ if ( id . startsWith ( RESOLVED_VIRTUAL_MODULE_ID ) ) {
126
133
delete chunk . modules [ id ] ;
127
134
}
128
135
}
0 commit comments