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

Commit adec713

Browse files
committed
Fix onTransform hook
1 parent 00967ff commit adec713

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

bundler/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export class Bundler {
188188
r[name] = chunk.filename
189189
return r
190190
}, {} as Record<string, string>)
191-
const mainJS = `__ALEPH__.asyncChunks=${JSON.stringify(asyncChunks)};` + this.#aleph.createMainJS(true)
191+
const mainJS = `__ALEPH__.asyncChunks=${JSON.stringify(asyncChunks)};` + await this.#aleph.createMainJS(true)
192192
const hash = computeHash(mainJS)
193193
const bundleFilename = `main.bundle.${hash.slice(0, 8)}.js`
194194
const bundleFilePath = join(this.#aleph.buildDir, bundleFilename)

compiler/mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export async function transform(specifier: string, code: string, options: Transf
124124
inlineStyles,
125125
denoHooks,
126126
starExports,
127+
jsxStaticClassNames,
127128
map,
128129
} = transformSync(specifier, code, transformOptions)
129130

@@ -160,6 +161,7 @@ export async function transform(specifier: string, code: string, options: Transf
160161
ssgPathsFn,
161162
denoHooks,
162163
starExports,
164+
jsxStaticClassNames,
163165
map
164166
}
165167
}

server/aleph.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type LoadListener = {
5353

5454
type TransformListener = {
5555
test: RegExp | 'hmr' | 'main',
56-
transform(input: TransformInput): TransformOutput,
56+
transform(input: TransformInput): TransformOutput | void | Promise<TransformOutput> | Promise<void>,
5757
}
5858

5959
type SsrListener = (input: SSRInput) => SSROutput
@@ -143,6 +143,7 @@ export class Aleph implements IAleph {
143143
const pagesDir = join(srcDir, 'pages')
144144
const buildManifestFile = join(this.#buildDir, 'build.manifest.json')
145145
const importMapString = JSON.stringify(this.#importMap)
146+
const pluginNames = this.#config.plugins.map(({ name }) => name).join(',')
146147

147148
let shouldRebuild = !await existsFile(buildManifestFile)
148149
let saveManifestFile = shouldRebuild
@@ -153,7 +154,8 @@ export class Aleph implements IAleph {
153154
typeof v !== 'object' ||
154155
v === null ||
155156
v.compiler !== wasmChecksum ||
156-
(v.importMap !== importMapString && confirm('The import-maps has been changed, rebuild modules?'))
157+
(v.importMap !== importMapString && confirm('The import-maps has been changed, clean build cache?')) ||
158+
(v.plugins !== pluginNames && confirm('The plugin list has been updated, clean build cache?'))
157159
)
158160
if (!shouldRebuild && v.importMap !== importMapString) {
159161
saveManifestFile = true
@@ -176,6 +178,7 @@ export class Aleph implements IAleph {
176178
deno: Deno.version.deno,
177179
compiler: wasmChecksum,
178180
importMap: importMapString,
181+
plugins: pluginNames,
179182
}, undefined, 2))
180183
}
181184

@@ -299,6 +302,11 @@ export class Aleph implements IAleph {
299302
if (this.#modules.has(specifier)) {
300303
try {
301304
const prevModule = this.#modules.get(specifier)!
305+
if (prevModule.jsFile === '/aleph.config.js') {
306+
log.info(`${prevModule.specifier.slice(1)} has be changed, please restart the server.`)
307+
return
308+
}
309+
302310
const module = await this.compile(specifier, {
303311
forceRefresh: true,
304312
ignoreDeps: true,
@@ -491,7 +499,7 @@ export class Aleph implements IAleph {
491499
this.#loadListeners.push({ test, load: callback })
492500
}
493501

494-
onTransform(test: RegExp | 'hmr' | 'main', callback: (input: TransformInput) => TransformOutput): void {
502+
onTransform(test: RegExp | 'hmr' | 'main', callback: (input: TransformInput) => TransformOutput | Promise<TransformOutput>): void {
495503
this.#transformListeners.push({ test, transform: callback })
496504
}
497505

@@ -501,10 +509,19 @@ export class Aleph implements IAleph {
501509

502510
/** add a module by given path and optional source code. */
503511
async addModule(specifier: string, sourceCode: string): Promise<Module> {
512+
let sourceType = getSourceType(specifier)
513+
if (sourceType === SourceType.Unknown) {
514+
throw new Error("addModule: unknown souce type")
515+
}
516+
if (sourceType === SourceType.CSS) {
517+
const ret = await cssLoader({ specifier, data: (new TextEncoder).encode(sourceCode) }, this)
518+
sourceCode = ret.code
519+
sourceType = SourceType.JS
520+
}
504521
const module = await this.compile(specifier, {
505522
source: {
506523
code: sourceCode,
507-
type: getSourceType(specifier),
524+
type: sourceType,
508525
isStyle: false,
509526
}
510527
})
@@ -642,7 +659,7 @@ export class Aleph implements IAleph {
642659
}
643660

644661
/** create main bootstrap script in javascript. */
645-
createMainJS(bundleMode = false): string {
662+
async createMainJS(bundleMode = false): Promise<string> {
646663
const alephPkgUri = getAlephPkgUri()
647664
const alephPkgPath = alephPkgUri.replace('https://', '').replace('http://localhost:', 'http_localhost_')
648665
const { framework, basePath: basePath, i18n: { defaultLocale } } = this.#config
@@ -671,9 +688,9 @@ export class Aleph implements IAleph {
671688
`bootstrap(${JSON.stringify(config, undefined, this.isDev ? 2 : undefined)});`
672689
].filter(Boolean).join('\n')
673690
}
674-
this.#transformListeners.forEach(({ test, transform }) => {
691+
for (const { test, transform } of this.#transformListeners) {
675692
if (test === 'main') {
676-
const ret = transform({
693+
let ret = await transform({
677694
module: {
678695
specifier: '/main.js',
679696
deps: [],
@@ -683,10 +700,10 @@ export class Aleph implements IAleph {
683700
code,
684701
})
685702
if (util.isFilledString(ret?.code)) {
686-
code = ret.code
703+
code = ret!.code
687704
}
688705
}
689-
})
706+
}
690707
return code
691708
}
692709

@@ -919,16 +936,16 @@ export class Aleph implements IAleph {
919936
// todo: merge source map
920937
}
921938
}
922-
this.#transformListeners.forEach(({ test, transform }) => {
939+
for (const { test, transform } of this.#transformListeners) {
923940
if (test === 'hmr') {
924941
const { jsBuffer, ready, ...rest } = module
925-
const ret = transform({ module: structuredClone(rest), code })
942+
const ret = await transform({ module: rest, code })
926943
if (util.isFilledString(ret?.code)) {
927-
code = ret.code
944+
code = ret!.code
928945
}
929946
// todo: merge source map
930947
}
931-
})
948+
}
932949
return new TextEncoder().encode([
933950
`import.meta.hot = $createHotContext(${JSON.stringify(specifier)});`,
934951
'',
@@ -1216,18 +1233,18 @@ export class Aleph implements IAleph {
12161233
}
12171234
}
12181235

1219-
this.#transformListeners.forEach(({ test, transform }) => {
1236+
for (const { test, transform } of this.#transformListeners) {
12201237
if (test instanceof RegExp && test.test(specifier)) {
12211238
const { jsBuffer, ready, ...rest } = module
1222-
const { code, map } = transform({ module: structuredClone(rest), code: jsCode, map: sourceMap }) || {}
1223-
if (util.isFilledString(code)) {
1224-
jsCode = code
1239+
const ret = await transform({ module: rest, code: jsCode, map: sourceMap })
1240+
if (util.isFilledString(ret?.code)) {
1241+
jsCode = ret!.code
12251242
}
1226-
if (util.isFilledString(map)) {
1227-
sourceMap = map
1243+
if (util.isFilledString(ret?.map)) {
1244+
sourceMap = ret!.map
12281245
}
12291246
}
1230-
})
1247+
}
12311248

12321249
// add source mapping url
12331250
if (sourceMap) {

server/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class Server {
110110

111111
const relPath = util.trimPrefix(pathname, '/_aleph')
112112
if (relPath == '/main.js') {
113-
resp.body = aleph.createMainJS(false)
113+
resp.body = await aleph.createMainJS(false)
114114
resp.setHeader('Content-Type', 'application/javascript; charset=utf-8')
115115
resp.writeTo(e)
116116
return

types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface Aleph {
1010
fetchModule(specifier: string): Promise<{ content: Uint8Array, contentType: string | null }>
1111
onResolve(test: RegExp, resolve: (specifier: string) => ResolveResult): void
1212
onLoad(test: RegExp, load: (input: LoadInput) => LoadOutput | Promise<LoadOutput>): void
13-
onTransform(test: 'hmr' | 'main' | RegExp, transform: (input: TransformInput) => TransformOutput): void
13+
onTransform(test: 'hmr' | 'main' | RegExp, transform: (input: TransformInput) => TransformOutput | void | Promise<TransformOutput> | Promise<void>): void
1414
onSSR(callback: (input: SSRInput) => SSROutput): void
1515
}
1616

0 commit comments

Comments
 (0)