1+ import { bold } from 'https://deno.land/[email protected] /fmt/colors.ts' 12import { basename , join } from 'https://deno.land/[email protected] /path/mod.ts' 23import type { ReactOptions } from '../compiler/mod.ts'
34import { defaultReactVersion } from '../shared/constants.ts'
45import { existsDir } from '../shared/fs.ts'
56import log from '../shared/log.ts'
67import util from '../shared/util.ts'
78import type { BrowserName , Config , RequiredConfig as TRequiredConfig , ImportMap , PostCSSPlugin } from '../types.d.ts'
9+ import { VERSION } from '../version.ts'
810import { getAlephPkgUri } from './helper.ts'
911
1012export type RequiredConfig = TRequiredConfig & {
@@ -192,7 +194,7 @@ export async function loadImportMap(importMapFile: string): Promise<ImportMap> {
192194 * - fix import map when the `srcDir` does not equal '/'
193195 * - respect react version in import map
194196 */
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 ) {
196198 // set default src directory
197199 if (
198200 config . srcDir === '/' &&
@@ -202,17 +204,38 @@ export async function fixConfigAndImportMap(workingDir: string, config: Required
202204 config . srcDir = '/src'
203205 }
204206
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 )
214237 }
215- } )
238+ }
216239}
217240
218241function isFramework ( v : any ) : v is 'react' {
0 commit comments