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

Commit af92fda

Browse files
committed
Improve init
1 parent 5f0c1d6 commit af92fda

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

server/aleph.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class Aleph implements IAleph {
117117
this.#pageRouting = new Routing(this.#config)
118118
}
119119

120-
await fixConfigAndImportMap(this.#workingDir, this.#config, this.#importMap)
120+
await fixConfigAndImportMap(this.#workingDir, this.#config, this.#importMap, importMapFile)
121121
ms.stop('load config')
122122

123123
Deno.env.set('ALEPH_ENV', this.#mode)
@@ -129,24 +129,32 @@ export class Aleph implements IAleph {
129129
const srcDir = join(this.#workingDir, this.#config.srcDir)
130130
const apiDir = join(srcDir, 'api')
131131
const pagesDir = join(srcDir, 'pages')
132-
const buildManifestFile = join(this.#buildDir, 'build.manifest.json')
132+
const manifestFile = join(this.#buildDir, 'build.manifest.json')
133133
const importMapString = JSON.stringify(this.#importMap)
134134
const pluginNames = this.#config.plugins.map(({ name }) => name).join(',')
135135

136-
let shouldRebuild = !await existsFile(buildManifestFile)
137-
let saveManifestFile = shouldRebuild
136+
let shouldRebuild = !await existsFile(manifestFile)
137+
let shouldUpdateManifest = shouldRebuild
138138
if (!shouldRebuild) {
139139
try {
140-
const v = JSON.parse(await Deno.readTextFile(buildManifestFile))
140+
const v = JSON.parse(await Deno.readTextFile(manifestFile))
141+
const confirmClean = (msg: string): boolean => {
142+
shouldUpdateManifest = true
143+
// in 'prodcution' mode, we don't ask user to clean build cache
144+
if (!this.isDev) {
145+
return true
146+
}
147+
return confirm(msg)
148+
}
141149
shouldRebuild = (
142150
typeof v !== 'object' ||
143151
v === null ||
144152
v.compiler !== wasmChecksum ||
145-
(v.importMap !== importMapString && confirm('The import-maps has been changed, clean build cache?')) ||
146-
(v.plugins !== pluginNames && confirm('The plugin list has been updated, clean build cache?'))
153+
(v.importMap !== importMapString && confirmClean('The import-maps has been changed, clean build cache?')) ||
154+
(v.plugins !== pluginNames && confirmClean('The plugin list has been updated, clean build cache?'))
147155
)
148-
if (!shouldRebuild && v.importMap !== importMapString && v.plugins !== pluginNames) {
149-
saveManifestFile = true
156+
if (shouldRebuild) {
157+
shouldUpdateManifest = true
150158
}
151159
} catch (e) { }
152160
}
@@ -159,9 +167,8 @@ export class Aleph implements IAleph {
159167
await ensureDir(this.#buildDir)
160168
}
161169

162-
if (saveManifestFile) {
163-
log.debug('rebuild...')
164-
ensureTextFile(buildManifestFile, JSON.stringify({
170+
if (shouldUpdateManifest) {
171+
ensureTextFile(manifestFile, JSON.stringify({
165172
aleph: VERSION,
166173
deno: Deno.version.deno,
167174
compiler: wasmChecksum,

server/config.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { bold } from 'https://deno.land/[email protected]/fmt/colors.ts'
12
import { basename, join } from 'https://deno.land/[email protected]/path/mod.ts'
23
import type { ReactOptions } from '../compiler/mod.ts'
34
import { defaultReactVersion } from '../shared/constants.ts'
45
import { existsDir } from '../shared/fs.ts'
56
import log from '../shared/log.ts'
67
import util from '../shared/util.ts'
78
import type { BrowserName, Config, RequiredConfig as TRequiredConfig, ImportMap, PostCSSPlugin } from '../types.d.ts'
9+
import { VERSION } from '../version.ts'
810
import { getAlephPkgUri } from './helper.ts'
911

1012
export 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 (/\/\/esm\.sh\/react@\d+\.\d+\.\d+(-[a-z0-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 (/\/\/deno\.land\/x\/aleph@v?\d+\.\d+\.\d+(-[a-z0-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 (/\/\/esm\.sh\/react@\d+\.\d+\.\d+(-[a-z0-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

218241
function isFramework(v: any): v is 'react' {

0 commit comments

Comments
 (0)