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

Commit 858be54

Browse files
committed
v0.3.0-beta.10
1 parent 3cbd834 commit 858be54

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

bundler/mod.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import type { DependencyGraph } from '../server/analyzer.ts'
88
import type { Aleph } from '../server/aleph.ts'
99
import { clearBuildCache, computeHash, getAlephPkgUri } from '../server/helper.ts'
1010
import { ensureTextFile, existsFile, lazyRemove } from '../shared/fs.ts'
11-
import type { BrowserName, Module } from '../types.d.ts'
11+
import util from '../shared/util.ts'
12+
import type { BrowserName, Module, DependencyDescriptor } from '../types.d.ts'
1213
import { VERSION } from '../version.ts'
1314
import { esbuild, stopEsbuild, esmLoader } from './esbuild.ts'
1415

@@ -136,7 +137,7 @@ export class Bundler {
136137
return jsFile
137138
}
138139

139-
let source = await this.#aleph.resolveModuleSource(mod.specifier)
140+
let source = 'source' in mod ? (mod as any).source : await this.#aleph.resolveModuleSource(mod.specifier)
140141
if (source === null) {
141142
this.#compiled.delete(mod.specifier)
142143
throw new Error(`Unsupported module '${mod.specifier}'`)
@@ -175,8 +176,22 @@ export class Bundler {
175176
}
176177
}
177178

