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

Commit 3c5c6a3

Browse files
committed
refactor: rewrite bundler(wip)
1 parent 9443b9d commit 3c5c6a3

File tree

1 file changed

+52
-34
lines changed

1 file changed

+52
-34
lines changed

server/bundler.ts

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,60 @@
1-
import { buildChecksum, TransformOptions, transformSync } from '../compiler/mod.ts'
1+
import { TransformOptions, transform } from '../compiler/mod.ts'
22
import type { ECMA } from '../deps.ts'
33
import { colors, minify, path } from '../deps.ts'
4-
import { defaultReactVersion } from "../shared/constants.ts"
4+
import { defaultReactVersion, hashShortLength } from '../shared/constants.ts'
55
import { existsFileSync, lazyRemove } from '../shared/fs.ts'
66
import log from '../shared/log.ts'
77
import util from '../shared/util.ts'
88
import { VERSION } from '../version.ts'
9-
import type { Application } from './app.ts'
10-
import { AlephRuntimeCode, clearCompilation, computeHash, getAlephPkgUri } from './helper.ts'
9+
import type { Application, Module } from './app.ts'
10+
import { AlephRuntimeCode, clearCompilation, computeHash, getAlephPkgUri, isLoaderPlugin, trimModuleExt } from './helper.ts'
1111

12-
/**
13-
* The Aleph Server Application class.
14-
*/
12+
/** The bundler class for aleph server. */
1513
export class Bundler {
16-
readonly #app: Application
14+
#app: Application
15+
#deps: Array<string>
1716

1817
constructor(app: Application) {
1918
this.#app = app
19+
this.#deps = []
2020
}
2121

22-
async bundle(entryMods: Set<string>, depMods: Map<string, boolean>) {
23-
console.log(entryMods)
24-
console.log(depMods)
22+
async bundle(entryMods: Array<string>, depMods: Map<string, boolean>) {
23+
this.#deps = Array.from(depMods.keys())
24+
await Promise.all([
25+
await this.createPolyfillBundle(),
26+
await this.createBundleChunk(
27+
'/deps.js',
28+
Array.from(depMods.entries())
29+
.filter(([url, exported]) => exported && util.isLikelyHttpURL(url))
30+
.map(([url]) => url)
31+
),
32+
await this.createBundleChunk(
33+
'/shared.js',
34+
Array.from(depMods.entries())
35+
.filter(([url, exported]) => exported && !util.isLikelyHttpURL(url))
36+
.map(([url]) => url)
37+
),
38+
...entryMods.map(async url => {
39+
await this.createBundleChunk(trimModuleExt(url) + '.js', [url])
40+
})
41+
])
42+
}
43+
44+
private async compile(module: Module) {
45+
const { content: sourceCode } = await this.#app.fetchModule(module.url)
46+
const { code, map, deps } = await transform(module.url, (new TextDecoder).decode(sourceCode), {
47+
importMap: this.#app.importMap,
48+
alephPkgUri: getAlephPkgUri(),
49+
reactVersion: defaultReactVersion,
50+
swcOptions: {
51+
target: 'es2020',
52+
sourceType: 'js'
53+
},
54+
bundleMode: true,
55+
bundleExternal: this.#deps,
56+
loaders: this.#app.config.plugins.filter(isLoaderPlugin)
57+
})
2558
}
2659

2760
async copyDist() {
@@ -59,27 +92,17 @@ export class Bundler {
5992
// ])
6093
}
6194

62-
/** transpile code without type check. */
63-
private async transpile(url: string, sourceCode: string, options: TransformOptions) {
64-
return transformSync(url, sourceCode, {
65-
...options,
66-
importMap: this.#app.importMap,
67-
alephPkgUri: getAlephPkgUri(),
68-
reactVersion: defaultReactVersion,
69-
})
70-
}
71-
7295
/** create polyfill bundle. */
7396
private async createPolyfillBundle() {
7497
const alephPkgUri = getAlephPkgUri()
7598
const { buildTarget } = this.#app.config
76-
const hash = computeHash(AlephRuntimeCode + buildTarget + buildChecksum + Deno.version.deno)
77-
const polyfillFile = path.join(this.#app.buildDir, `polyfill.bundle.${util.shortHash(hash)}.js`)
78-
if (!existsFileSync(polyfillFile)) {
79-
const rawPolyfillFile = `${alephPkgUri}/compiler/polyfills/${buildTarget}/polyfill.js`
80-
await this.runDenoBundle(rawPolyfillFile, polyfillFile, AlephRuntimeCode, true)
99+
const hash = computeHash(AlephRuntimeCode + buildTarget + Deno.version.deno + VERSION)
100+
const bundleFile = path.join(this.#app.buildDir, `polyfill.bundle.${hash.slice(0, hashShortLength)}.js`)
101+
if (!existsFileSync(bundleFile)) {
102+
const rawPolyfillFile = `${alephPkgUri}/compiler/polyfills/${buildTarget}/mod.ts`
103+
await this.runDenoBundle(rawPolyfillFile, bundleFile, AlephRuntimeCode, true)
81104
}
82-
log.info(` {} polyfill (${buildTarget.toUpperCase()}) ${colors.dim('• ' + util.formatBytes(Deno.statSync(polyfillFile).size))}`)
105+
log.info(` {} polyfill (${buildTarget.toUpperCase()}) ${colors.dim('• ' + util.formatBytes(Deno.statSync(bundleFile).size))}`)
83106
}
84107

85108
/** create bundle chunk. */
@@ -96,7 +119,7 @@ export class Bundler {
96119
}).flat().join('\n')
97120
const hash = computeHash(entryCode + VERSION + Deno.version.deno)
98121
const bundleEntryFile = path.join(this.#app.buildDir, `${name}.bundle.entry.js`)
99-
const bundleFile = path.join(this.#app.buildDir, `${name}.bundle.${util.shortHash(hash)}.js`)
122+
const bundleFile = path.join(this.#app.buildDir, `${name}.bundle.${hash.slice(0, hashShortLength)}.js`)
100123
if (!existsFileSync(bundleFile)) {
101124
await Deno.writeTextFile(bundleEntryFile, entryCode)
102125
await this.runDenoBundle(bundleEntryFile, bundleFile)
@@ -124,7 +147,7 @@ export class Bundler {
124147
// transpile bundle code to `buildTarget`
125148
const { buildTarget } = this.#app.config
126149

127-
let { code } = transformSync(
150+
let { code } = await transform(
128151
'/bundle.js',
129152
await Deno.readTextFile(bundleFile),
130153
{
@@ -135,11 +158,6 @@ export class Bundler {
135158
}
136159
)
137160

138-
// workaround for https://github.com/denoland/deno/issues/9212
139-
if (Deno.version.deno === '1.7.0' && bundleEntryFile.endsWith('deps.bundle.entry.js')) {
140-
code = code.replace(' _ = l.baseState, ', ' var _ = l.baseState, ')
141-
}
142-
143161
// IIFEify
144162
code = `(() => { ${header};${code} })()`
145163

0 commit comments

Comments
 (0)