@@ -516,11 +516,12 @@ export class Appliaction {
516
516
}
517
517
518
518
const p = Deno . run ( {
519
- cmd : [ Deno . execPath ( ) , 'info' ] ,
519
+ cmd : [ Deno . execPath ( ) , 'info' , '--unstable' , '--json' ] ,
520
520
stdout : 'piped' ,
521
521
stderr : 'null'
522
522
} )
523
- this . #denoCacheDir = ( new TextDecoder ) . decode ( await p . output ( ) ) . split ( '"' ) [ 1 ]
523
+ const output = ( new TextDecoder ) . decode ( await p . output ( ) )
524
+ this . #denoCacheDir = JSON . parse ( output ) . denoDir
524
525
p . close ( )
525
526
if ( ! existsDirSync ( this . #denoCacheDir) ) {
526
527
log . fatal ( 'invalid deno cache dir' )
@@ -937,7 +938,7 @@ export class Appliaction {
937
938
const isLocalhost = / ^ h t t p s ? : \/ \/ l o c a l h o s t ( : \d + ) ? \/ / . test ( url )
938
939
if ( [ 'js' , 'ts' , 'jsx' , 'tsx' ] . includes ( mod . loader ) && ! isLocalhost ) {
939
940
try {
940
- sourceContent = await this . loadDependency ( url )
941
+ sourceContent = await this . fetchDependency ( url )
941
942
const sourceHash = ( new Sha1 ) . update ( sourceContent ) . hex ( )
942
943
if ( mod . sourceHash === '' || mod . sourceHash !== sourceHash ) {
943
944
mod . sourceHash = sourceHash
@@ -949,7 +950,7 @@ export class Appliaction {
949
950
return mod
950
951
}
951
952
} else {
952
- // cache non-localhost file to local drive
953
+ // todo: cache non-localhost file to local drive
953
954
try {
954
955
if ( ! isLocalhost ) {
955
956
log . info ( 'Download' , url )
@@ -1246,44 +1247,46 @@ export class Appliaction {
1246
1247
} )
1247
1248
}
1248
1249
1249
- /** load dependency content, use deno builtin cache system */
1250
- private async loadDependency ( url : string ) : Promise < Uint8Array > {
1250
+ /** fetch dependency content, use deno builtin cache system */
1251
+ private async fetchDependency ( url : string ) : Promise < Uint8Array > {
1251
1252
const u = new URL ( url )
1252
1253
if ( url . startsWith ( 'https://esm.sh/' ) ) {
1253
1254
if ( this . isDev && ! u . searchParams . has ( 'dev' ) ) {
1254
1255
u . searchParams . set ( 'dev' , '' )
1256
+ u . search = u . search . replace ( 'dev=' , 'dev' )
1255
1257
}
1256
- u . search = u . search . replace ( / \= ( & | $ ) / , '$1' )
1257
1258
}
1258
1259
1259
1260
const { protocol, hostname, port, pathname, search } = u
1260
1261
const versioned = reFullVersion . test ( pathname )
1261
- const dir = path . join ( this . #denoCacheDir, 'deps' , util . trimSuffix ( protocol , ':' ) , hostname + ( port ? '_PORT' + port : '' ) )
1262
- const filename = path . join ( dir , ( new Sha256 ( ) ) . update ( pathname + search ) . hex ( ) )
1262
+ const reload = this . #reloading || ! versioned
1263
+ const cacheDir = path . join ( this . #denoCacheDir, 'deps' , util . trimSuffix ( protocol , ':' ) , hostname + ( port ? '_PORT' + port : '' ) )
1264
+ const cacheFilename = path . join ( cacheDir , ( new Sha256 ( ) ) . update ( pathname + search ) . hex ( ) )
1263
1265
1264
- if ( versioned && ! this . #reloading && existsFileSync ( filename ) ) {
1265
- return await Deno . readFile ( filename )
1266
+ if ( ! reload && existsFileSync ( cacheFilename ) ) {
1267
+ return await Deno . readFile ( cacheFilename )
1266
1268
}
1267
1269
1268
- const p = Deno . run ( {
1269
- cmd : [
1270
- Deno . execPath ( ) ,
1271
- 'cache' ,
1272
- this . #reloading || ! versioned ? '--reload' : '' ,
1273
- u . toString ( )
1274
- ] . filter ( Boolean ) ,
1275
- stdout : 'piped' ,
1276
- stderr : 'piped'
1277
- } )
1278
- await Deno . stderr . write ( await p . output ( ) )
1279
- await Deno . stderr . write ( await p . stderrOutput ( ) )
1280
- p . close ( )
1281
-
1282
- if ( existsFileSync ( filename ) ) {
1283
- return await Deno . readFile ( filename )
1284
- } else {
1285
- throw new Error ( `not found` )
1270
+ try {
1271
+ log . info ( 'Download' , url )
1272
+ const p = Deno . run ( {
1273
+ cmd : [ Deno . execPath ( ) , 'cache' , reload ? '--reload' : '' , u . toString ( ) ] ,
1274
+ stdout : 'null' ,
1275
+ stderr : 'null'
1276
+ } )
1277
+ await p . status ( )
1278
+ p . close ( )
1279
+ if ( existsFileSync ( cacheFilename ) ) {
1280
+ return await Deno . readFile ( cacheFilename )
1281
+ }
1282
+ } catch ( e ) {
1283
+ log . warn ( e )
1286
1284
}
1285
+
1286
+ // download dep when deno cache failed
1287
+ log . info ( 'Redownload' , url )
1288
+ const buffer = await fetch ( u . toString ( ) ) . then ( resp => resp . arrayBuffer ( ) )
1289
+ return await Deno . readAll ( new Deno . Buffer ( buffer ) )
1287
1290
}
1288
1291
1289
1292
/** bundle modules for production. */
@@ -1388,7 +1391,7 @@ export class Appliaction {
1388
1391
} ) . flat ( ) . join ( '\n' )
1389
1392
const mod = this . newModule ( `/${ name } .js` )
1390
1393
const hash = ( new Sha1 ) . update ( buildChecksum ) . update ( bundlingCode ) . hex ( )
1391
- const bundlingFile = path . join ( this . buildDir , mod . url )
1394
+ const bundlingFile = path . join ( this . buildDir , ` ${ name } .bundling.js` )
1392
1395
const bundleFile = path . join ( this . buildDir , `${ name } .bundle.${ util . shortHash ( hash ) } .js` )
1393
1396
1394
1397
if ( ! existsFileSync ( bundleFile ) ) {
@@ -1445,7 +1448,7 @@ export class Appliaction {
1445
1448
} )
1446
1449
1447
1450
// workaround for https://github.com/denoland/deno/issues/9212
1448
- if ( Deno . version . deno === '1.7.0' && bundlingFile . endsWith ( '/ deps.bundling.js' ) ) {
1451
+ if ( Deno . version . deno === '1.7.0' && bundlingFile . endsWith ( 'deps.bundling.js' ) ) {
1449
1452
code = code . replace ( ' _ = l.baseState, ' , ' var _ = l.baseState, ' )
1450
1453
}
1451
1454
0 commit comments