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

Commit 91fa5aa

Browse files
author
Je
committed
refactor: clean up
1 parent 774d722 commit 91fa5aa

File tree

3 files changed

+77
-50
lines changed

3 files changed

+77
-50
lines changed

project.ts

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,26 @@ export class Project {
122122
}
123123

124124
isHMRable(moduleID: string) {
125-
return !reHttp.test(moduleID) && (
126-
moduleID === '/404.js' ||
127-
moduleID === '/app.js' ||
128-
moduleID.startsWith('/pages/') ||
129-
moduleID.startsWith('/components/') ||
130-
reStyleModuleExt.test(moduleID)
131-
)
125+
if (reHttp.test(moduleID)) {
126+
return false
127+
}
128+
if (reStyleModuleExt.test(moduleID)) {
129+
return true
130+
}
131+
if (reModuleExt.test(moduleID)) {
132+
return moduleID === '/404.js' ||
133+
moduleID === '/app.js' ||
134+
moduleID.startsWith('/pages/') ||
135+
moduleID.startsWith('/components/')
136+
}
137+
if (reMDExt.test(moduleID)) {
138+
return moduleID.startsWith('/pages/')
139+
}
140+
const plugin = this.config.plugins.find(p => p.test.test(moduleID))
141+
if (plugin?.acceptHMR) {
142+
return true
143+
}
144+
return false
132145
}
133146

