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

Commit 4e3b1ef

Browse files
committed
fix: workaround for denoland/deno#9849
1 parent 1d0cef7 commit 4e3b1ef

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

server/app.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { buildChecksum, ImportMap, SourceType, transform, TransformOptions } from '../compiler/mod.ts'
1+
import { buildChecksum, ImportMap, parseExportNames, SourceType, transform, TransformOptions } from '../compiler/mod.ts'
22
import { colors, createHash, ensureDir, path, walk } from '../deps.ts'
33
import { EventEmitter } from '../framework/core/events.ts'
44
import { moduleExts, toPagePath, trimModuleExt } from '../framework/core/module.ts'
5-
import { Routing, RouteModule } from '../framework/core/routing.ts'
5+
import { RouteModule, Routing } from '../framework/core/routing.ts'
66
import { defaultReactVersion, minDenoVersion } from '../shared/constants.ts'
77
import {
88
ensureTextFile,
@@ -73,12 +73,16 @@ export class Application implements ServerApplication {
7373

7474
/** initiate application */
7575
private async init(reload: boolean) {
76-
const t = performance.now()
76+
let t = performance.now()
77+
7778
const [config, importMap] = await Promise.all([
7879
loadConfig(this.workingDir),
7980
loadImportMap(this.workingDir)
8081
])
8182

83+
log.debug(`load config in ${Math.round(performance.now() - t)}ms`)
84+
t = performance.now()
85+
8286
Object.assign(this.config, config)
8387
Object.assign(this.importMap, importMap)
8488
this.#pageRouting.config(this.config)
@@ -538,7 +542,7 @@ export class Application implements ServerApplication {
538542
}
539543

540544
/** get ssr html scripts */
541-
getSSRHTMLScripts() {
545+
getSSRHTMLScripts(pagePath?: string) {
542546
const { baseUrl } = this.config
543547

544548
if (this.isDev) {
@@ -550,8 +554,8 @@ export class Application implements ServerApplication {
550554

551555
return [
552556
bundlerRuntimeCode,
553-
...['polyfill', 'deps', 'shared', 'main']
554-
.filter(name => this.#bundler.getBundledFile(name) !== null)
557+
...['polyfill', 'deps', 'shared', 'main', pagePath ? '/pages' + pagePath.replace(/\/$/, '/index') : '']
558+
.filter(name => name !== "" && this.#bundler.getBundledFile(name) !== null)
555559
.map(name => ({
556560
src: util.cleanPath(`${baseUrl}/_aleph/${this.#bundler.getBundledFile(name)}`)
557561
}))
@@ -939,12 +943,14 @@ export class Application implements ServerApplication {
939943

940944
const t = performance.now()
941945
const [sourceCode, sourceType] = source
942-
const { code, map, deps } = await transform(url, sourceCode, {
946+
const { code, deps, starExports, map } = await transform(url, sourceCode, {
943947
...this.defaultCompileOptions,
944948
swcOptions: {
945949
target: 'es2020',
946950
sourceType
947951
},
952+
// workaround for https://github.com/denoland/deno/issues/9849
953+
resolveStarExports: !this.isDev && Deno.version.deno.replace(/\.\d+$/, '') === '1.8',
948954
sourceMap: this.isDev,
949955
loaders: this.config.plugins.filter(isLoaderPlugin)
950956
})
@@ -955,6 +961,16 @@ export class Application implements ServerApplication {
955961
jsSourceMap = map
956962
}
957963

964+
// workaround for https://github.com/denoland/deno/issues/9849
965+
if (starExports && starExports.length > 0) {
966+
for (let index = 0; index < starExports.length; index++) {
967+
const url = starExports[index]
968+
const [sourceCode, sourceType] = await this.resolveModule(url)
969+
const names = await parseExportNames(url, sourceCode, { sourceType })
970+
jsContent = jsContent.replace(`export * from "${url}:`, `export {${names.filter(name => name !== 'default').join(',')}} from "`)
971+
}
972+
}
973+
958974
mod.deps = deps.map(({ specifier, isDynamic }) => {
959975
const dep: DependencyDescriptor = { url: specifier, hash: '' }
960976
if (isDynamic) {

server/bundler.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ECMA, minify as terser } from 'https://esm.sh/[email protected]'
2-
import { transform, parseExportNames } from '../compiler/mod.ts'
2+
import { parseExportNames, transform } from '../compiler/mod.ts'
33
import { colors, ensureDir, path } from '../deps.ts'
44
import { trimModuleExt } from '../framework/core/module.ts'
55
import { defaultReactVersion } from '../shared/constants.ts'
@@ -136,7 +136,7 @@ export class Bundler {
136136
}
137137

138138
const [sourceCode, sourceType] = source
139-
let { code, bundleStarExports } = await transform(
139+
let { code, starExports } = await transform(
140140
mod.url,
141141
sourceCode,
142142
{
@@ -153,9 +153,9 @@ export class Bundler {
153153
}
154154
)
155155

156-
if (bundleStarExports && bundleStarExports.length > 0) {
157-
for (let index = 0; index < bundleStarExports.length; index++) {
158-
const url = bundleStarExports[index]
156+
if (starExports && starExports.length > 0) {
157+
for (let index = 0; index < starExports.length; index++) {
158+
const url = starExports[index]
159159
const [sourceCode, sourceType] = await this.#app.resolveModule(url)
160160
const names = await parseExportNames(url, sourceCode, { sourceType })
161161
code = code.replace(`export const $$star_${index}`, `export const {${names.filter(name => name !== 'default').join(',')}}`)
@@ -284,10 +284,10 @@ export class Bundler {
284284

285285
// minify code
286286
// todo: use swc minify instead(https://github.com/swc-project/swc/pull/1302)
287-
// const mini = await minify(code, parseInt(util.trimPrefix(buildTarget, 'es')) as ECMA)
288-
// if (mini !== undefined) {
289-
// code = mini
290-
// }
287+
const mini = await minify(code, parseInt(util.trimPrefix(buildTarget, 'es')) as ECMA)
288+
if (mini !== undefined) {
289+
code = mini
290+
}
291291

292292
await clearCompilation(bundleFile)
293293
await Deno.writeTextFile(bundleFile, code)

server/ssr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class Renderer {
7171
type: 'application/json',
7272
innerText: JSON.stringify(data, undefined, isDev ? 2 : 0),
7373
} : '',
74-
...this.#app.getSSRHTMLScripts(),
74+
...this.#app.getSSRHTMLScripts(url.pagePath),
7575
...scripts.map((script: Record<string, any>) => {
7676
if (script.innerText && !this.#app.isDev) {
7777
return { ...script, innerText: script.innerText }

0 commit comments

Comments
 (0)