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

Commit 5ea7323

Browse files
committed
refactor(server): alephPkgUrl -> alephModuleUrl
1 parent 9e9323e commit 5ea7323

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

server/app.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { Config, DependencyDescriptor, ImportMap, Module, RenderResult, Rou
1111
import { VERSION } from '../version.ts'
1212
import { Request } from './api.ts'
1313
import { defaultConfig, loadConfig } from './config.ts'
14-
import { AlephRuntimeCode, cleanupCompilation, computeHash, createHtml, formatBytesWithColor, getAlephPkgUrl, getRelativePath, reFullVersion, reHashJs, reHashResolve, respondErrorJSON } from './util.ts'
14+
import { AlephRuntimeCode, cleanupCompilation, computeHash, createHtml, formatBytesWithColor, getAlephModuleUrl, getRelativePath, reFullVersion, reHashJs, reHashResolve, respondErrorJSON } from './util.ts'
1515

1616
/**
1717
* The Aleph Server Application class.
@@ -349,7 +349,7 @@ export class Application {
349349
/** initialize project */
350350
private async init(reload: boolean) {
351351
const t = performance.now()
352-
const alephPkgUrl = getAlephPkgUrl()
352+
const alephModuleUrl = getAlephModuleUrl()
353353
const { env, framework, plugins, ssr } = this.config
354354
const walkOptions = { includeDirs: false, exts: moduleExts, skip: [/^\./, /\.d\.ts$/i, /\.(test|spec|e2e)\.m?(j|t)sx?$/i] }
355355
const apiDir = path.join(this.srcDir, 'api')
@@ -437,20 +437,20 @@ export class Application {
437437
}
438438

439439
// pre-compile framework modules
440-
await this.compile(`${alephPkgUrl}/framework/${framework}/bootstrap.ts`)
440+
await this.compile(`${alephModuleUrl}/framework/${framework}/bootstrap.ts`)
441441
if (this.isDev) {
442442
const mods = ['hmr.ts', 'nomodule.ts']
443443
for (const mod of mods) {
444-
await this.compile(`${alephPkgUrl}/framework/core/${mod}`)
444+
await this.compile(`${alephModuleUrl}/framework/core/${mod}`)
445445
}
446446
if (framework === 'react') {
447-
await this.compile(`${alephPkgUrl}/framework/react/refresh.ts`)
447+
await this.compile(`${alephModuleUrl}/framework/react/refresh.ts`)
448448
}
449449
}
450450

451451
// compile and import framework renderer when ssr is enable
452452
if (ssr) {
453-
const rendererUrl = `${alephPkgUrl}/framework/${framework}/renderer.ts`
453+
const rendererUrl = `${alephModuleUrl}/framework/${framework}/renderer.ts`
454454
await this.compile(rendererUrl)
455455
const { render } = await import('file://' + this.#modules.get(rendererUrl)!.jsFile)
456456
this.#renderer = { render }
@@ -579,7 +579,7 @@ export class Application {
579579

580580
/** get main js. */
581581
getMainJS(bundleMode = false): string {
582-
const alephPkgUrl = getAlephPkgUrl()
582+
const alephModuleUrl = getAlephModuleUrl()
583583
const { baseUrl, defaultLocale, framework } = this.config
584584
const config: Record<string, any> = {
585585
baseUrl,
@@ -600,10 +600,10 @@ export class Application {
600600
}
601601

602602
if (bundleMode) {
603-
return `var bootstrap=__ALEPH.pack["${alephPkgUrl}/framework/${framework}/bootstrap.ts"].default;bootstrap(${JSON.stringify(config)})`
603+
return `var bootstrap=__ALEPH.pack["${alephModuleUrl}/framework/${framework}/bootstrap.ts"].default;bootstrap(${JSON.stringify(config)})`
604604
}
605605

606-
const prefix = alephPkgUrl.replace('https://', '').replace('http://localhost:', 'http_localhost_')
606+
const prefix = alephModuleUrl.replace('https://', '').replace('http://localhost:', 'http_localhost_')
607607
return [
608608
(this.config.framework === 'react' && this.isDev) && `import "./-/${prefix}/framework/react/refresh.js"`,
609609
`import bootstrap from "./-/${prefix}/framework/${framework}/bootstrap.js"`,
@@ -711,7 +711,13 @@ export class Application {
711711
log.debug(`init compiler wasm in ${Math.round(performance.now() - t)}ms`)
712712
}
713713

714-
return transpileSync(sourceCode, options)
714+
return transpileSync(sourceCode, {
715+
...options,
716+
importMap: this.importMap,
717+
alephModuleUrl: getAlephModuleUrl(),
718+
reactVersion: this.config.reactVersion,
719+
isDev: this.isDev,
720+
})
715721
}
716722

717723
/** download and compile a moudle by given url, then cache on the disk. */
@@ -724,7 +730,7 @@ export class Application {
724730
bundledModules?: string[]
725731
}
726732
): Promise<Module> {
727-
const alephPkgUrl = getAlephPkgUrl()
733+
const alephModuleUrl = getAlephModuleUrl()
728734
const isRemote = util.isLikelyHttpURL(url)
729735
const localUrl = this.fixImportUrl(url)
730736
const name = util.trimModuleExt(path.basename(localUrl))
@@ -856,7 +862,7 @@ export class Application {
856862
if (loader === 'css') {
857863
const css = await this.preprocessCSS(sourceCode)
858864
sourceCode = [
859-
`import { applyCSS } from "${alephPkgUrl}/framework/core/style.ts";`,
865+
`import { applyCSS } from "${alephModuleUrl}/framework/core/style.ts";`,
860866
`export default function __applyCSS() {`,
861867
` applyCSS(${JSON.stringify(url)}, ${JSON.stringify(css)});`,
862868
`}`,
@@ -879,14 +885,12 @@ export class Application {
879885
sourceType: loader,
880886
sourceMap: this.isDev,
881887
}
888+
log.debug(url)
882889
const { code, map, deps, inlineStyles } = await this.transpile(sourceCode, {
883890
url,
884891
bundleMode,
885892
bundledModules,
886-
swcOptions,
887-
importMap: this.importMap,
888-
reactVersion: this.config.reactVersion,
889-
isDev: this.isDev,
893+
swcOptions
890894
})
891895

892896
fsync = true
@@ -1122,7 +1126,7 @@ export class Application {
11221126

11231127
/** bundle modules for production. */
11241128
private async bundle() {
1125-
const alephPkgUrl = getAlephPkgUrl()
1129+
const alephModuleUrl = getAlephModuleUrl()
11261130
const refCounter = new Map<string, number>()
11271131
const lookup = (url: string) => {
11281132
if (this.#modules.has(url)) {
@@ -1142,7 +1146,7 @@ export class Application {
11421146
const pageModules: Module[] = []
11431147

11441148
// add framework bootstrap
1145-
refCounter.set(`${alephPkgUrl}/framework/${this.config.framework}/bootstrap.ts`, 1)
1149+
refCounter.set(`${alephModuleUrl}/framework/${this.config.framework}/bootstrap.ts`, 1)
11461150
if (appModule) {
11471151
await this.compile(appModule.url, { bundleMode: true })
11481152
lookup(appModule.url)
@@ -1196,14 +1200,14 @@ export class Application {
11961200

11971201
/** create polyfill bundle. */
11981202
private async createPolyfillBundle() {
1199-
const alephPkgUrl = getAlephPkgUrl()
1203+
const alephModuleUrl = getAlephModuleUrl()
12001204
const { buildTarget } = this.config
12011205
const hash = computeHash(AlephRuntimeCode + buildTarget + buildChecksum + Deno.version.deno)
12021206
const polyfillFile = path.join(this.buildDir, `polyfill.bundle.${util.shortHash(hash)}.js`)
12031207
const polyfillMod = this.newModule('/polyfill.js')
12041208
polyfillMod.hash = polyfillMod.sourceHash = hash
12051209
if (!existsFileSync(polyfillFile)) {
1206-
const rawPolyfillFile = `${alephPkgUrl}/compiler/polyfills/${buildTarget}/polyfill.js`
1210+
const rawPolyfillFile = `${alephModuleUrl}/compiler/polyfills/${buildTarget}/polyfill.js`
12071211
await this.runDenoBundle(rawPolyfillFile, polyfillFile, AlephRuntimeCode, true)
12081212
}
12091213
log.info(` {} polyfill (${buildTarget.toUpperCase()}) ${colors.dim('• ' + util.formatBytes(Deno.statSync(polyfillFile).size))}`)

server/config.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { existsFileSync } from '../shared/fs.ts'
55
import log from '../shared/log.ts'
66
import util from '../shared/util.ts'
77
import type { Config, ImportMap } from '../types.ts'
8-
import { VERSION } from '../version.ts'
9-
import { reLocaleID } from './util.ts'
8+
import { getAlephModuleUrl, reLocaleID } from './util.ts'
109

1110
export const defaultConfig: Readonly<Required<Config>> = {
1211
framework: 'react',
@@ -159,12 +158,10 @@ export async function loadConfig(workingDir: string): Promise<[Config, ImportMap
159158
// update import map for alephjs dev env
160159
const DEV_PORT = Deno.env.get('ALEPH_DEV_PORT')
161160
if (DEV_PORT) {
162-
const alias = `http://localhost:${DEV_PORT}/`
161+
const alephModuleUrl = getAlephModuleUrl()
163162
const imports = {
164-
'https://deno.land/x/aleph/': alias,
165-
[`https://deno.land/x/aleph@v${VERSION}/`]: alias,
166-
'aleph': `${alias}mod.ts`,
167-
'aleph/': alias,
163+
'aleph': `${alephModuleUrl}/mod.ts`,
164+
'aleph/': `${alephModuleUrl}/`,
168165
'react': `https://esm.sh/react@${config.reactVersion}`,
169166
'react-dom': `https://esm.sh/react-dom@${config.reactVersion}`,
170167
}

server/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export const AlephRuntimeCode = `
4141
});
4242
`
4343

44-
/** get aleph pkg url. */
45-
export function getAlephPkgUrl() {
44+
/** get aleph module url. */
45+
export function getAlephModuleUrl() {
4646
const DEV_PORT = Deno.env.get('ALEPH_DEV_PORT')
4747
if (DEV_PORT) {
4848
return `http://localhost:${DEV_PORT}`

0 commit comments

Comments
 (0)