1
1
import { createGenerator } from "https://esm.sh/@unocss/[email protected] " ;
2
+ import MagicString from "https://esm.sh/[email protected] " ;
2
3
import { transform } from "../compiler/mod.ts" ;
3
4
import type { TransformOptions } from "../compiler/types.ts" ;
4
5
import { readCode } from "../lib/fs.ts" ;
@@ -51,8 +52,10 @@ export default {
51
52
return new Response ( null , { status : 304 } ) ;
52
53
}
53
54
54
- let clientDependencyGraph : DependencyGraph | undefined = Reflect . get ( globalThis , "clientDependencyGraph" ) ;
55
- if ( ! clientDependencyGraph ) {
55
+ let clientDependencyGraph : DependencyGraph ;
56
+ if ( Reflect . has ( globalThis , "clientDependencyGraph" ) ) {
57
+ clientDependencyGraph = Reflect . get ( globalThis , "clientDependencyGraph" ) ;
58
+ } else {
56
59
clientDependencyGraph = new DependencyGraph ( ) ;
57
60
Reflect . set ( globalThis , "clientDependencyGraph" , clientDependencyGraph ) ;
58
61
}
@@ -75,28 +78,20 @@ export default {
75
78
asJsModule,
76
79
hmr : isDev ,
77
80
} ) ;
81
+ clientDependencyGraph . mark ( specifier , { deps : deps ?. map ( ( specifier ) => ( { specifier } ) ) } ) ;
78
82
resBody = code ;
79
83
if ( ! asJsModule ) {
80
84
resType = "text/css" ;
81
85
}
82
- clientDependencyGraph . mark ( specifier , { deps : deps ?. map ( ( specifier ) => ( { specifier } ) ) } ) ;
83
86
} else {
84
87
const { atomicCSS, jsxConfig, importMap, buildTarget } = options ;
85
- const graphVersions = clientDependencyGraph . modules . filter ( ( mod ) =>
86
- ! util . isLikelyHttpURL ( specifier ) && ! util . isLikelyHttpURL ( mod . specifier ) && mod . specifier !== specifier
87
- ) . reduce ( ( acc , { specifier, version } ) => {
88
- acc [ specifier ] = version . toString ( 16 ) ;
89
- return acc ;
90
- } , { } as Record < string , string > ) ;
91
88
const alephPkgUri = getAlephPkgUri ( ) ;
92
- const { code, deps } = await transform ( specifier , rawCode , {
89
+ let { code, deps, map } = await transform ( specifier , rawCode , {
93
90
...jsxConfig ,
94
91
lang : lang as TransformOptions [ "lang" ] ,
95
92
stripDataExport : isRouteFile ( specifier ) ,
96
93
target : buildTarget ?? ( isDev ? "es2022" : "es2015" ) ,
97
94
alephPkgUri,
98
- graphVersions,
99
- initialGraphVersion : clientDependencyGraph . initialVersion . toString ( 16 ) ,
100
95
importMap : JSON . stringify ( importMap ) ,
101
96
isDev,
102
97
} ) ;
@@ -110,16 +105,39 @@ export default {
110
105
inlineCSS = css ;
111
106
}
112
107
}
108
+ const locDeps = deps ?. filter ( ( dep ) => ! util . isLikelyHttpURL ( dep . specifier ) ) ;
109
+ if ( locDeps ?. length ) {
110
+ const s = new MagicString ( code ) ;
111
+ locDeps . forEach ( ( dep ) => {
112
+ const { specifier, importUrl, loc } = dep ;
113
+ const versionStr = clientDependencyGraph . get ( specifier ) ?. version || clientDependencyGraph . initialVersion ;
114
+ let url = importUrl ;
115
+ if ( url . includes ( "?" ) ) {
116
+ url = `"${ url } &v=${ versionStr } "` ;
117
+ } else {
118
+ url = `"${ url } ?v=${ versionStr } "` ;
119
+ }
120
+ s . overwrite ( loc . start , loc . end , url ) ;
121
+ } ) ;
122
+ code = s . toString ( ) ;
123
+ }
113
124
if ( inlineCSS ) {
125
+ resBody += `\nimport { applyCSS as __applyCSS } from "${
126
+ toLocalPath ( alephPkgUri )
127
+ } /framework/core/style.ts";\n__applyCSS(${ JSON . stringify ( specifier ) } , ${ JSON . stringify ( inlineCSS ) } );`;
128
+ deps = [ ...( deps || [ ] ) , { specifier : alephPkgUri + "/framework/core/style.ts" } ] as typeof deps ;
129
+ }
130
+ clientDependencyGraph . mark ( specifier , { deps } ) ;
131
+ if ( map ) {
132
+ const m = JSON . parse ( map ) ;
133
+ if ( ! util . isLikelyHttpURL ( specifier ) ) {
134
+ m . sources = [ `file://source/${ util . trimPrefix ( specifier , "." ) } ` ] ;
135
+ }
136
+ m . sourcesContent = [ rawCode ] ;
114
137
resBody = code +
115
- `\nimport { applyCSS as __applyCSS } from "${
116
- toLocalPath ( alephPkgUri )
117
- } /framework/core/style.ts";\n__applyCSS(${ JSON . stringify ( specifier ) } , ${ JSON . stringify ( inlineCSS ) } );`;
118
- deps ?. push ( { specifier : alephPkgUri + "/framework/core/style.ts" } ) ;
119
- clientDependencyGraph . mark ( specifier , { deps } ) ;
138
+ `\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${ btoa ( JSON . stringify ( m ) ) } ` ;
120
139
} else {
121
140
resBody = code ;
122
- clientDependencyGraph . mark ( specifier , { deps } ) ;
123
141
}
124
142
}
125
143
return new Response ( resBody , {
0 commit comments