Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 5bbdc58

Browse files
committed
Upgrade import map automatically
1 parent 5019443 commit 5bbdc58

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

server/app.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ import type {
3636
import { VERSION } from '../version.ts'
3737
import {
3838
defaultConfig,
39-
defaultImports,
39+
defaultImportMap,
4040
loadConfig,
41-
loadImportMap,
41+
loadAndUpgradeImportMap,
4242
RequiredConfig
4343
} from './config.ts'
4444
import { CSSProcessor } from './css.ts'
@@ -112,18 +112,17 @@ export class Application implements ServerApplication {
112112
let t = performance.now()
113113
const [config, importMap,] = await Promise.all([
114114
loadConfig(this.workingDir),
115-
loadImportMap(this.workingDir),
115+
loadAndUpgradeImportMap(this.workingDir),
116116
])
117117

118118
Object.assign(this.config, config)
119119
Object.assign(this.importMap, {
120120
...importMap,
121121
imports: Object.assign(
122-
defaultImports(this.config.react.version),
122+
defaultImportMap(this.config.react.version).imports,
123123
importMap.imports
124124
)
125125
})
126-
console.log(this.importMap)
127126
this.#pageRouting.config(this.config)
128127
this.#cssProcesser.config(!this.isDev, this.config.css)
129128

@@ -1317,7 +1316,6 @@ export class Application implements ServerApplication {
13171316
})
13181317
}
13191318

1320-
log.info('- bundle')
13211319
await this.#bundler.bundle(concatAllEntries())
13221320
}
13231321

server/config.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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'
23
import type { ImportMap, ReactResolve } from '../compiler/mod.ts'
34
import { defaultReactVersion } from '../shared/constants.ts'
45
import { existsFileSync, existsDirSync } from '../shared/fs.ts'
56
import log from '../shared/log.ts'
67
import util from '../shared/util.ts'
78
import type { Config, CSSOptions, PostCSSPlugin } from '../types.ts'
9+
import { VERSION } from '../version.ts'
810
import { getAlephPkgUri, reLocaleID } from './helper.ts'
911

1012
export interface RequiredConfig extends Required<Omit<Config, 'css'>> {
@@ -133,11 +135,12 @@ export async function loadConfig(workingDir: string): Promise<Config> {
133135
return config
134136
}
135137

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> {
138140
const importMap: ImportMap = { imports: {}, scopes: {} }
141+
let importMapFile = ''
139142
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)
141144
if (existsFileSync(importMapFile)) {
142145
const data = JSON.parse(await Deno.readTextFile(importMapFile))
143146
const imports: Record<string, string> = toPlainStringRecord(data.imports)
@@ -152,10 +155,44 @@ export async function loadImportMap(workingDir: string): Promise<ImportMap> {
152155
}
153156
}
154157

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+
155177
return importMap
156178
}
157179

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> {
159196
const alephPkgUri = getAlephPkgUri()
160197
return {
161198
'aleph/': `${alephPkgUri}/`,

0 commit comments

Comments
 (0)