134147
isSSRable(pathname: string): boolean {
@@ -555,6 +568,18 @@ export class Project {
555568
await ensureDir(this.buildDir)
556569
}
557570

571+
// import postcss plugins
572+
await Promise.all(this.config.postcss.plugins.map(async p => {
573+
let name: string
574+
if (typeof p === 'string') {
575+
name = p
576+
} else {
577+
name = p.name
578+
}
579+
const { default: Plugin } = await import(`https://esm.sh/${name}[email protected]&no-check`)
580+
this.#postcssPlugins[name] = Plugin
581+
}))
582+
558583
// inject ALEPH global variable
559584
Object.assign(globalThis, {
560585
ALEPH: {
@@ -571,18 +596,6 @@ export class Project {
571596
// change current work dir to appDoot
572597
Deno.chdir(this.appRoot)
573598

574-
// import postcss plugins
575-
await Promise.all(this.config.postcss.plugins.map(async p => {
576-
let name: string
577-
if (typeof p === 'string') {
578-
name = p
579-
} else {
580-
name = p.name
581-
}
582-
const { default: Plugin } = await import(`https://esm.sh/${name}[email protected]&no-check`)
583-
this.#postcssPlugins[name] = Plugin
584-
}))
585-
586599
for await (const { path: p, } of walk(this.srcDir, { ...walkOptions, maxDepth: 1, exts: [...walkOptions.exts, '.jsx', '.tsx'] })) {
587600
const name = path.basename(p)
588601
switch (name.replace(reModuleExt, '')) {
@@ -825,16 +838,16 @@ export class Project {
825838
const metaFile = path.join(this.buildDir, 'main.meta.json')
826839

827840
module.jsContent = [
828-
this.isDev && 'import "./-/deno.land/x/aleph/hmr.js";',
829-
'import "./-/deno.land/x/aleph/aleph.js";',
830-
'import "./-/deno.land/x/aleph/context.js";',
831-
'import "./-/deno.land/x/aleph/error.js";',
832-
'import "./-/deno.land/x/aleph/events.js";',
833-
'import "./-/deno.land/x/aleph/routing.js";',
834-
'import "./-/deno.land/x/aleph/util.js";',
835-
'import bootstrap from "./-/deno.land/x/aleph/bootstrap.js";',
836-
`bootstrap(${JSON.stringify(config, undefined, this.isDev ? 4 : undefined)});`
837-
].filter(Boolean).join(this.isDev ? '\n' : '')
841+
this.isDev && 'import "./-/deno.land/x/aleph/hmr.js"',
842+
'import "./-/deno.land/x/aleph/aleph.js"',
843+
'import "./-/deno.land/x/aleph/context.js"',
844+
'import "./-/deno.land/x/aleph/error.js"',
845+
'import "./-/deno.land/x/aleph/events.js"',
846+
'import "./-/deno.land/x/aleph/routing.js"',
847+
'import "./-/deno.land/x/aleph/util.js"',
848+
'import bootstrap from "./-/deno.land/x/aleph/bootstrap.js"',
849+
`bootstrap(${JSON.stringify(config, undefined, this.isDev ? 4 : undefined)})`
850+
].filter(Boolean).join(this.isDev ? '\n' : ';')
838851
module.hash = getHash(module.jsContent)
839852
module.jsFile = path.join(this.buildDir, `main.${module.hash.slice(0, hashShort)}.js`)
840853
module.deps = [
@@ -1045,7 +1058,7 @@ export class Project {
10451058
].filter(Boolean).map(l => !this.isDev ? String(l).trim() : l).join(this.isDev ? '\n' : '')
10461059
mod.jsSourceMap = ''
10471060
mod.hash = getHash(mod.jsContent)
1048-
} else if (mod.loader === 'js') {
1061+
} else if (mod.loader === 'js' || mod.loader === 'ts' || mod.loader === 'jsx' || mod.loader === 'tsx') {
10491062
const useDenos: string[] = []
10501063
const compileOptions = {
10511064
mode: this.mode,
@@ -1291,22 +1304,6 @@ export class Project {
12911304
}
12921305
}
12931306
const ret: RenderResult = { url, status: url.pagePath === '' ? 404 : 200, head: [], scripts: [], body: '<main></main>', data: null }
1294-
Object.assign(window, {
1295-
location: {
1296-
protocol: 'http:',
1297-
host: 'localhost',
1298-
hostname: 'localhost',
1299-
port: '',
1300-
href: 'https://localhost' + url.pathname + url.query.toString(),
1301-
origin: 'https://localhost',
1302-
pathname: url.pathname,
1303-
search: url.query.toString(),
1304-
hash: '',
1305-
reload() { },
1306-
replace() { },
1307-
toString() { return this.href },
1308-
}
1309-
})
13101307
if (ret.status === 404) {
13111308
if (this.isDev) {
13121309
log.warn(`page '${url.pathname}' not found`)
@@ -1420,6 +1417,20 @@ export class Project {
14201417
// add virtual browser global objects
14211418
Object.assign(globalThis, {
14221419
document: new Document(),
1420+
location: {
1421+
protocol: 'http:',
1422+
host: 'localhost',
1423+
hostname: 'localhost',
1424+
port: '',
1425+
href: 'https://localhost/',
1426+
origin: 'https://localhost',
1427+
pathname: '/',
1428+
search: '',
1429+
hash: '',
1430+
reload() { },
1431+
replace() { },
1432+
toString() { return this.href },
1433+
},
14231434
innerWidth: 1920,
14241435
innerHeight: 1080,
14251436
devicePixelRatio: 1,

project_test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
2-
import { Project } from './project.ts';
3-
import { path } from './std.ts';
1+
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts'
2+
import { Project } from './project.ts'
3+
import { path } from './std.ts'
44

55
Deno.test({
66
name: 'project build(hello world)',

renderer.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,23 @@ export async function renderPage(
114114
data[id] = ret
115115
}
116116
})
117-
Object.assign(window, { [`__asyncData_${useDenEvent}`]: {} })
117+
Object.assign(window, {
118+
[`__asyncData_${useDenEvent}`]: {},
119+
location: {
120+
protocol: 'http:',
121+
host: 'localhost',
122+
hostname: 'localhost',
123+
port: '',
124+
href: 'https://localhost' + url.pathname + url.query.toString(),
125+
origin: 'https://localhost',
126+
pathname: url.pathname,
127+
search: url.query.toString(),
128+
hash: '',
129+
reload() { },
130+
replace() { },
131+
toString() { return this.href },
132+
}
133+
})
118134
while (true) {
119135
try {
120136
if (useDenoAsyncCalls.length > 0) {

0 commit comments

Comments
 (0)