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

Commit 98c6ac6

Browse files
committed
Improve bootstrap time
1 parent 3e71032 commit 98c6ac6

File tree

4 files changed

+33
-35
lines changed

4 files changed

+33
-35
lines changed

bundler/mod.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { dirname, join } from 'https://deno.land/[email protected]/path/mod.ts'
22
import { ensureDir } from 'https://deno.land/[email protected]/fs/ensure_dir.ts'
3-
import { transform } from '../compiler/mod.ts'
3+
import { SourceType, transform } from '../compiler/mod.ts'
44
import { trimBuiltinModuleExts } from '../framework/core/module.ts'
5-
import { ensureTextFile, existsFile, lazyRemove } from '../shared/fs.ts'
6-
import type { BrowserName, Module } from '../types.d.ts'
7-
import { VERSION } from '../version.ts'
5+
import { cssLoader } from '../plugins/css.ts'
86
import type { DependencyGraph } from '../server/analyzer.ts'
97
import type { Aleph } from '../server/aleph.ts'
108
import { clearBuildCache, computeHash, getAlephPkgUri } from '../server/helper.ts'
9+
import { ensureTextFile, existsFile, lazyRemove } from '../shared/fs.ts'
10+
import type { BrowserName, Module } from '../types.d.ts'
11+
import { VERSION } from '../version.ts'
1112
import { esbuild, stopEsbuild, esmLoader } from './esbuild.ts'
1213