179+
let extraDeps: DependencyDescriptor[] = []
180+
for (const { test, transform } of this.#aleph.transformListeners) {
181+
if (test instanceof RegExp && test.test(mod.specifier)) {
182+
const { jsBuffer, ready, ...rest } = mod
183+
const ret = await transform({ module: { ...structuredClone(rest) }, code, bundleMode: true })
184+
if (util.isFilledString(ret?.code)) {
185+
code = ret!.code
186+
}
187+
if (Array.isArray(ret?.extraDeps)) {
188+
extraDeps.push(...ret!.extraDeps)
189+
}
190+
}
191+
}
192+
178193
// compile deps
179-
await Promise.all(mod.deps.map(async dep => {
194+
await Promise.all(mod.deps.concat(extraDeps).map(async dep => {
180195
if (!dep.specifier.startsWith('#') && !externals.includes(dep.specifier)) {
181196
const depMod = this.#aleph.getModule(dep.specifier)
182197
if (depMod !== null) {

server/aleph.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ export class Aleph implements IAleph {
471471
return this.#ready
472472
}
473473

474+
get transformListeners() {
475+
return this.#transformListeners
476+
}
477+
474478
/** get the module by given specifier. */
475479
getModule(specifier: string): Module | null {
476480
if (specifier === 'app') {
@@ -532,21 +536,23 @@ export class Aleph implements IAleph {
532536
if (sourceType === SourceType.Unknown) {
533537
throw new Error("addModule: unknown souce type")
534538
}
539+
const source = {
540+
code: sourceCode,
541+
type: sourceType,
542+
}
535543
const module = await this.compile(specifier, {
536-
source: {
537-
code: sourceCode,
538-
type: sourceType,
539-
},
544+
source,
540545
forceRefresh,
541546
})
542547
if (specifier.startsWith('pages/') || specifier.startsWith('api/')) {
543548
specifier = '/' + specifier
544549
}
545-
if (specifier.startsWith('/pages/')) {
550+
if (specifier.startsWith('/pages/') && this.isPageModule(specifier)) {
546551
this.#pageRouting.update(...this.createRouteUpdate(specifier))
547552
} else if (specifier.startsWith('/api/') && !specifier.startsWith('/api/_middlewares.')) {
548553
this.#apiRouting.update(...this.createRouteUpdate(specifier))
549554
}
555+
Object.assign(module, { source })
550556
return module
551557
}
552558

@@ -1025,6 +1031,21 @@ export class Aleph implements IAleph {
10251031
})
10261032
}
10271033

1034+
resolveImport({ jsFile, sourceHash }: Module, importer: string, bundleMode?: boolean, timeStamp?: boolean): string {
1035+
const relPath = toRelativePath(
1036+
dirname(toLocalPath(importer)),
1037+
jsFile
1038+
)
1039+
if (bundleMode) {
1040+
return util.trimSuffix(relPath, '.js') + '.bundling.js'
1041+
}
1042+
let hash = '#' + sourceHash.slice(0, 8)
1043+
if (timeStamp) {
1044+
hash += '-' + Date.now()
1045+
}
1046+
return relPath + hash
1047+
}
1048+
10281049
async resolveModuleSource(specifier: string, data?: any): Promise<ModuleSource> {
10291050
let sourceCode: string = ''
10301051
let sourceType: SourceType = SourceType.Unknown
@@ -1152,14 +1173,9 @@ export class Aleph implements IAleph {
11521173

11531174
if (!forceRefresh && await existsFile(metaFp)) {
11541175
try {
1155-
const { specifier: _specifier, sourceHash, deps, isStyle, ssrPropsFn, ssgPathsFn, denoHooks } = JSON.parse(await Deno.readTextFile(metaFp))
1156-
if (_specifier === specifier && util.isFilledString(sourceHash) && util.isArray(deps)) {
1157-
mod.sourceHash = sourceHash
1158-
mod.deps = deps
1159-
mod.isStyle = Boolean(isStyle) || undefined
1160-
mod.ssrPropsFn = util.isFilledString(ssrPropsFn) ? ssrPropsFn : undefined
1161-
mod.ssgPathsFn = Boolean(ssgPathsFn) || undefined
1162-
mod.denoHooks = util.isFilledArray(denoHooks) ? denoHooks : undefined
1176+
const meta = JSON.parse(await Deno.readTextFile(metaFp))
1177+
if (meta.specifier === specifier && util.isFilledString(meta.sourceHash) && util.isArray(meta.deps)) {
1178+
Object.assign(mod, meta)
11631179
} else {
11641180
log.warn(`removing invalid metadata '${name}.meta.json'`)
11651181
Deno.remove(metaFp)
@@ -1413,22 +1429,14 @@ export class Aleph implements IAleph {
14131429
}
14141430

14151431
private async cacheModule(module: Module, sourceMap?: string) {
1416-
const { specifier, jsBuffer, jsFile } = module
1432+
const { jsBuffer, jsFile, ready, ...rest } = module
14171433
if (jsBuffer) {
14181434
const cacheFp = join(this.#buildDir, jsFile)
14191435
const metaFp = cacheFp.slice(0, -3) + '.meta.json'
14201436
await ensureDir(dirname(cacheFp))
14211437
await Promise.all([
14221438
Deno.writeFile(cacheFp, jsBuffer),
1423-
Deno.writeTextFile(metaFp, JSON.stringify({
1424-
specifier,
1425-
sourceHash: module.sourceHash,
1426-
isStyle: module.isStyle,
1427-
ssrPropsFn: module.ssrPropsFn,
1428-
ssgPathsFn: module.ssgPathsFn,
1429-
denoHooks: module.denoHooks,
1430-
deps: module.deps,
1431-
}, undefined, 2)),
1439+
Deno.writeTextFile(metaFp, JSON.stringify({ ...rest }, undefined, 2)),
14321440
sourceMap ? Deno.writeTextFile(`${cacheFp}.map`, sourceMap) : Promise.resolve(),
14331441
lazyRemove(cacheFp.slice(0, -3) + '.bundling.js'),
14341442
])

types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface Aleph {
88
addDist(path: string, content: Uint8Array): Promise<void>
99
addModule(specifier: string, sourceCode: string, forceRefresh?: boolean): Promise<Module>
1010
fetchModule(specifier: string): Promise<{ content: Uint8Array, contentType: string | null }>
11+
resolveImport(module: Module, importer: string, bundleMode?: boolean, timeStamp?: boolean): string
1112
onResolve(test: RegExp, resolve: (specifier: string) => ResolveResult): void
1213
onLoad(test: RegExp, load: (input: LoadInput) => LoadOutput | Promise<LoadOutput>): void
1314
onTransform(test: 'hmr' | 'main' | RegExp, transform: (input: TransformInput) => TransformOutput | void | Promise<TransformOutput | void>): void
@@ -91,6 +92,7 @@ export type LoadOutput = {
9192
export type TransformInput = {
9293
module: Omit<Module, 'jsBuffer' | 'ready'>
9394
code: string
95+
bundleMode?: boolean
9496
map?: string
9597
}
9698

version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** `VERSION` managed by https://deno.land/x/publish */
2-
export const VERSION = '0.3.0-beta.9'
2+
export const VERSION = '0.3.0-beta.10'
33

44
/** `prepublish` will be invoked before publish */
55
export async function prepublish(version: string): Promise<boolean> {

0 commit comments

Comments
 (0)