1
- import { join } from 'https://deno.land/[email protected] /path/mod.ts'
1
+ import { basename , join } from 'https://deno.land/[email protected] /path/mod.ts'
2
+ import { bold } from 'https://deno.land/[email protected] /fmt/colors.ts'
2
3
import type { ImportMap , ReactResolve } from '../compiler/mod.ts'
3
4
import { defaultReactVersion } from '../shared/constants.ts'
4
5
import { existsFileSync , existsDirSync } from '../shared/fs.ts'
5
6
import log from '../shared/log.ts'
6
7
import util from '../shared/util.ts'
7
8
import type { Config , CSSOptions , PostCSSPlugin } from '../types.ts'
9
+ import { VERSION } from '../version.ts'
8
10
import { getAlephPkgUri , reLocaleID } from './helper.ts'
9
11
10
12
export interface RequiredConfig extends Required < Omit < Config , 'css' > > {
@@ -133,11 +135,12 @@ export async function loadConfig(workingDir: string): Promise<Config> {
133
135
return config
134
136
}
135
137
136
- /** load import maps from `import_map.json` */
137
- export async function loadImportMap ( workingDir : string ) : Promise < ImportMap > {
138
+ /** load and upgrade the import maps from `import_map.json` */
139
+ export async function loadAndUpgradeImportMap ( workingDir : string ) : Promise < ImportMap > {
138
140
const importMap : ImportMap = { imports : { } , scopes : { } }
141
+ let importMapFile = ''
139
142
for ( const filename of Array . from ( [ 'import_map' , 'import-map' , 'importmap' ] ) . map ( name => `${ name } .json` ) ) {
140
- const importMapFile = join ( workingDir , filename )
143
+ importMapFile = join ( workingDir , filename )
141
144
if ( existsFileSync ( importMapFile ) ) {
142
145
const data = JSON . parse ( await Deno . readTextFile ( importMapFile ) )
143
146
const imports : Record < string , string > = toPlainStringRecord ( data . imports )
@@ -152,10 +155,44 @@ export async function loadImportMap(workingDir: string): Promise<ImportMap> {
152
155
}
153
156
}
154
157
158
+ const upgrade : { name : string , url : string } [ ] = [ ]
159
+ for ( const [ name , url ] of Object . entries ( importMap . imports ) ) {
160
+ if ( url . startsWith ( 'https://deno.land/x/aleph' ) ) {
161
+ const a = url . split ( 'aleph' ) [ 1 ] . split ( '/' )
162
+ const version = util . trimPrefix ( a . shift ( ) ! , '@v' )
163
+ if ( version !== VERSION && a . length > 0 ) {
164
+ upgrade . push ( { name, url : `https://deno.land/x/aleph@v${ VERSION } /${ a . join ( '/' ) } ` } )
165
+ }
166
+ }
167
+ }
168
+
169
+ if ( upgrade . length > 0 ) {
170
+ log . info ( `upgraded ${ basename ( importMapFile ) } to ${ bold ( VERSION ) } ` )
171
+ upgrade . forEach ( ( { name, url } ) => {
172
+ importMap . imports [ name ] = url
173
+ } )
174
+ Deno . writeTextFile ( importMapFile , JSON . stringify ( importMap , undefined , 2 ) )
175
+ }
176
+
155
177
return importMap
156
178
}
157
179
158
- export function defaultImports ( reactVersion : string ) : Record < string , string > {
180
+ export function defaultImportMap ( reactVersion : string ) : ImportMap {
181
+ const alephPkgUri = getAlephPkgUri ( )
182
+ return {
183
+ imports : {
184
+ 'aleph/' : `${ alephPkgUri } /` ,
185
+ 'framework' : `${ alephPkgUri } /framework/core/mod.ts` ,
186
+ 'framework/react' : `${ alephPkgUri } /framework/react/mod.ts` ,
187
+ 'react' : `https://esm.sh/react@${ reactVersion } ` ,
188
+ 'react-dom' : `https://esm.sh/react-dom@${ reactVersion } ` ,
189
+ 'react-dom/server' : `https://esm.sh/react-dom@${ reactVersion } /server` ,
190
+ } ,
191
+ scopes : { }
192
+ }
193
+ }
194
+
195
+ export function updateImports ( reactVersion : string ) : Record < string , string > {
159
196
const alephPkgUri = getAlephPkgUri ( )
160
197
return {
161
198
'aleph/' : `${ alephPkgUri } /` ,
0 commit comments