1314
export const bundlerRuntimeCode = `
@@ -134,12 +135,22 @@ export class Bundler {
134135
return jsFile
135136
}
136137

137-
const source = await this.#aleph.resolveModuleSource(mod.specifier)
138+
let source = await this.#aleph.resolveModuleSource(mod.specifier)
138139
if (source === null) {
139140
this.#compiled.delete(mod.specifier)
140141
throw new Error(`Unsupported module '${mod.specifier}'`)
141142
}
142143

144+
if (source.type === SourceType.CSS) {
145+
const { code, map } = await cssLoader({ specifier: mod.specifier, data: source.code }, this.#aleph)
146+
source = {
147+
type: SourceType.JS,
148+
code,
149+
map
150+
}
151+
mod.isStyle = true
152+
}
153+
143154
try {
144155
let { code, starExports } = await transform(
145156
mod.specifier,

plugins/css.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ export default (): Plugin => {
206206
name: 'css-loader',
207207
setup: aleph => {
208208
aleph.onResolve(test, () => ({ acceptHMR: true }))
209-
aleph.onLoad(test, input => cssLoader(input, aleph))
210209
}
211210
}
212211
}

server/aleph.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { delay } from 'https://deno.land/[email protected]/async/delay.ts'
21
import { dim } from 'https://deno.land/[email protected]/fmt/colors.ts'
32
import { indexOf, copy, equals } from 'https://deno.land/[email protected]/bytes/mod.ts'
43
import { ensureDir } from 'https://deno.land/[email protected]/fs/ensure_dir.ts'
@@ -31,7 +30,6 @@ import { createHtml, Renderer } from './renderer.ts'
3130
type ModuleSource = {
3231
code: string
3332
type: SourceType
34-
isStyle: boolean
3533
map?: string
3634
}
3735

@@ -523,21 +521,13 @@ export class Aleph implements IAleph {
523521
/** add a module by given path and optional source code. */
524522
async addModule(specifier: string, sourceCode: string): Promise<Module> {
525523
let sourceType = getSourceType(specifier)
526-
let isStyle = false
527524
if (sourceType === SourceType.Unknown) {
528525
throw new Error("addModule: unknown souce type")
529526
}
530-
if (sourceType === SourceType.CSS) {
531-
const ret = await cssLoader({ specifier, data: (new TextEncoder).encode(sourceCode) }, this)
532-
sourceCode = ret.code
533-
sourceType = SourceType.JS
534-
isStyle = true
535-
}
536527
const module = await this.compile(specifier, {
537528
source: {
538529
code: sourceCode,
539530
type: sourceType,
540-
isStyle,
541531
}
542532
})
543533
if (specifier.startsWith('pages/') || specifier.startsWith('api/')) {
@@ -1047,20 +1037,9 @@ export class Aleph implements IAleph {
10471037
}
10481038
}
10491039

1050-
if (sourceType === SourceType.CSS) {
1051-
isStyle = true
1052-
// todo: covert source map
1053-
const { code, type = 'js' } = await cssLoader({ specifier, data: sourceCode }, this)
1054-
if (type === 'js') {
1055-
sourceCode = code
1056-
sourceType = SourceType.JS
1057-
}
1058-
}
1059-
10601040
return {
10611041
code: sourceCode,
10621042
type: sourceType,
1063-
isStyle,
10641043
map: sourceMap ? sourceMap : undefined
10651044
}
10661045
}
@@ -1074,7 +1053,11 @@ export class Aleph implements IAleph {
10741053
return module
10751054
}
10761055

1077-
private async initModule(specifier: string, { source: customSource, forceRefresh, httpExternal }: CompileOptions = {}): Promise<[Module, ModuleSource | null]> {
1056+
/** init the module by given specifier, don't transpile the code when the returned `source` is equal to null */
1057+
private async initModule(
1058+
specifier: string,
1059+
{ source: customSource, forceRefresh, httpExternal }: CompileOptions = {}
1060+
): Promise<[Module, ModuleSource | null]> {
10781061
let external = false
10791062
let data: any = null
10801063

@@ -1161,19 +1144,14 @@ export class Aleph implements IAleph {
11611144
} catch (e) { }
11621145
}
11631146

1164-
const shouldLoad = !(
1165-
(isRemote && !this.#reloading && mod.sourceHash !== '') &&
1166-
await existsFile(cacheFp)
1167-
)
1168-
if (shouldLoad) {
1147+
if (!isRemote || this.#reloading || mod.sourceHash === '' || !await existsFile(cacheFp)) {
11691148
try {
11701149
const src = customSource || await this.resolveModuleSource(specifier, data)
11711150
const sourceHash = computeHash(src.code)
11721151
if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) {
11731152
mod.sourceHash = sourceHash
11741153
source = src
11751154
}
1176-
mod.isStyle = src.isStyle
11771155
} catch (err) {
11781156
defer(err)
11791157
return [mod, null]
@@ -1205,6 +1183,14 @@ export class Aleph implements IAleph {
12051183
return
12061184
}
12071185

1186+
if (source.type === SourceType.CSS) {
1187+
const { code, map } = await cssLoader({ specifier, data: source.code }, this)
1188+
source.code = code
1189+
source.map = map
1190+
source.type = SourceType.JS
1191+
module.isStyle = true
1192+
}
1193+
12081194
const ms = new Measure()
12091195
const encoder = new TextEncoder()
12101196
const { code, deps = [], denoHooks, ssrPropsFn, ssgPathsFn, starExports, jsxStaticClassNames, map } = await transform(specifier, source.code, {
@@ -1318,7 +1304,7 @@ export class Aleph implements IAleph {
13181304
}
13191305
depModule = mod
13201306
}
1321-
if (depModule) {
1307+
if (!ignoreDeps && depModule) {
13221308
const hash = depModule.hash || depModule.sourceHash
13231309
if (hashLoc !== undefined) {
13241310
if (await this.replaceDepHash(module, hashLoc, hash)) {

server/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export class Server {
122122
return
123123
}
124124

125+
// serve modules
125126
if (relPath.endsWith('.js')) {
126127
let module = aleph.findModule(({ jsFile }) => jsFile === relPath)
127128
if (!module && aleph.isDev) {
@@ -151,6 +152,7 @@ export class Server {
151152
}
152153
}
153154

155+
// serve other build files
154156
const filePath = join(aleph.buildDir, relPath)
155157
if (await existsFile(filePath)) {
156158
const info = Deno.lstatSync(filePath)

0 commit comments

Comments
 (0)