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

Commit bacb677

Browse files
committed
Update types
1 parent bcb9530 commit bacb677

File tree

6 files changed

+106
-93
lines changed

6 files changed

+106
-93
lines changed

bundler/mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { ensureDir } from 'https://deno.land/[email protected]/fs/ensure_dir.ts'
33
import { transform } from '../compiler/mod.ts'
44
import { trimBuiltinModuleExts } from '../framework/core/module.ts'
55
import { ensureTextFile, existsFile, lazyRemove } from '../shared/fs.ts'
6-
import type { BrowserName } from '../types.d.ts'
6+
import type { BrowserName, Module } from '../types.d.ts'
77
import { VERSION } from '../version.ts'
88
import type { DependencyGraph } from '../server/analyzer.ts'
9-
import type { Aleph, Module } from '../server/aleph.ts'
9+
import type { Aleph } from '../server/aleph.ts'
1010
import { clearBuildCache, computeHash, getAlephPkgUri } from '../server/helper.ts'
1111
import { esbuild, stopEsbuild, esbuildUrlLoader } from './esbuild.ts'
1212

framework/react/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function init(aleph: Aleph) {
2424
code
2525
].join('\n')
2626
}))
27-
aleph.onTransform('hmr', ({ specifier, code }) => ({
27+
aleph.onTransform('hmr', ({ module: { specifier }, code }) => ({
2828
code: code.includes('$RefreshReg$(') ? [
2929
'const prevRefreshReg = $RefreshReg$;',
3030
'const prevRefreshSig = $RefreshSig$;',

server/aleph.ts

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import cssPlugin, { cssLoader, isCSS } from '../plugins/css.ts'
1414
import { ensureTextFile, existsDir, existsFile, lazyRemove } from '../shared/fs.ts'
1515
import log, { Measure } from '../shared/log.ts'
1616
import util from '../shared/util.ts'
17-
import type { Aleph as IAleph, ImportMap, LoadInput, LoadOutput, RouterURL, ResolveResult, TransformOutput, TransformInput } from '../types.d.ts'
17+
import type { Aleph as IAleph, DependencyDescriptor, ImportMap, LoadInput, LoadOutput, Module, RouterURL, ResolveResult, TransformOutput, TransformInput, SSRData, SSRInput, SSROutput } from '../types.d.ts'
1818
import { VERSION } from '../version.ts'
1919
import { Analyzer } from './analyzer.ts'
2020
import { cache } from './cache.ts'
@@ -25,44 +25,20 @@ import {
2525
getAlephPkgUri, getSourceType, isLocalUrl, moduleExclude, toLocalPath, toRelativePath
2626
} from './helper.ts'
2727
import { getContentType } from './mime.ts'
28-
import type { SSRData } from './renderer.ts'
2928
import { createHtml, Renderer } from './renderer.ts'
3029

31-
/** A module includes the compilation details. */
32-
export type Module = {
33-
specifier: string
34-
deps: DependencyDescriptor[]
35-
external?: boolean
36-
isStyle?: boolean
37-
externalRemoteDeps?: boolean
38-
ssrPropsFn?: string
39-
ssgPathsFn?: boolean
40-
denoHooks?: string[]
41-
hash?: string
42-
sourceHash: string
43-
jsFile: string
44-
jsBuffer?: Uint8Array
45-
ready: Promise<void>
46-
}
47-
4830
type ModuleSource = {
4931
code: string
5032
type: SourceType
5133
isStyle: boolean
5234
map?: string
5335
}
5436

55-
type DependencyDescriptor = {
56-
specifier: string
57-
isDynamic?: boolean
58-
hashLoc?: number
59-
}
60-
6137
type CompileOptions = {
6238
source?: ModuleSource,
6339
forceRefresh?: boolean,
6440
ignoreDeps?: boolean,
65-
externalRemoteDeps?: boolean
41+
httpExternal?: boolean
6642
}
6743

6844
type ResolveListener = {
@@ -80,9 +56,9 @@ type TransformListener = {
8056
transform(input: TransformInput): TransformOutput,
8157
}
8258

83-
type SsrListener = (path: string, html: string) => { html: string }
59+
type SsrListener = (input: SSRInput) => SSROutput
8460

85-
/** The Aleph class for aleph runtime. */
61+
/** The class for Aleph server runtime. */
8662
export class Aleph implements IAleph {
8763
#config: RequiredConfig
8864
#importMap: ImportMap
@@ -135,7 +111,7 @@ export class Aleph implements IAleph {
135111
}
136112
if (configFile) {
137113
if (!configFile.endsWith('.json')) {
138-
const mod = await this.compile(`/${basename(configFile)}`, { externalRemoteDeps: true })
114+
const mod = await this.compile(`/${basename(configFile)}`, { httpExternal: true })
139115
configFile = join(this.#buildDir, mod.jsFile)
140116
}
141117
Object.assign(this.#config, await loadConfig(configFile))
@@ -217,7 +193,7 @@ export class Aleph implements IAleph {
217193

218194
const mwFile = await findFile(this.#workingDir, ['ts', 'js', 'mjs'].map(ext => `${this.#config.srcDir}/api/_middlewares.${ext}`))
219195
if (mwFile) {
220-
const mwMod = await this.compile(`/api/${basename(mwFile)}`, { externalRemoteDeps: true })
196+
const mwMod = await this.compile(`/api/${basename(mwFile)}`, { httpExternal: true })
221197
const { default: _middlewares } = await import('file://' + join(this.#buildDir, mwMod.jsFile))
222198
const middlewares = Array.isArray(_middlewares) ? _middlewares.filter(fn => util.isFunction(fn)) : []
223199
this.#config.server.middlewares.push(...middlewares)
@@ -326,7 +302,7 @@ export class Aleph implements IAleph {
326302
const module = await this.compile(specifier, {
327303
forceRefresh: true,
328304
ignoreDeps: true,
329-
externalRemoteDeps: specifier.startsWith('/api/')
305+
httpExternal: specifier.startsWith('/api/')
330306
})
331307
const refreshPage = (
332308
this.#config.ssr &&
@@ -500,7 +476,7 @@ export class Aleph implements IAleph {
500476
if (this.#modules.has(specifier)) {
501477
return [url, this.#modules.get(specifier)!]
502478
}
503-
const module = await this.compile(specifier, { externalRemoteDeps: true })
479+
const module = await this.compile(specifier, { httpExternal: true })
504480
return [url, module]
505481
}
506482
}
@@ -519,7 +495,7 @@ export class Aleph implements IAleph {
519495
this.#transformListeners.push({ test, transform: callback })
520496
}
521497

522-
onSSR(callback: (path: string, html: string) => { html: string }): void {
498+
onSSR(callback: (input: SSRInput) => SSROutput): void {
523499
this.#ssrListeners.push(callback)
524500
}
525501

@@ -638,8 +614,13 @@ export class Aleph implements IAleph {
638614
async #renderPage(url: RouterURL, nestedModules: string[]): Promise<[string, Record<string, SSRData> | null]> {
639615
let [html, data] = await this.#renderer.renderPage(url, nestedModules)
640616
for (const callback of this.#ssrListeners) {
641-
const ret = callback(url.toString(), html)
642-
html = ret.html
617+
const ret = callback({ path: url.toString(), html, data })
618+
if (util.isFilledString(ret.html)) {
619+
html = ret.html
620+
}
621+
if (util.isPlainObject(ret.data)) {
622+
data = ret.data
623+
}
643624
}
644625
return [html, data]
645626
}
@@ -676,22 +657,31 @@ export class Aleph implements IAleph {
676657
rewrites: this.#config.server.rewrites,
677658
}
678659

660+
let code: string
679661
if (bundleMode) {
680-
return [
662+
code = [
681663
`__ALEPH__.basePath = ${JSON.stringify(basePath)};`,
682664
`__ALEPH__.pack["${alephPkgUri}/framework/${framework}/bootstrap.ts"].default(${JSON.stringify(config)});`
683665
].join('')
666+
} else {
667+
code = [
668+
`import bootstrap from "./-/${alephPkgPath}/framework/${framework}/bootstrap.js";`,
669+
this.isDev && `import { connect } from "./-/${alephPkgPath}/framework/core/hmr.js";`,
670+
this.isDev && `connect(${JSON.stringify(basePath)});`,
671+
`bootstrap(${JSON.stringify(config, undefined, this.isDev ? 2 : undefined)});`
672+
].filter(Boolean).join('\n')
684673
}
685-
686-
let code = [
687-
`import bootstrap from "./-/${alephPkgPath}/framework/${framework}/bootstrap.js";`,
688-
this.isDev && `import { connect } from "./-/${alephPkgPath}/framework/core/hmr.js";`,
689-
this.isDev && `connect(${JSON.stringify(basePath)});`,
690-
`bootstrap(${JSON.stringify(config, undefined, this.isDev ? 2 : undefined)});`
691-
].filter(Boolean).join('\n')
692674
this.#transformListeners.forEach(({ test, transform }) => {
693675
if (test === 'main') {
694-
const ret = transform({ specifier: '/main.js', code })
676+
const ret = transform({
677+
module: {
678+
specifier: '/main.js',
679+
deps: [],
680+
sourceHash: '',
681+
jsFile: '',
682+
},
683+
code,
684+
})
695685
code = ret.code
696686
}
697687
})
@@ -929,7 +919,8 @@ export class Aleph implements IAleph {
929919
}
930920
this.#transformListeners.forEach(({ test, transform }) => {
931921
if (test === 'hmr') {
932-
const ret = transform({ specifier, code })
922+
const { jsBuffer, ready, ...rest } = module
923+
const ret = transform({ module: structuredClone(rest), code })
933924
code = ret.code
934925
// todo: merge source map
935926
}
@@ -1034,7 +1025,7 @@ export class Aleph implements IAleph {
10341025
return module
10351026
}
10361027

1037-
private async initModule(specifier: string, { source: customSource, forceRefresh, externalRemoteDeps }: CompileOptions = {}): Promise<[Module, ModuleSource | null]> {
1028+
private async initModule(specifier: string, { source: customSource, forceRefresh, httpExternal }: CompileOptions = {}): Promise<[Module, ModuleSource | null]> {
10381029
let external = false
10391030
let data: any = null
10401031

@@ -1064,7 +1055,7 @@ export class Aleph implements IAleph {
10641055
}
10651056

10661057
let mod = this.#modules.get(specifier)
1067-
if (mod && !forceRefresh && !(!externalRemoteDeps && mod.externalRemoteDeps)) {
1058+
if (mod && !forceRefresh && !(!httpExternal && mod.httpExternal)) {
10681059
await mod.ready
10691060
return [mod, null]
10701061
}
@@ -1082,7 +1073,7 @@ export class Aleph implements IAleph {
10821073
specifier,
10831074
deps: [],
10841075
sourceHash: '',
1085-
externalRemoteDeps,
1076+
httpExternal,
10861077
jsFile,
10871078
ready: new Promise((resolve) => {
10881079
defer = (err?: Error) => {
@@ -1146,7 +1137,7 @@ export class Aleph implements IAleph {
11461137
ignoreDeps = false,
11471138
__tracing: Set<string> = new Set()
11481139
): Promise<void> {
1149-
const { specifier, jsFile, externalRemoteDeps } = module
1140+
const { specifier, jsFile, httpExternal } = module
11501141

11511142
// ensure the module only be transppiled once in current compilation context,
11521143
// to avoid dead-loop caused by cicular imports
@@ -1163,13 +1154,13 @@ export class Aleph implements IAleph {
11631154

11641155
const ms = new Measure()
11651156
const encoder = new TextEncoder()
1166-
const { code, deps, denoHooks, ssrPropsFn, ssgPathsFn, starExports, map } = await transform(specifier, source.code, {
1157+
const { code, deps, denoHooks, ssrPropsFn, ssgPathsFn, starExports, jsxStaticClassNames, map } = await transform(specifier, source.code, {
11671158
...this.commonCompilerOptions,
11681159
sourceMap: this.isDev,
11691160
swcOptions: {
11701161
sourceType: source.type
11711162
},
1172-
externalRemoteDeps
1163+
httpExternal
11731164
})
11741165

11751166
let jsCode = code
@@ -1213,9 +1204,18 @@ export class Aleph implements IAleph {
12131204
})
12141205
}
12151206

1207+
Object.assign(module, { ssrPropsFn, ssgPathsFn, jsxStaticClassNames })
1208+
if (util.isFilledArray(denoHooks)) {
1209+
module.denoHooks = denoHooks.map(id => util.trimPrefix(id, 'useDeno-'))
1210+
if (!this.#config.ssr) {
1211+
log.error(`'useDeno' hook in SPA mode is illegal: ${specifier}`)
1212+
}
1213+
}
1214+
12161215
this.#transformListeners.forEach(({ test, transform }) => {
12171216
if (test instanceof RegExp && test.test(specifier)) {
1218-
const { code, map } = transform({ specifier, code: jsCode, map: sourceMap })
1217+
const { jsBuffer, ready, ...rest } = module
1218+
const { code, map } = transform({ module: structuredClone(rest), code: jsCode, map: sourceMap })
12191219
jsCode = code
12201220
if (map) {
12211221
sourceMap = map
@@ -1244,15 +1244,6 @@ export class Aleph implements IAleph {
12441244
return dep
12451245
}) || []
12461246

1247-
module.ssrPropsFn = ssrPropsFn
1248-
module.ssgPathsFn = ssgPathsFn
1249-
if (util.isFilledArray(denoHooks)) {
1250-
module.denoHooks = denoHooks.map(id => util.trimPrefix(id, 'useDeno-'))
1251-
if (!this.#config.ssr) {
1252-
log.error(`'useDeno' hook in SPA mode is illegal: ${specifier}`)
1253-
}
1254-
}
1255-
12561247
ms.stop(`transpile '${specifier}'`)
12571248

12581249
const cacheFp = join(this.#buildDir, jsFile)
@@ -1282,7 +1273,7 @@ export class Aleph implements IAleph {
12821273
if (ignoreDeps) {
12831274
depModule = this.getModule(specifier)
12841275
} else {
1285-
const [mod, src] = await this.initModule(specifier, { externalRemoteDeps })
1276+
const [mod, src] = await this.initModule(specifier, { httpExternal })
12861277
if (!mod.external) {
12871278
await this.transpileModule(mod, src, false, __tracing)
12881279
}

server/analyzer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import util from '../shared/util.ts'
2-
import type { Aleph, Module } from './aleph.ts'
2+
import type { Module } from '../types.d.ts'
3+
import type { Aleph } from './aleph.ts'
34
import { getAlephPkgUri } from './helper.ts'
45

56
export type DependencyGraph = {

server/renderer.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
import { basename, dirname } from 'https://deno.land/[email protected]/path/mod.ts'
22
import log from '../shared/log.ts'
33
import util from '../shared/util.ts'
4-
import type { RouterURL } from '../types.d.ts'
5-
import type { Aleph, Module } from './aleph.ts'
6-
7-
export type SSRData = {
8-
value: any
9-
expires: number
10-
}
11-
12-
export type SSROutput = {
13-
html: string
14-
data: Record<string, SSRData> | null
15-
}
4+
import type { Module, RouterURL, SSRData, SSROutput } from '../types.d.ts'
5+
import type { Aleph } from './aleph.ts'
166

177
/** The render result of framework SSR. */
188
export type FrameworkRenderResult = {

0 commit comments

Comments
 (0)