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

Commit 4bb6c82

Browse files
author
Je
committed
refactor: cleanup
1 parent 1c94a03 commit 4bb6c82

File tree

3 files changed

+58
-50
lines changed

3 files changed

+58
-50
lines changed

mime.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// mime types for web
1+
// MIME Types for Web
22
const mimeTypes: Record<string, string[]> = {
33
// application
44
'application/javascript': ['js', 'mjs'],
@@ -9,16 +9,16 @@ const mimeTypes: Record<string, string[]> = {
99
'application/xml': ['xml', 'xsl'],
1010
// text
1111
'text/html': ['html', 'htm'],
12-
'text/markdown': ['markdown', 'md'],
12+
'text/markdown': ['md', 'markdown'],
13+
'text/mdx': ['mdx'],
1314
'text/typescript': ['ts', 'tsx'],
1415
'text/jsx': ['jsx'],
15-
'text/mdx': ['mdx'],
1616
'text/css': ['css'],
1717
'text/less': ['less'],
1818
'text/sass': ['sass', 'scss'],
1919
'text/stylus': ['stylus', 'styl'],
2020
'text/csv': ['csv'],
21-
'text/plain': ['txt', 'text', 'conf', 'ini', 'log'],
21+
'text/plain': ['txt', 'text', 'conf', 'ini', 'log', 'yaml'],
2222
// font
2323
'font/ttf': ['ttf'],
2424
'font/otf': ['otf'],
@@ -44,13 +44,14 @@ const mimeTypes: Record<string, string[]> = {
4444
'video/ogg': ['ogv'],
4545
'video/webm': ['webm'],
4646
}
47-
47+
const defaultType = 'application/octet-stream'
4848
const typesMap = Object.keys(mimeTypes).reduce((map, contentType) => {
4949
mimeTypes[contentType].forEach(ext => map.set(ext, contentType))
5050
return map
5151
}, new Map<string, string>())
5252

53-
export function getContentType(filepath: string): string {
54-
const ext = filepath.split('.').pop()!.toLowerCase()
55-
return typesMap.get(ext) ?? 'application/octet-stream'
53+
/** get content type by file name */
54+
export function getContentType(filename: string): string {
55+
const ext = filename.split('.').pop()!.toLowerCase()
56+
return typesMap.get(ext) ?? defaultType
5657
}

project.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -536,17 +536,17 @@ export class Project {
536536
Object.assign(this, { buildID: this.mode + '.' + this.config.buildTarget })
537537
// update routing
538538
this.#routing = new Routing([], this.config.baseUrl, this.config.defaultLocale, this.config.locales)
539-
// import postcss plugins
540-
await Promise.all(this.config.postcss.plugins.map(async p => {
541-
let name: string
542-
if (typeof p === 'string') {
543-
name = p
544-
} else {
545-
name = p.name
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
546548
}
547-
const { default: Plugin } = await import(`https://esm.sh/${name}[email protected]&no-check`)
548-
this.#postcssPlugins[name] = Plugin
549-
}))
549+
})
550550
}
551551

552552
private async _init(reload: boolean) {
@@ -565,24 +565,21 @@ export class Project {
565565
await ensureDir(this.buildDir)
566566
}
567567

568-
Object.assign(globalThis, {
569-
ALEPH: {
570-
ENV: {
571-
...this.config.env,
572-
__version: version,
573-
__buildMode: this.mode,
574-
__buildTarget: this.config.buildTarget,
575-
} as AlephEnv
576-
},
577-
document: new Document(),
578-
innerWidth: 1920,
579-
innerHeight: 1080,
580-
devicePixelRatio: 1,
581-
$RefreshReg$: () => { },
582-
$RefreshSig$: () => (type: any) => type,
583-
})
568+
// change current work dir to appDoot
584569
Deno.chdir(this.appRoot)
585570

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+
586583
for await (const { path: p, } of walk(this.srcDir, { ...walkOptions, maxDepth: 1, exts: [...walkOptions.exts, '.jsx', '.tsx'] })) {
587584
const name = path.basename(p)
588585
switch (name.replace(reModuleExt, '')) {
@@ -627,7 +624,7 @@ export class Project {
627624
log.info(colors.bold(`Aleph.js v${version}`))
628625
if (this.config.__file) {
629626
log.info(colors.bold('- Config'))
630-
log.info(' ⚙️', this.config.__file)
627+
log.info(' ', this.config.__file)
631628
}
632629
log.info(colors.bold('- Global'))
633630
if (this.#modules.has('/app.js')) {
@@ -970,7 +967,7 @@ export class Project {
970967
let sourceCode = (new TextDecoder).decode(sourceContent)
971968
for (const plugin of this.config.plugins) {
972969
if (plugin.test.test(url) && plugin.transform) {
973-
const { code, loader = 'js' } = await plugin.transform(sourceContent)
970+
const { code, loader = 'js' } = await plugin.transform(sourceContent, url)
974971
sourceCode = code
975972
mod.loader = loader
976973
break
@@ -1418,6 +1415,16 @@ export class Project {
14181415
}
14191416
}
14201417

1418+
// add virtual browser global objects
1419+
Object.assign(globalThis, {
1420+
document: new Document(),
1421+
innerWidth: 1920,
1422+
innerHeight: 1080,
1423+
devicePixelRatio: 1,
1424+
$RefreshReg$: () => { },
1425+
$RefreshSig$: () => (type: any) => type,
1426+
})
1427+
14211428
/** inject HMR and React Fast Referesh helper code */
14221429
export function injectHmr({ id, sourceFilePath, jsContent }: Module): string {
14231430
let hmrImportPath = path.relative(

types.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ export interface AlephEnv {
1313
readonly __buildTarget: string
1414
}
1515

16+
/**
17+
* A plugin for **Aleph.js** application.
18+
*/
19+
export interface Plugin {
20+
/** `name` gives the plugin a name. */
21+
name?: string
22+
/** `test` matches the import url. */
23+
test: RegExp
24+
/** `resolve` resolves the import url, if the `external` returned the compilation will skip the import url. */
25+
resolve?(url: string): { url: string, external?: boolean }
26+
/** `transform` transforms the source content. */
27+
transform?(content: Uint8Array, url: string): Promise<{ code: string, sourceMap?: string, loader?: 'js' | 'jsx' | 'css' | 'markdown' }>
28+
}
29+
1630
/**
1731
* The options for **SSR**.
1832
*/
@@ -27,20 +41,6 @@ export interface SSROptions {
2741
staticPaths?: string[]
2842
}
2943

30-
/**
31-
* A plugin for **Aleph.js** application.
32-
*/
33-
export interface Plugin {
34-
/** `name` gives the plugin a name. */
35-
name?: string
36-
/** `test` matches import url to transform */
37-
test: RegExp
38-
/** `resolve` resolves the import url, if the `external` returned the compilation will skip the import url. */
39-
resolve?(url: string): { url: string, external?: boolean }
40-
/** `transform` transforms the source content. */
41-
transform?(content: Uint8Array): Promise<{ code: string, sourceMap?: string, loader?: 'js' | 'jsx' | 'css' | 'markdown' }>
42-
}
43-
4444
/**
4545
* Config for Aleph.js application.
4646
*/

0 commit comments

Comments
 (0)