1
+ import { bold } from 'https://deno.land/[email protected] /fmt/colors.ts'
1
2
import { basename , join } from 'https://deno.land/[email protected] /path/mod.ts'
2
3
import type { ReactOptions } from '../compiler/mod.ts'
3
4
import { defaultReactVersion } from '../shared/constants.ts'
4
5
import { existsDir } from '../shared/fs.ts'
5
6
import log from '../shared/log.ts'
6
7
import util from '../shared/util.ts'
7
8
import type { BrowserName , Config , RequiredConfig as TRequiredConfig , ImportMap , PostCSSPlugin } from '../types.d.ts'
9
+ import { VERSION } from '../version.ts'
8
10
import { getAlephPkgUri } from './helper.ts'
9
11
10
12
export type RequiredConfig = TRequiredConfig & {
@@ -192,7 +194,7 @@ export async function loadImportMap(importMapFile: string): Promise<ImportMap> {
192
194
* - fix import map when the `srcDir` does not equal '/'
193
195
* - respect react version in import map
194
196
*/
195
- export async function fixConfigAndImportMap ( workingDir : string , config : RequiredConfig , importMap : ImportMap ) {
197
+ export async function fixConfigAndImportMap ( workingDir : string , config : RequiredConfig , importMap : ImportMap , importMapFile : string | null ) {
196
198
// set default src directory
197
199
if (
198
200
config . srcDir === '/' &&
@@ -202,17 +204,38 @@ export async function fixConfigAndImportMap(workingDir: string, config: Required
202
204
config . srcDir = '/src'
203
205
}
204
206
205
- Object . keys ( importMap . imports ) . forEach ( key => {
206
- const url = importMap . imports [ key ]
207
- // strip `srcDir` prefix
208
- if ( config . srcDir !== '/' && url . startsWith ( '.' + config . srcDir ) ) {
209
- importMap . imports [ key ] = '.' + util . trimPrefix ( url , '.' + config . srcDir )
210
- }
211
- // react verison should respect the import maps
212
- if ( / \/ \/ e s m \. s h \/ r e a c t @ \d + \. \d + \. \d + ( - [ a - z 0 - 9 \. ] + ) ? $ / . test ( url ) ) {
213
- config . react . version = url . split ( '@' ) . pop ( ) !
207
+ if ( importMapFile ) {
208
+ let imports = { ...importMap . imports }
209
+ let updateImportMaps : boolean | null = null
210
+
211
+ Object . keys ( importMap . imports ) . forEach ( key => {
212
+ const url = importMap . imports [ key ]
213
+ // strip `srcDir` prefix
214
+ if ( config . srcDir !== '/' && url . startsWith ( '.' + config . srcDir ) ) {
215
+ importMap . imports [ key ] = '.' + util . trimPrefix ( url , '.' + config . srcDir )
216
+ }
217
+ // fix Aleph.js version
218
+ if ( / \/ \/ d e n o \. l a n d \/ x \/ a l e p h @ v ? \d + \. \d + \. \d + ( - [ a - z 0 - 9 \. ] + ) ? \/ / . test ( url ) ) {
219
+ const [ prefix , rest ] = util . splitBy ( url , '@' )
220
+ const [ version , suffix ] = util . splitBy ( rest , '/' )
221
+ if ( version !== 'v' + VERSION && updateImportMaps === null ) {
222
+ updateImportMaps = confirm ( `You are using a different version of Aleph.js, expect ${ version } -> v${ bold ( VERSION ) } , update '${ basename ( importMapFile ) } '?` )
223
+ }
224
+ if ( updateImportMaps ) {
225
+ imports [ key ] = `${ prefix } @v${ VERSION } /${ suffix } `
226
+ }
227
+ }
228
+ // react verison should respect the import maps
229
+ if ( / \/ \/ e s m \. s h \/ r e a c t @ \d + \. \d + \. \d + ( - [ a - z 0 - 9 \. ] + ) ? $ / . test ( url ) ) {
230
+ config . react . version = url . split ( '@' ) . pop ( ) !
231
+ }
232
+ } )
233
+
234
+ if ( updateImportMaps ) {
235
+ const json = JSON . stringify ( { ...importMap , imports } , undefined , 2 )
236
+ await Deno . writeTextFile ( importMapFile , json )
214
237
}
215
- } )
238
+ }
216
239
}
217
240
218
241
function isFramework ( v : any ) : v is 'react' {
0 commit comments