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

Commit 20cc19e

Browse files
author
Je
committed
fix: fix getRelativePath function
1 parent 29d2583 commit 20cc19e

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

project.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ interface RenderResult {
5151
}
5252

5353
/**
54-
* A Project to manage the Aleph.js appliaction, features include:
55-
* - compile source files
54+
* A Project to manage the Aleph.js appliaction.
55+
* features include:
56+
* - compile source codes
5657
* - manage deps
5758
* - apply plugins
5859
* - watch file changes
5960
* - call APIs
6061
* - SSR/SSG
6162
*/
6263
export class Project {
63-
readonly mode: 'development' | 'production'
6464
readonly appRoot: string
65+
readonly mode: 'development' | 'production'
6566
readonly config: Readonly<Required<Config>> & { __file?: string }
6667
readonly importMap: Readonly<{ imports: Record<string, string> }>
6768
readonly ready: Promise<void>
@@ -74,9 +75,9 @@ export class Project {
7475
#rendered: Map<string, Map<string, RenderResult>> = new Map()
7576
#postcssPlugins: Record<string, AcceptedPlugin> = {}
7677

77-
constructor(dir: string, mode: 'development' | 'production', reload = false) {
78+
constructor(appDir: string, mode: 'development' | 'production', reload = false) {
79+
this.appRoot = path.resolve(appDir)
7880
this.mode = mode
79-
this.appRoot = dir
8081
this.config = {
8182
srcDir: '/',
8283
outputDir: '/dist',
@@ -298,7 +299,7 @@ export class Project {
298299
const outputDir = path.join(this.srcDir, this.config.outputDir)
299300
const distDir = path.join(outputDir, '_aleph')
300301
const outputModules = new Set<string>()
301-
const lookup = async (moduleID: string) => {
302+
const lookup = (moduleID: string) => {
302303
if (this.#modules.has(moduleID) && !outputModules.has(moduleID)) {
303304
outputModules.add(moduleID)
304305
const { deps } = this.#modules.get(moduleID)!
@@ -380,7 +381,7 @@ export class Project {
380381
// copy public assets
381382
const publicDir = path.join(this.appRoot, 'public')
382383
if (existsDirSync(publicDir)) {
383-
log.info(colors.bold(' Public Assets'))
384+
log.info(colors.bold('- Public Assets'))
384385
for await (const { path: p } of walk(publicDir, { includeDirs: false, skip: [/\/\.[^\/]+($|\/)/] })) {
385386
const rp = util.trimPrefix(p, publicDir)
386387
const fp = path.join(outputDir, rp)
@@ -536,17 +537,6 @@ export class Project {
536537
Object.assign(this, { buildID: this.mode + '.' + this.config.buildTarget })
537538
// update routing
538539
this.#routing = new Routing([], this.config.baseUrl, this.config.defaultLocale, this.config.locales)
539-
// inject ALEPH global variable
540-
Object.assign(globalThis, {
541-
ALEPH: {
542-
ENV: {
543-
...this.config.env,
544-
__version: version,
545-
__buildMode: this.mode,
546-
__buildTarget: this.config.buildTarget,
547-
} as AlephEnv
548-
}
549-
})
550540
}
551541

552542
private async _init(reload: boolean) {
@@ -565,6 +555,18 @@ export class Project {
565555
await ensureDir(this.buildDir)
566556
}
567557

558+
// inject ALEPH global variable
559+
Object.assign(globalThis, {
560+
ALEPH: {
561+
ENV: {
562+
...this.config.env,
563+
__version: version,
564+
__buildMode: this.mode,
565+
__buildTarget: this.config.buildTarget,
566+
} as AlephEnv
567+
}
568+
})
569+
568570
// change current work dir to appDoot
569571
Deno.chdir(this.appRoot)
570572

@@ -1468,7 +1470,7 @@ export function injectHmr({ id, sourceFilePath, jsContent }: Module): string {
14681470

14691471
/** get relative the path of `to` to `from` */
14701472
function getRelativePath(from: string, to: string): string {
1471-
let r = path.relative(from, to)
1473+
let r = path.relative(from, to).split('\\').join('/')
14721474
if (!r.startsWith('.') && !r.startsWith('/')) {
14731475
r = './' + r
14741476
}

project_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
2+
import { Project } from './project.ts';
3+
import { path } from './std.ts';
4+
5+
Deno.test('project build(hello world)', async () => {
6+
const output: string[] = []
7+
const appDir = path.resolve('./examples/hello-world')
8+
const project = new Project(appDir, 'production')
9+
await project.build()
10+
for await (const entry of Deno.readDir(path.resolve(appDir, 'dist'))) {
11+
output.push(entry.name)
12+
}
13+
output.sort()
14+
assertEquals(output, [
15+
'404.html',
16+
'_aleph',
17+
'_fallback.html',
18+
'favicon.ico',
19+
'index.html',
20+
'logo.svg'
21+
])
22+
})

0 commit comments

Comments
 (0)