1
1
import { createGenerator } from "https://esm.sh/@unocss/[email protected] " ;
2
- import { transform } from "../compiler/mod.ts" ;
3
- import type { TransformOptions } from "../compiler/types.ts" ;
2
+ import MagicString from "https://esm.sh/[email protected] " ;
3
+ import { parseDeps , transform } from "../compiler/mod.ts" ;
4
+ import type { TransformOptions , TransformResult } from "../compiler/types.ts" ;
4
5
import { readCode } from "../lib/fs.ts" ;
5
6
import { restoreUrl , toLocalPath } from "../lib/helpers.ts" ;
6
7
import log from "../lib/log.ts" ;
@@ -30,17 +31,17 @@ export default {
30
31
let mtime : number | undefined ;
31
32
let lang : string | undefined ;
32
33
let isCSS : boolean ;
33
- let enableAtomicCSS : boolean ;
34
+ let useUno : boolean ;
34
35
if ( loaded ) {
35
36
rawCode = loaded . code ;
36
37
mtime = loaded . modtime ;
37
38
lang = loaded . lang ;
38
39
isCSS = loaded . lang === "css" ;
39
- enableAtomicCSS = ! ! loaded . atomicCSS ;
40
+ useUno = ! ! loaded . atomicCSS ;
40
41
} else {
41
42
let ctype : string ;
42
43
[ rawCode , mtime , ctype ] = await readCode ( specifier ) ;
43
- enableAtomicCSS = pathname . endsWith ( ".jsx" ) || pathname . endsWith ( ".tsx" ) ;
44
+ useUno = pathname . endsWith ( ".jsx" ) || pathname . endsWith ( ".tsx" ) ;
44
45
isCSS = ctype . startsWith ( "text/css" ) || ctype . startsWith ( "text/postcss" ) ;
45
46
}
46
47
const etag = mtime
@@ -84,27 +85,46 @@ export default {
84
85
resType = "text/css" ;
85
86
}
86
87
} else {
87
- const { atomicCSS, jsxConfig, importMap, buildTarget } = options ;
88
88
const alephPkgUri = getAlephPkgUri ( ) ;
89
- const graphVersions = clientDependencyGraph . modules . filter ( ( mod ) =>
90
- ! util . isLikelyHttpURL ( specifier ) && ! util . isLikelyHttpURL ( mod . specifier ) && mod . specifier !== specifier
91
- ) . reduce ( ( acc , { specifier, version } ) => {
92
- acc [ specifier ] = version . toString ( 16 ) ;
93
- return acc ;
94
- } , { } as Record < string , string > ) ;
95
- let { code, deps, map } = await transform ( specifier , rawCode , {
96
- ...jsxConfig ,
97
- lang : lang as TransformOptions [ "lang" ] ,
98
- stripDataExport : isRouteFile ( specifier ) ,
99
- target : buildTarget ?? ( isDev ? "es2022" : "es2015" ) ,
100
- alephPkgUri,
101
- importMap : JSON . stringify ( importMap ) ,
102
- graphVersions,
103
- initialGraphVersion : clientDependencyGraph . initialVersion . toString ( 16 ) ,
104
- isDev,
105
- } ) ;
89
+ const { atomicCSS, jsxConfig, importMap, buildTarget } = options ;
90
+ let ret : TransformResult ;
91
+ if ( / ^ h t t p s ? : \/ \/ ( c d n \. ) e s m \. s h \/ / . test ( specifier ) ) {
92
+ // don't transform modules imported from esm.sh
93
+ const deps = await parseDeps ( specifier , rawCode , { importMap : JSON . stringify ( importMap ) } ) ;
94
+ if ( deps . length > 0 ) {
95
+ const s = new MagicString ( rawCode ) ;
96
+ deps . forEach ( ( dep ) => {
97
+ const { importUrl, loc } = dep ;
98
+ if ( loc ) {
99
+ s . overwrite ( loc . start , loc . end , `"${ toLocalPath ( importUrl ) } "` ) ;
100
+ }
101
+ } ) ;
102
+ ret = { code : s . toString ( ) , deps } ;
103
+ } else {
104
+ ret = { code : rawCode , deps } ;
105
+ }
106
+ } else {
107
+ const graphVersions = clientDependencyGraph . modules . filter ( ( mod ) =>
108
+ ! util . isLikelyHttpURL ( specifier ) && ! util . isLikelyHttpURL ( mod . specifier ) && mod . specifier !== specifier
109
+ ) . reduce ( ( acc , { specifier, version } ) => {
110
+ acc [ specifier ] = version . toString ( 16 ) ;
111
+ return acc ;
112
+ } , { } as Record < string , string > ) ;
113
+ ret = await transform ( specifier , rawCode , {
114
+ ...jsxConfig ,
115
+ lang : lang as TransformOptions [ "lang" ] ,
116
+ stripDataExport : isRouteFile ( specifier ) ,
117
+ target : buildTarget ?? ( isDev ? "es2022" : "es2015" ) ,
118
+ alephPkgUri,
119
+ importMap : JSON . stringify ( importMap ) ,
120
+ graphVersions,
121
+ initialGraphVersion : clientDependencyGraph . initialVersion . toString ( 16 ) ,
122
+ isDev,
123
+ } ) ;
124
+ }
125
+ let { code, map, deps } = ret ;
106
126
let inlineCSS = loaded ?. inlineCSS ;
107
- if ( enableAtomicCSS && Boolean ( atomicCSS ?. presets ?. length ) ) {
127
+ if ( useUno && Boolean ( atomicCSS ?. presets ?. length ) ) {
108
128
const uno = createGenerator ( atomicCSS ) ;
109
129
const { css } = await uno . generate ( rawCode , { id : specifier , minify : ! isDev } ) ;
110
130
if ( inlineCSS ) {
@@ -130,7 +150,7 @@ export default {
130
150
resBody = code +
131
151
`\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${ btoa ( JSON . stringify ( m ) ) } \n` ;
132
152
} catch {
133
- log . warn ( `Failed to add source map for '${ specifier } '` ) ;
153
+ log . warn ( `[dev] Failed to add source map for '${ specifier } '` ) ;
134
154
resBody = code ;
135
155
}
136
156
} else {
0 commit comments