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

Commit d532bd1

Browse files
committed
fix(server): fix _createPageBundle method
1 parent 3059b8a commit d532bd1

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

server/project.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { VERSION } from '../version.ts'
1010
import { Request } from './api.ts'
1111
import log from './log.ts'
1212
import type { DependencyDescriptor, ImportMap, Module, RenderResult } from './types.ts'
13-
import { cleanupCompilation, colorfulBytesString, createHtml, ensureTextFile, existsDirSync, existsFileSync, fixImportMap, fixImportUrl, getAlephPkgUrl, getRelativePath, newModule } from './util.ts'
13+
import { AlephRuntimeCode, cleanupCompilation, colorfulBytesString, createHtml, ensureTextFile, existsDirSync, existsFileSync, fixImportMap, fixImportUrl, getAlephPkgUrl, getRelativePath, newModule } from './util.ts'
1414

1515
/**
1616
* A Project to manage the Aleph.js appliaction.
@@ -1262,28 +1262,6 @@ export class Project {
12621262

12631263
/** bundle modules for production. */
12641264
private async _bundle() {
1265-
const header = `
1266-
var __ALEPH = window.__ALEPH || (window.__ALEPH = {
1267-
pack: {},
1268-
exportFrom: function(specifier, url, exports) {
1269-
if (url in this.pack) {
1270-
var mod = this.pack[url]
1271-
if (!(specifier in this.pack)) {
1272-
this.pack[specifier] = {}
1273-
}
1274-
if (exports === '*') {
1275-
for (var k in mod) {
1276-
this.pack[specifier][k] = mod[k]
1277-
}
1278-
} else if (typeof exports === 'object' && exports !== null) {
1279-
for (var k in exports) {
1280-
this.pack[specifier][exports[k]] = mod[k]
1281-
}
1282-
}
1283-
}
1284-
}
1285-
});
1286-
`.replaceAll(' '.repeat(12), '')
12871265
const alephPkgUrl = getAlephPkgUrl()
12881266
const refCounter = new Map<string, number>()
12891267
const lookup = (url: string) => {
@@ -1340,9 +1318,9 @@ export class Project {
13401318
}
13411319

13421320
log.info('- Bundle')
1343-
await this._createChunkBundle('deps', remoteDepList, header)
1321+
await this._createChunkBundle('deps', remoteDepList)
13441322
if (localDepList.length > 0) {
1345-
await this._createChunkBundle('shared', localDepList, header)
1323+
await this._createChunkBundle('shared', localDepList)
13461324
}
13471325

13481326
// copy main module
@@ -1353,17 +1331,17 @@ export class Project {
13531331

13541332
// create and copy polyfill
13551333
const polyfillMode = newModule('/polyfill.js')
1356-
polyfillMode.hash = polyfillMode.sourceHash = (new Sha1).update(header).update(`${this.config.buildTarget}-${VERSION}`).hex()
1334+
polyfillMode.hash = polyfillMode.sourceHash = (new Sha1).update(AlephRuntimeCode).update(`${this.config.buildTarget}-${VERSION}`).hex()
13571335
const polyfillFile = path.join(this.buildDir, `polyfill.${polyfillMode.hash.slice(0, hashShort)}.js`)
13581336
if (!existsFileSync(polyfillFile)) {
13591337
const rawPolyfillFile = `${alephPkgUrl}/compiler/polyfills/${this.config.buildTarget}/polyfill.js`
1360-
await this._runDenoBundle(rawPolyfillFile, polyfillFile, header, true)
1338+
await this._runDenoBundle(rawPolyfillFile, polyfillFile, AlephRuntimeCode, true)
13611339
}
13621340
Deno.copyFile(polyfillFile, path.join(this.outputDir, '_aleph', `polyfill.${polyfillMode.hash.slice(0, hashShort)}.js`))
13631341
this.#modules.set(polyfillMode.url, polyfillMode)
13641342

13651343
// bundle and copy page moudles
1366-
await Promise.all(pageModules.map(async mod => this._createPageBundle(mod, localDepList, header)))
1344+
await Promise.all(pageModules.map(async mod => this._createPageBundle(mod, localDepList, AlephRuntimeCode)))
13671345
}
13681346

13691347
/** create chunk bundle. */
@@ -1403,11 +1381,11 @@ export class Project {
14031381

14041382
/** create page bundle. */
14051383
private async _createPageBundle(mod: Module, bundledPaths: string[], header = '') {
1406-
const { bundlingFile, sourceHash } = await this._compile(mod.url, { bundleMode: true, bundledPaths })
1384+
const { bundlingFile, hash } = await this._compile(mod.url, { bundleMode: true, bundledPaths })
14071385
const _tmp = util.trimSuffix(bundlingFile.replace(reHashJs, ''), '.bundling')
14081386
const _tmp_bundlingFile = _tmp + `.bundling.js`
1409-
const bundleFile = _tmp + `.bundle.${sourceHash.slice(0, hashShort)}.js`
1410-
const saveAs = path.join(this.outputDir, `/_aleph/`, util.trimPrefix(_tmp, this.buildDir) + `.${sourceHash.slice(0, hashShort)}.js`)
1387+
const bundleFile = _tmp + `.bundle.${hash.slice(0, hashShort)}.js`
1388+
const saveAs = path.join(this.outputDir, `/_aleph/`, util.trimPrefix(_tmp, this.buildDir) + `.${hash.slice(0, hashShort)}.js`)
14111389

14121390
if (existsFileSync(bundleFile)) {
14131391
await ensureDir(path.dirname(saveAs))

server/util.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
import { colors, ensureDir, path } from '../deps.ts'
2-
import {
3-
MB,
4-
reHashJs,
5-
reHttp,
6-
reMDExt,
7-
reModuleExt,
8-
reStyleModuleExt
9-
} from '../shared/constants.ts'
2+
import { MB, reHashJs, reHttp, reMDExt, reModuleExt, reStyleModuleExt } from '../shared/constants.ts'
103
import util from '../shared/util.ts'
114
import { VERSION } from '../version.ts'
125
import { ImportMap, Module } from './types.ts'
136

7+
export const AlephRuntimeCode = `
8+
var __ALEPH = window.__ALEPH || (window.__ALEPH = {
9+
pack: {},
10+
exportFrom: function(specifier, url, exports) {
11+
if (url in this.pack) {
12+
var mod = this.pack[url]
13+
if (!(specifier in this.pack)) {
14+
this.pack[specifier] = {}
15+
}
16+
if (exports === '*') {
17+
for (var k in mod) {
18+
this.pack[specifier][k] = mod[k]
19+
}
20+
} else if (typeof exports === 'object' && exports !== null) {
21+
for (var k in exports) {
22+
this.pack[specifier][exports[k]] = mod[k]
23+
}
24+
}
25+
}
26+
},
27+
require: function(name) {
28+
switch (name) {
29+
case 'regenerator-runtime':
30+
return regeneratorRuntime
31+
default:
32+
throw new Error("module name is undefined")
33+
}
34+
},
35+
});
36+
`.replaceAll(' '.repeat(12), '')
37+
1438
export function getAlephPkgUrl() {
1539
let url = `https://deno.land/x/aleph@v${VERSION}`
1640
const { __ALEPH_DEV_PORT: devPort } = globalThis as any
@@ -184,7 +208,7 @@ export function fixImportUrl(importUrl: string): string {
184208
].join('')
185209
}
186210
const result = pathname + ext
187-
return !isRemote && importUrl.startsWith('/api/') ? decodeURI(result) : result;
211+
return !isRemote && importUrl.startsWith('/api/') ? decodeURI(result) : result
188212
}
189213

190214
/**

0 commit comments

Comments
 (0)