Skip to main content
Module

x/aleph/project.ts

The Full-stack Framework in Deno.
Very Popular
Go to Latest
File
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
import marked from 'https://esm.sh/marked@1.2.0'import postcss, { AcceptedPlugin } from 'https://esm.sh/postcss@8.1.3'import { minify } from 'https://esm.sh/terser@5.3.2'import { safeLoadFront } from 'https://esm.sh/yaml-front-matter@4.1.0'import { Request } from './api.ts'import { EventEmitter } from './events.ts'import { createHtml } from './html.ts'import log from './log.ts'import { getPagePath, RouteModule, Routing } from './routing.ts'import { colors, ensureDir, path, ServerRequest, Sha1, walk } from './std.ts'import { compile } from './tsc/compile.ts'import type { AlephEnv, APIHandler, Config, RouterURL } from './types.ts'import util, { existsDirSync, existsFileSync, hashShort, MB, reHashJs, reHttp, reLocaleID, reMDExt, reModuleExt, reStyleModuleExt } from './util.ts'import { cleanCSS, Document, less } from './vendor/mod.ts'import { version } from './version.ts'
interface Module { id: string url: string loader: string isRemote: boolean sourceFilePath: string sourceHash: string deps: { url: string, hash: string, async?: boolean }[] jsFile: string jsContent: string jsSourceMap: string hash: string}
interface Renderer { renderPage: Function renderHead: Function renderScripts: Function}
interface RenderResult { url: RouterURL status: number head: string[] scripts: Record<string, any>[] body: string data: Record<string, string> | null}
export class Project { readonly mode: 'development' | 'production' readonly appRoot: string readonly config: Readonly<Required<Config>> readonly importMap: Readonly<{ imports: Record<string, string> }> readonly ready: Promise<void>
#modules: Map<string, Module> = new Map() #routing: Routing = new Routing() #apiRouting: Routing = new Routing() #fsWatchListeners: Array<EventEmitter> = [] #renderer: Renderer = { renderPage: () => void 0, renderHead: () => void 0, renderScripts: () => void 0 } #rendered: Map<string, Map<string, RenderResult>> = new Map() #postcssPlugins: Record<string, AcceptedPlugin> = {}
constructor(dir: string, mode: 'development' | 'production', reload = false) { this.mode = mode this.appRoot = dir this.config = { srcDir: '/', outputDir: '/dist', baseUrl: '/', defaultLocale: 'en', env: {}, locales: [], ssr: { fallback: '_fallback.html' }, buildTarget: mode === 'development' ? 'es2018' : 'es2015', sourceMap: false, reactUrl: 'https://esm.sh/react@16.14.0', reactDomUrl: 'https://esm.sh/react-dom@16.14.0', plugins: [], postcss: { plugins: [ 'autoprefixer' ] } } this.importMap = { imports: {} } this.ready = (async () => { const t = performance.now() await this._loadConfig() await this._init(reload) log.debug('init project in ' + Math.round(performance.now() - t) + 'ms') })() }
get isDev() { return this.mode === 'development' }
get srcDir() { return path.join(this.appRoot, this.config.srcDir) }
get buildDir() { return path.join(this.appRoot, '.aleph', this.mode + '.' + this.config.buildTarget) }
isHMRable(moduleID: string) { return !reHttp.test(moduleID) && ( moduleID === '/404.js' || moduleID === '/app.js' || moduleID.startsWith('/pages/') || moduleID.startsWith('/components/') || reStyleModuleExt.test(moduleID) ) }
isSSRable(pathname: string): boolean { const { ssr } = this.config if (util.isPlainObject(ssr)) { if (ssr.include) { for (let r of ssr.include) { if (!r.test(pathname)) { return false } } } if (ssr.exclude) { for (let r of ssr.exclude) { if (r.test(pathname)) { return false } } } } return true }
getModule(id: string): Module | null { if (this.#modules.has(id)) { return this.#modules.get(id)! } return null }
getModuleByPath(pathname: string): Module | null { const { baseUrl } = this.config let modId = pathname if (baseUrl !== '/') { modId = util.trimPrefix(modId, baseUrl) } if (modId.startsWith('/_aleph/')) { modId = util.trimPrefix(modId, '/_aleph') } if (modId.startsWith('/-/')) { modId = '//' + util.trimSuffix(util.trimPrefix(modId, '/-/'), '.js') if (!reStyleModuleExt.test(modId)) { modId = modId + '.js' } } else if (modId.endsWith('.js')) { let id = modId.slice(0, modId.length - 3) if (reHashJs.test(modId)) { id = modId.slice(0, modId.length - (1 + hashShort + 3)) } if (reMDExt.test(id) || reStyleModuleExt.test(id)) { modId = id } else { modId = id + '.js' } } if (!this.#modules.has(modId)) { log.warn(`can't get the module by path '${pathname}(${modId})'`) console.log(Array.from(this.#modules.keys())) } return this.getModule(modId) }
createFSWatcher(): EventEmitter { const e = new EventEmitter() this.#fsWatchListeners.push(e) return e }
removeFSWatcher(e: EventEmitter) { e.removeAllListeners() const index = this.#fsWatchListeners.indexOf(e) if (index > -1) { this.#fsWatchListeners.splice(index, 1) } }
async callAPI(req: ServerRequest, loc: { pathname: string, search?: string }): Promise<APIHandler | null> { const [url] = this.#apiRouting.createRouter(loc) if (url.pagePath != '') { const moduleID = url.pagePath + '.js' if (this.#modules.has(moduleID)) { try { const { default: handle } = await import('file://' + this.#modules.get(moduleID)!.jsFile) if (util.isFunction(handle)) { await handle(new Request(req, url.pathname, url.params, url.query)) } else { req.respond({ status: 500, headers: new Headers({ 'Content-Type': 'application/json; charset=utf-8' }), body: JSON.stringify({ error: { status: 404, message: "handle not found" } }) }).catch(err => log.warn('ServerRequest.respond:', err.message)) } } catch (err) { req.respond({ status: 500, headers: new Headers({ 'Content-Type': 'application/json; charset=utf-8' }), body: JSON.stringify({ error: { status: 500, message: err.message } }) }).catch(err => log.warn('ServerRequest.respond:', err.message)) log.error('callAPI:', err) } } } else { req.respond({ status: 404, headers: new Headers({ 'Content-Type': 'application/javascript; charset=utf-8' }), body: JSON.stringify({ error: { status: 404, message: 'page not found' } }) }).catch(err => log.warn('ServerRequest.respond:', err.message)) } return null }
async getSSRData(loc: { pathname: string, search?: string }): Promise<[number, any]> { if (!this.isSSRable(loc.pathname)) { return [404, null] }
const { status, data } = await this._renderPage(loc) return [status, data] }
async getPageHtml(loc: { pathname: string, search?: string }): Promise<[number, string, Record<string, string> | null]> { if (!this.isSSRable(loc.pathname)) { const [url] = this.#routing.createRouter(loc) return [url.pagePath === '' ? 404 : 200, await this.getSPAIndexHtml(), null] }
const { baseUrl } = this.config const mainModule = this.#modules.get('/main.js')! const { url, status, head, scripts, body, data } = await this._renderPage(loc) const html = createHtml({ lang: url.locale, head: head, scripts: [ data ? { type: 'application/json', innerText: JSON.stringify(data), id: 'ssr-data' } : '', { src: path.join(baseUrl, `/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' }, { src: path.join(baseUrl, `/_aleph/-/deno.land/x/aleph/nomodule.js${this.isDev ? '?dev' : ''}`), nomodule: true }, ...scripts ], body, minify: !this.isDev }) return [status, html, data] }
async getSPAIndexHtml() { const { baseUrl, defaultLocale } = this.config const mainModule = this.#modules.get('/main.js')! const customLoading = await this._renderLoadingPage() const html = createHtml({ lang: defaultLocale, scripts: [ customLoading?.data ? { type: 'application/json', innerText: JSON.stringify(customLoading?.data), id: 'ssr-data' } : '', { src: path.join(baseUrl, `/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' }, { src: path.join(baseUrl, `/_aleph/-/deno.land/x/aleph/nomodule.js${this.isDev ? '?dev' : ''}`), nomodule: true }, ], head: customLoading?.head || [], body: `<main>${customLoading?.body || ''}</main>`, minify: !this.isDev }) return html }
async build() { const start = performance.now() const outputDir = path.join(this.srcDir, this.config.outputDir) const distDir = path.join(outputDir, '_aleph') const outputModules = new Set<string>() const lookup = async (moduleID: string) => { if (this.#modules.has(moduleID) && !outputModules.has(moduleID)) { outputModules.add(moduleID) const { deps } = this.#modules.get(moduleID)! deps.forEach(({ url }) => { const { id } = this._moduleFromURL(url) lookup(id) }) } }
// wait for project ready await this.ready
// lookup output modules this.#routing.lookup(path => path.forEach(r => lookup(r.module.id))) lookup('/main.js') lookup('/404.js') lookup('/app.js') lookup('//deno.land/x/aleph/nomodule.js') lookup('//deno.land/x/aleph/tsc/tslib.js')
if (existsDirSync(outputDir)) { await Deno.remove(outputDir, { recursive: true }) } await ensureDir(outputDir) await ensureDir(distDir)
// ssg const { ssr } = this.config const SPAIndexHtml = await this.getSPAIndexHtml() if (ssr) { log.info(colors.bold(' Pages (SSG)')) const paths = new Set(this.#routing.paths) if (typeof ssr === 'object' && ssr.staticPaths) { ssr.staticPaths.forEach(path => paths.add(path)) } await Promise.all(Array.from(paths).map(async pathname => { if (this.isSSRable(pathname)) { const [status, html, data] = await this.getPageHtml({ pathname }) if (status == 200) { const htmlFile = path.join(outputDir, pathname, 'index.html') await writeTextFile(htmlFile, html) if (data) { const dataFile = path.join(outputDir, '_aleph/data', pathname, 'data.js') await writeTextFile(dataFile, `export default ` + JSON.stringify(data)) } log.info(' ○', pathname, colors.dim('• ' + util.bytesString(html.length))) } else if (status == 404) { log.info(' ○', colors.dim(pathname), colors.red(`Page not found`)) } else if (status == 500) { log.info(' ○', colors.dim(pathname), colors.red(`Error 505`)) } } })) const fbHtmlFile = path.join(outputDir, util.isPlainObject(ssr) && ssr.fallback ? ssr.fallback : '_fallback.html') await writeTextFile(fbHtmlFile, SPAIndexHtml) } else { await writeTextFile(path.join(outputDir, 'index.html'), SPAIndexHtml) }
// write 404 page const { baseUrl } = this.config const { url, head, scripts, body, data } = await this._render404Page() const mainModule = this.#modules.get('/main.js')! const e404PageHtml = createHtml({ lang: url.locale, head: head, scripts: [ data ? { type: 'application/json', innerText: JSON.stringify(data), id: 'ssr-data' } : '', { src: path.join(baseUrl, `/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' }, { src: path.join(baseUrl, `/_aleph/-/deno.land/x/aleph/nomodule.js${this.isDev ? '?dev' : ''}`), nomodule: true }, ...scripts ], body, minify: !this.isDev }) await writeTextFile(path.join(outputDir, '404.html'), e404PageHtml)
// copy public assets const publicDir = path.join(this.appRoot, 'public') if (existsDirSync(publicDir)) { log.info(colors.bold(' Public Assets')) for await (const { path: p } of walk(publicDir, { includeDirs: false, skip: [/\/\.[^\/]+($|\/)/] })) { const rp = util.trimPrefix(p, publicDir) const fp = path.join(outputDir, rp) const fi = await Deno.lstat(p) await ensureDir(path.dirname(fp)) await Deno.copyFile(p, fp) log.info(' ✹', rp, colors.dim('•'), colorfulBytesString(fi.size)) } }
const moduleState = { deps: { bytes: 0, count: 0 }, modules: { bytes: 0, count: 0 }, styles: { bytes: 0, count: 0 } } const logModule = (key: 'deps' | 'modules' | 'styles', size: number) => { moduleState[key].bytes += size moduleState[key].count++ }
// write modules const { sourceMap } = this.config await Promise.all(Array.from(outputModules).map((moduleID) => { const { sourceFilePath, loader, isRemote, jsContent, jsSourceMap, hash } = this.#modules.get(moduleID)! const saveDir = path.join(distDir, path.dirname(sourceFilePath)) const name = path.basename(sourceFilePath).replace(reModuleExt, '') const jsFile = path.join(saveDir, name + (isRemote ? '' : '.' + hash.slice(0, hashShort))) + '.js' if (isRemote) { logModule('deps', jsContent.length) } else { if (loader === 'css') { logModule('styles', jsContent.length) } else { logModule('modules', jsContent.length) } } return Promise.all([ writeTextFile(jsFile, jsContent), sourceMap && jsSourceMap ? writeTextFile(jsFile + '.map', jsSourceMap) : Promise.resolve(), ]) }))
const { deps, modules, styles } = moduleState log.info(colors.bold(' Modules')) log.info(' {}', colors.bold(deps.count.toString()), 'deps', colors.dim(`${util.bytesString(deps.bytes)} (mini, uncompress)`)) log.info(' {}', colors.bold(modules.count.toString()), 'modules', colors.dim(`${util.bytesString(modules.bytes)} (mini, uncompress)`)) log.info(' {}', colors.bold(styles.count.toString()), 'styles', colors.dim(`${util.bytesString(styles.bytes)} (mini, uncompress)`))
log.info(`Done in ${Math.round(performance.now() - start)}ms`) }
private async _loadConfig() { const { ALEPH_IMPORT_MAP } = globalThis as any if (ALEPH_IMPORT_MAP) { const { imports } = ALEPH_IMPORT_MAP Object.assign(this.importMap, { imports: Object.assign({}, this.importMap.imports, imports) }) }
const importMapFile = path.join(this.appRoot, 'import_map.json') if (existsFileSync(importMapFile)) { const { imports } = JSON.parse(await Deno.readTextFile(importMapFile)) Object.assign(this.importMap, { imports: Object.assign({}, this.importMap.imports, imports) }) }
const config: Record<string, any> = {} for (const name of Array.from(['aleph.config', 'config']).map(name => ['ts', 'js', 'mjs', 'json'].map(ext => `${name}.${ext}`)).flat()) { const p = path.join(this.appRoot, name) if (existsFileSync(p)) { if (name.endsWith('.json')) { const conf = JSON.parse(await Deno.readTextFile(p)) if (util.isPlainObject(conf)) { Object.assign(config, conf) Object.assign(this.config, { __file: name }) } } else { let { default: conf } = await import('file://' + p) if (util.isFunction(conf)) { conf = await conf() } if (util.isPlainObject(conf)) { Object.assign(config, conf) Object.assign(this.config, { __file: name }) } } break } }
const { srcDir, ouputDir, baseUrl, buildTarget, sourceMap, defaultLocale, locales, ssr, env, plugins, postcss, } = config if (util.isNEString(srcDir)) { Object.assign(this.config, { srcDir: util.cleanPath(srcDir) }) } if (util.isNEString(ouputDir)) { Object.assign(this.config, { ouputDir: util.cleanPath(ouputDir) }) } if (util.isNEString(baseUrl)) { Object.assign(this.config, { baseUrl: util.cleanPath(encodeURI(baseUrl)) }) } if (/^es(20\d{2}|next)$/i.test(buildTarget)) { Object.assign(this.config, { buildTarget: buildTarget.toLowerCase() }) } if (typeof sourceMap === 'boolean') { Object.assign(this.config, { sourceMap }) } if (util.isNEString(defaultLocale)) { Object.assign(this.config, { defaultLocale }) } if (util.isArray(locales)) { Object.assign(this.config, { locales: Array.from(new Set(locales.filter(l => reLocaleID.test(l)))) }) locales.filter(l => !reLocaleID.test(l)).forEach(l => log.warn(`invalid locale ID '${l}'`)) } if (typeof ssr === 'boolean') { Object.assign(this.config, { ssr }) } else if (util.isPlainObject(ssr)) { const fallback = util.isNEString(ssr.fallback) ? util.ensureExt(ssr.fallback, '.html') : '404.html' const include = util.isArray(ssr.include) ? ssr.include.map(v => util.isNEString(v) ? new RegExp(v) : v).filter(v => v instanceof RegExp) : [] const exclude = util.isArray(ssr.exclude) ? ssr.exclude.map(v => util.isNEString(v) ? new RegExp(v) : v).filter(v => v instanceof RegExp) : [] const staticPaths = util.isArray(ssr.staticPaths) ? ssr.staticPaths.map(v => util.cleanPath(v)) : [] Object.assign(this.config, { ssr: { fallback, include, exclude, staticPaths } }) } if (util.isPlainObject(env)) { Object.assign(this.config, { env }) } if (util.isNEArray(plugins)) { Object.assign(this.config, { plugins }) } if (util.isPlainObject(postcss) && util.isArray(postcss.plugins)) { Object.assign(this.config, { postcss }) } else if (existsFileSync(path.join(this.appRoot, 'postcss.config.json'))) { const text = await Deno.readTextFile(path.join(this.appRoot, 'postcss.config.json')) try { const postcss = JSON.parse(text) if (util.isPlainObject(postcss) && util.isArray(postcss.plugins)) { Object.assign(this.config, { postcss }) } } catch (e) { log.warn('bad postcss.config.json', e.message) } } // update buildID Object.assign(this, { buildID: this.mode + '.' + this.config.buildTarget }) // update routing options this.#routing = new Routing([], this.config.baseUrl, this.config.defaultLocale, this.config.locales) // import post plugins this.config.postcss.plugins.map(async p => { let name: string if (typeof p === 'string') { name = p } else { name = p.name } const { default: Plugin } = await import(`https://esm.sh/${name}?external=postcss@8.1.3&no-check`) this.#postcssPlugins[name] = Plugin }) }
private async _init(reload: boolean) { const walkOptions = { includeDirs: false, exts: ['.js', '.ts', '.mjs'], skip: [/^\./, /\.d\.ts$/i, /\.(test|spec|e2e)\.m?(j|t)sx?$/i] } const apiDir = path.join(this.srcDir, 'api') const pagesDir = path.join(this.srcDir, 'pages')
if (!(existsDirSync(pagesDir))) { log.fatal(`'pages' directory not found.`) }
if (reload) { if (existsDirSync(this.buildDir)) { await Deno.remove(this.buildDir, { recursive: true }) } await ensureDir(this.buildDir) }
Object.assign(globalThis, { ALEPH: { ENV: { ...this.config.env, __version: version, __buildMode: this.mode, __buildTarget: this.config.buildTarget, } as AlephEnv }, document: new Document(), innerWidth: 1920, innerHeight: 1080, devicePixelRatio: 1, $RefreshReg$: () => { }, $RefreshSig$: () => (type: any) => type, }) Deno.chdir(this.appRoot)
for await (const { path: p, } of walk(this.srcDir, { ...walkOptions, maxDepth: 1, exts: [...walkOptions.exts, '.jsx', '.tsx'] })) { const name = path.basename(p) switch (name.replace(reModuleExt, '')) { case 'app': case '404': case 'loading': await this._compile('/' + name) break } }
if (existsDirSync(apiDir)) { for await (const { path: p } of walk(apiDir, walkOptions)) { const mod = await this._compile('/api' + util.trimPrefix(p, apiDir)) this.#apiRouting.update(this._getRouteModule(mod)) } }
for await (const { path: p } of walk(pagesDir, { ...walkOptions, exts: [...walkOptions.exts, '.jsx', '.tsx', '.md'] })) { const rp = util.trimPrefix(p, pagesDir) const mod = await this._compile('/pages' + rp) this.#routing.update(this._getRouteModule(mod)) }
const precompileUrls = [ 'https://deno.land/x/aleph/bootstrap.ts', 'https://deno.land/x/aleph/nomodule.ts', 'https://deno.land/x/aleph/tsc/tslib.js', ] if (this.isDev) { precompileUrls.push('https://deno.land/x/aleph/hmr.ts') } for (const url of precompileUrls) { await this._compile(url) } await this._compile('https://deno.land/x/aleph/renderer.ts', { forceTarget: 'es2020' }) await this._createMainModule()
const { renderPage, renderHead, renderScripts } = await import('file://' + this.#modules.get('//deno.land/x/aleph/renderer.js')!.jsFile) this.#renderer = { renderPage, renderHead, renderScripts }
log.info(colors.bold('Aleph.js')) if ('__file' in this.config) { log.info(colors.bold(' Config')) log.info(' ⚙️', this.config['__file']) } log.info(colors.bold(' Global')) if (this.#modules.has('/app.js')) { log.info(' ✓', 'Custom App') } if (this.#modules.has('/404.js')) { log.info(' ✓', 'Custom 404 Page') } if (this.#modules.has('/loading.js')) { log.info(' ✓', 'Custom Loading Page') }
if (this.isDev) { if (this.#apiRouting.paths.length > 0) { log.info(colors.bold(' APIs')) } for (const path of this.#apiRouting.paths) { log.info(' λ', path) } log.info(colors.bold(' Pages')) for (const path of this.#routing.paths) { const isIndex = path == '/' log.info(' ○', path, isIndex ? colors.dim('(index)') : '') } }
if (this.isDev) { this._watch() } }
private async _watch() { const w = Deno.watchFs(this.srcDir, { recursive: true }) log.info('Start watching code changes...') for await (const event of w) { for (const p of event.paths) { const path = '/' + util.trimPrefix(util.trimPrefix(p, this.appRoot), '/') // handle `api` dir remove directly const validated = (() => { if (!reModuleExt.test(path) && !reStyleModuleExt.test(path) && !reMDExt.test(path)) { return false } // ignore `.aleph` and output directories if (path.startsWith('/.aleph/') || path.startsWith(this.config.outputDir)) { return false } if (reModuleExt.test(path)) { switch (path.replace(reModuleExt, '')) { case '/404': case '/app': return true default: if (path.startsWith('/api/')) { return true } } } if ((reModuleExt.test(path) || reMDExt.test(path)) && path.startsWith('/pages/')) { return true } let isDep = false for (const { deps } of this.#modules.values()) { if (deps.findIndex(dep => dep.url === path) > -1) { isDep = true break } } return isDep })() if (validated) { const moduleID = path.replace(reModuleExt, '.js') util.debounceX(moduleID, () => { const shouldUpdateMainModule = (() => { switch (moduleID) { case '/404.js': case '/app.js': return true default: if (moduleID.startsWith('/pages/')) { return true } return false } })() if (existsFileSync(p)) { let type = 'modify' if (!this.#modules.has(moduleID)) { type = 'add' } log.info(type, path) this._compile(path, { forceCompile: true }).then(mod => { const hmrable = this.isHMRable(mod.id) if (hmrable) { if (type === 'add') { this.#fsWatchListeners.forEach(e => e.emit('add', mod.id, mod.hash)) } else { this.#fsWatchListeners.forEach(e => e.emit('modify-' + mod.id, mod.hash)) } } if (moduleID === '/app.js') { this.#rendered.clear() } else if (moduleID.startsWith('/pages/')) { this.#rendered.delete(getPagePath(moduleID)) this.#routing.update(this._getRouteModule(mod)) } else if (moduleID.startsWith('/api/')) { this.#apiRouting.update(this._getRouteModule(mod)) } if (shouldUpdateMainModule) { this._createMainModule() } this._updateDependency(path, mod.hash, ({ id, hash }) => { if (id.startsWith('/pages/')) { this.#rendered.delete(getPagePath(id)) } if (!hmrable && this.isHMRable(id)) { this.#fsWatchListeners.forEach(e => e.emit('modify-' + id, hash)) } }) }).catch(err => { log.error(`compile(${path}):`, err.message) }) } else if (this.#modules.has(moduleID)) { if (moduleID === '/app.js') { this.#rendered.clear() } else if (moduleID.startsWith('/pages/')) { this.#rendered.delete(getPagePath(moduleID)) this.#routing.removeRoute(moduleID) } else if (moduleID.startsWith('/api/')) { this.#apiRouting.removeRoute(moduleID) } if (shouldUpdateMainModule) { this._createMainModule() } this.#modules.delete(moduleID) if (this.isHMRable(moduleID)) { this.#fsWatchListeners.forEach(e => e.emit('remove', moduleID)) } log.info('remove', path) } }, 150) } } } }
private _getRouteModule({ id, hash }: Module): RouteModule { const asyncDeps = this._lookupAsyncDeps(id).filter(({ async }) => !!async).map(({ async, ...rest }) => rest) return { id, hash, asyncDeps: asyncDeps.length > 0 ? asyncDeps : undefined } }
private _moduleFromURL(url: string): Module { const isRemote = reHttp.test(url) || (url in this.importMap.imports && reHttp.test(this.importMap.imports[url])) const sourceFilePath = renameImportUrl(url) const id = (isRemote ? '//' + util.trimPrefix(sourceFilePath, '/-/') : sourceFilePath).replace(reModuleExt, '.js') let loader = '' if (reStyleModuleExt.test(url)) { loader = 'css' } else if (reMDExt.test(url)) { loader = 'markdown' } else if (reModuleExt.test(url)) { loader = 'js' } else if (isRemote) { loader = 'js' } return { id, url, loader, isRemote, sourceFilePath, sourceHash: '', deps: [], jsFile: '', jsContent: '', jsSourceMap: '', hash: '', } as Module }
private async _createMainModule(): Promise<Module> { const { baseUrl, defaultLocale } = this.config const config: Record<string, any> = { baseUrl, defaultLocale, locales: [], routes: this.#routing.routes, preloadModules: ['/404.js', '/app.js'].filter(id => this.#modules.has(id)).map(id => { return this._getRouteModule(this.#modules.get(id)!) }) } const module = this._moduleFromURL('/main.js') const metaFile = path.join(this.buildDir, 'main.meta.json')
module.jsContent = [ this.isDev && 'import "./-/deno.land/x/aleph/hmr.js";', 'import "./-/deno.land/x/aleph/aleph.js";', 'import "./-/deno.land/x/aleph/context.js";', 'import "./-/deno.land/x/aleph/error.js";', 'import "./-/deno.land/x/aleph/events.js";', 'import "./-/deno.land/x/aleph/routing.js";', 'import "./-/deno.land/x/aleph/util.js";', 'import bootstrap from "./-/deno.land/x/aleph/bootstrap.js";', `Object.assign(window, ${JSON.stringify({ ALEPH: (globalThis as any)['ALEPH'] }, undefined, this.isDev ? 4 : undefined)});`, `bootstrap(${JSON.stringify(config, undefined, this.isDev ? 4 : undefined)});` ].filter(Boolean).join(this.isDev ? '\n' : '') module.hash = getHash(module.jsContent) module.jsFile = path.join(this.buildDir, `main.${module.hash.slice(0, hashShort)}.js`) module.deps = [ this.isDev && 'https://deno.land/x/aleph/hmr.ts', 'https://deno.land/x/aleph/bootstrap.ts' ].filter(Boolean).map(url => ({ url: String(url), hash: this.#modules.get(String(url).replace(reHttp, '//').replace(reModuleExt, '.js'))?.hash || '' }))
await cleanupCompilation(module.jsFile) await Promise.all([ writeTextFile(module.jsFile, module.jsContent), writeTextFile(metaFile, JSON.stringify({ url: '/main.js', sourceHash: module.hash, hash: module.hash, deps: module.deps, }, undefined, 4)) ]) this.#modules.set(module.id, module)
return module }
// todo: force recompile remote modules which URL don't specify version private async _compile(url: string, options?: { sourceCode?: string, forceCompile?: boolean, forceTarget?: string }) { const mod = this._moduleFromURL(url) if (this.#modules.has(mod.id) && !options?.forceCompile) { return this.#modules.get(mod.id)! }
const { importMap } = this const name = path.basename(mod.sourceFilePath).replace(reModuleExt, '') const saveDir = path.join(this.buildDir, path.dirname(mod.sourceFilePath)) const metaFile = path.join(saveDir, `${name}.meta.json`)
if (existsFileSync(metaFile)) { const { sourceHash, hash, deps } = JSON.parse(await Deno.readTextFile(metaFile)) const jsFile = path.join(saveDir, name + (mod.isRemote ? '' : '.' + hash.slice(0, hashShort))) + '.js' if (util.isNEString(sourceHash) && util.isNEString(hash) && util.isArray(deps) && existsFileSync(jsFile)) { try { mod.jsContent = await Deno.readTextFile(jsFile) if (existsFileSync(jsFile + '.map')) { mod.jsSourceMap = await Deno.readTextFile(jsFile + '.map') } mod.jsFile = jsFile mod.hash = hash mod.deps = deps mod.sourceHash = sourceHash } catch (e) { } } }
let sourceContent = '' let shouldCompile = false if (options?.sourceCode) { const sourceHash = getHash(options.sourceCode, true) if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) { mod.sourceHash = sourceHash sourceContent = options.sourceCode shouldCompile = true } } else if (mod.isRemote) { let dlUrl = url for (const importPath in importMap.imports) { const alias = importMap.imports[importPath] if (importPath === url) { dlUrl = alias break } else if (importPath.endsWith('/') && url.startsWith(importPath)) { dlUrl = util.trimSuffix(alias, '/') + '/' + util.trimPrefix(url, importPath) break } } if (/^https?:\/\/[0-9a-z\.\-]+\/react(@[0-9a-z\.\-]+)?\/?$/i.test(dlUrl)) { dlUrl = this.config.reactUrl } if (/^https?:\/\/[0-9a-z\.\-]+\/react\-dom(@[0-9a-z\.\-]+)?(\/server)?\/?$/i.test(dlUrl)) { dlUrl = this.config.reactDomUrl if (/\/server\/?$/i.test(url)) { dlUrl += '/server' } } if (dlUrl.startsWith('https://esm.sh/')) { const u = new URL(dlUrl) u.searchParams.set('target', this.config.buildTarget) if (this.isDev && !u.searchParams.has('dev')) { u.searchParams.set('dev', '') } dlUrl = u.toString().replace(/=(&|$)/, '$1') } else if (dlUrl.startsWith('https://deno.land/x/aleph/')) { dlUrl = `https://deno.land/x/aleph@v${version}/` + util.trimPrefix(dlUrl, 'https://deno.land/x/aleph/') } if (mod.sourceHash === '') { log.info('Download', url, dlUrl != url ? colors.dim(`${dlUrl}`) : '') try { const resp = await fetch(dlUrl) if (resp.status != 200) { throw new Error(`Download ${url}: ${resp.status} - ${resp.statusText}`) } mod.sourceHash = getHash(sourceContent) sourceContent = await resp.text() shouldCompile = true } catch (err) { throw new Error(`Download ${url}: ${err.message}`) } } else if (/^https?:\/\/(localhost|127.0.0.1)(:\d+)?\//.test(dlUrl)) { try { const resp = await fetch(dlUrl) if (resp.status != 200) { throw new Error(`${resp.status} - ${resp.statusText}`) } const text = await resp.text() const sourceHash = getHash(text, true) if (mod.sourceHash !== sourceHash) { mod.sourceHash = sourceHash sourceContent = text shouldCompile = true } } catch (err) { throw new Error(`Download ${url}: ${err.message}`) } } } else { const filepath = path.join(this.srcDir, url) try { const fileinfo = await Deno.stat(filepath) // 10mb limit if (fileinfo.size > 10 * (1 << 20)) { throw new Error(`ignored module '${url}': too large(${(fileinfo.size / (1 << 20)).toFixed(2)}mb)`) } } catch (err) { if (err instanceof Deno.errors.NotFound) { throw new Error(`module '${url}' not found`) } } const text = await Deno.readTextFile(filepath) const sourceHash = getHash(text, true) if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) { mod.sourceHash = sourceHash sourceContent = text shouldCompile = true } }
let fsync = false
// compile source code if (shouldCompile) { const t = performance.now() mod.deps = [] if (mod.loader === 'css') { let css: string = sourceContent if (mod.id.endsWith('.less')) { try { const output = await less.render(sourceContent || '/* empty content */') css = output.css } catch (error) { throw new Error(`less: ${error}`); } } const plugins = this.config.postcss.plugins.map(p => { if (typeof p === 'string') { return this.#postcssPlugins[p] } else { const Plugin = this.#postcssPlugins[p.name] as Function return Plugin(p.options) } }) css = (await postcss(plugins).process(css).async()).content if (this.isDev) { css = css.trim() } else { const output = cleanCSS.minify(css) css = output.styles } mod.jsContent = [ `import { applyCSS } from ${JSON.stringify(relativePath( path.dirname(mod.url), '/-/deno.land/x/aleph/head.js' ))};`, `applyCSS(${JSON.stringify(url)}, ${JSON.stringify(this.isDev ? `\n${css}\n` : css)});`, ].join(this.isDev ? '\n' : '') mod.jsSourceMap = '' // todo: sourceMap mod.hash = getHash(css) } else if (mod.loader === 'markdown') { const { __content, ...props } = safeLoadFront(sourceContent) const html = marked.parse(__content) mod.jsContent = [ this.isDev && `const _s = $RefreshSig$();`, `import React, { useEffect, useRef } from ${JSON.stringify(relativePath(path.dirname(mod.sourceFilePath), '/-/esm.sh/react.js'))};`, `import { redirect } from ${JSON.stringify(relativePath(path.dirname(mod.sourceFilePath), '/-/deno.land/x/aleph/aleph.js'))};`, `export default function MarkdownPage() {`, this.isDev && ` _s();`, ` const ref = useRef(null);`, ` useEffect(() => {`, ` const anchors = [];`, ` const onClick = e => {`, ` e.preventDefault();`, ` redirect(e.currentTarget.getAttribute("href"));`, ` };`, ` if (ref.current) {`, ` ref.current.querySelectorAll("a").forEach(a => {`, ` const href = a.getAttribute("href");`, ` if (href && !/^[a-z0-9]+:/i.test(href)) {`, ` a.addEventListener("click", onClick, false);`, ` anchors.push(a);`, ` }`, ` });`, ` }`, ` return () => anchors.forEach(a => a.removeEventListener("click", onClick));`, ` }, []);`, ` return React.createElement("div", {className: "markdown-page", ref, dangerouslySetInnerHTML: {__html: ${JSON.stringify(html)}}});`, `}`, `MarkdownPage.meta = ${JSON.stringify(props, undefined, this.isDev ? 4 : undefined)};`, this.isDev && `_s(MarkdownPage, "useRef{ref}\\nuseEffect{}");`, this.isDev && `$RefreshReg$(MarkdownPage, "MarkdownPage");`, ].filter(Boolean).map(l => !this.isDev ? String(l).trim() : l).join(this.isDev ? '\n' : '') mod.jsSourceMap = '' mod.hash = getHash(mod.jsContent) } else if (mod.loader === 'js') { const useDenos: string[] = [] const compileOptions = { mode: this.mode, target: options?.forceTarget || this.config.buildTarget, reactRefresh: this.isDev && !mod.isRemote, rewriteImportPath: (path: string, async?: boolean) => this._rewriteImportPath(mod, path, async), signUseDeno: (id: string) => { const sig = 'useDeno.' + (new Sha1()).update(id).update(version).update(Date.now().toString()).hex().slice(0, hashShort) useDenos.push(sig) return sig } } const { diagnostics, outputText, sourceMapText } = compile(mod.sourceFilePath, sourceContent, compileOptions) if (diagnostics && diagnostics.length > 0) { throw new Error(`compile ${url}: ${diagnostics.map(d => d.messageText).join('\n')}`) } const jsContent = outputText.replace(/import\s*{([^}]+)}\s*from\s*("|')tslib("|');?/g, 'import {$1} from ' + JSON.stringify(relativePath( path.dirname(mod.sourceFilePath), '/-/deno.land/x/aleph/tsc/tslib.js' )) + ';') if (this.isDev) { mod.jsContent = jsContent mod.jsSourceMap = sourceMapText! } else { const { code, map } = await minify(jsContent, { compress: false, mangle: true, sourceMap: { content: sourceMapText!, } }) if (code) { mod.jsContent = code } else { mod.jsContent = jsContent } if (util.isNEString(map)) { mod.jsSourceMap = map } } mod.hash = getHash(mod.jsContent) useDenos.forEach(sig => { mod.deps.push({ url: '#' + sig, hash: '', async: true }) }) } else { throw new Error(`Unknown loader '${mod.loader}'`) }
log.debug(`compile '${url}' in ${Math.round(performance.now() - t)}ms`)
if (!fsync) { fsync = true } }
this.#modules.set(mod.id, mod)
// compile deps for (const dep of mod.deps.filter(({ url }) => !url.startsWith('#useDeno.'))) { const depMod = await this._compile(dep.url) if (dep.hash !== depMod.hash) { dep.hash = depMod.hash if (!reHttp.test(dep.url)) { const depImportPath = relativePath( path.dirname(url), dep.url.replace(reModuleExt, '') ) mod.jsContent = mod.jsContent.replace(/(import|Import|export)([\s\S]*?)(from\s*:?\s*|\()("|')([^'"]+)("|')(\)|;)?/g, (s, key, fields, from, ql, importPath, qr, end) => { if ( reHashJs.test(importPath) && importPath.slice(0, importPath.length - (hashShort + 4)) === depImportPath ) { return `${key}${fields}${from}${ql}${depImportPath}.${dep.hash.slice(0, hashShort)}.js${qr}${end || ''}` } return s }) mod.hash = getHash(mod.jsContent) } if (!fsync) { fsync = true } } }
if (fsync) { mod.jsFile = path.join(saveDir, name + (mod.isRemote ? '' : `.${mod.hash.slice(0, hashShort)}`)) + '.js' await cleanupCompilation(mod.jsFile) await Promise.all([ writeTextFile(metaFile, JSON.stringify({ url, sourceHash: mod.sourceHash, hash: mod.hash, deps: mod.deps, }, undefined, 4)), writeTextFile(mod.jsFile, mod.jsContent), mod.jsSourceMap !== '' ? writeTextFile(mod.jsFile + '.map', mod.jsSourceMap) : Promise.resolve() ]) }
return mod }
private _updateDependency(depPath: string, depHash: string, callback: (mod: Module) => void, tracing = new Set<string>()) { this.#modules.forEach(mod => { mod.deps.forEach(dep => { if (dep.url === depPath && dep.hash !== depHash && !tracing?.has(mod.id)) { const depImportPath = relativePath( path.dirname(mod.url), dep.url.replace(reModuleExt, '') ) dep.hash = depHash if (mod.id === '/main.js') { this._createMainModule() } else { mod.jsContent = mod.jsContent.replace(/(import|export)([^'"]*)("|')([^'"]+)("|')(\)|;)?/g, (s, key, from, ql, importPath, qr, end) => { if ( reHashJs.test(importPath) && importPath.slice(0, importPath.length - (hashShort + 4)) === depImportPath ) { return `${key}${from}${ql}${depImportPath}.${dep.hash.slice(0, hashShort)}.js${qr}${end || ''}` } return s }) mod.hash = getHash(mod.jsContent) mod.jsFile = `${mod.jsFile.replace(reHashJs, '')}.${mod.hash.slice(0, hashShort)}.js` Promise.all([ writeTextFile(mod.jsFile.replace(reHashJs, '') + '.meta.json', JSON.stringify({ sourceFile: mod.url, sourceHash: mod.sourceHash, hash: mod.hash, deps: mod.deps, }, undefined, 4)), writeTextFile(mod.jsFile, mod.jsContent), mod.jsSourceMap !== '' ? writeTextFile(mod.jsFile + '.map', mod.jsSourceMap) : Promise.resolve() ]) } callback(mod) tracing.add(mod.id) log.debug('update dependency:', depPath, '->', mod.url) this._updateDependency(mod.url, mod.hash, callback, tracing) } }) }) }
private _rewriteImportPath(mod: Module, importPath: string, async?: boolean): string { let rewrittenPath: string if (importPath in this.importMap.imports) { importPath = this.importMap.imports[importPath] } if (reHttp.test(importPath)) { if (mod.isRemote) { rewrittenPath = relativePath( path.dirname(mod.url.replace(reHttp, '/-/').replace(/:(\d+)/, '/$1')), renameImportUrl(importPath) ) } else { rewrittenPath = relativePath( path.dirname(mod.url), renameImportUrl(importPath) ) } } else { if (mod.isRemote) { const modUrl = new URL(mod.url) let pathname = importPath if (!pathname.startsWith('/')) { pathname = path.join(path.dirname(modUrl.pathname), importPath) } const importUrl = new URL(modUrl.protocol + '//' + modUrl.host + pathname) rewrittenPath = relativePath( path.dirname(mod.sourceFilePath), renameImportUrl(importUrl.toString()) ) } else { rewrittenPath = importPath.replace(reModuleExt, '') + '.' + 'x'.repeat(hashShort) } } if (reHttp.test(importPath)) { mod.deps.push({ url: importPath, hash: '', async }) } else { if (mod.isRemote) { const sourceUrl = new URL(mod.url) let pathname = importPath if (!pathname.startsWith('/')) { pathname = path.join(path.dirname(sourceUrl.pathname), importPath) } mod.deps.push({ url: sourceUrl.protocol + '//' + sourceUrl.host + pathname, hash: '', async }) } else { mod.deps.push({ url: path.join(path.dirname(mod.url), importPath), hash: '', async }) } }
if (reHttp.test(rewrittenPath)) { return rewrittenPath }
if (!rewrittenPath.startsWith('.') && !rewrittenPath.startsWith('/')) { rewrittenPath = '/' + rewrittenPath } return rewrittenPath.replace(reModuleExt, '') + '.js' }
private async _renderPage(loc: { pathname: string, search?: string }) { const start = performance.now() const [url, pageModuleTree] = this.#routing.createRouter(loc) const key = [url.pathname, url.query.toString()].filter(Boolean).join('?') if (url.pagePath !== '') { if (this.#rendered.has(url.pagePath)) { const cache = this.#rendered.get(url.pagePath)! if (cache.has(key)) { return cache.get(key)! } } else { this.#rendered.set(url.pagePath, new Map()) } } const ret: RenderResult = { url, status: url.pagePath === '' ? 404 : 200, head: [], scripts: [], body: '<main></main>', data: null } Object.assign(window, { location: { protocol: 'http:', host: 'localhost', hostname: 'localhost', port: '', href: 'https://esm.sh' + url.pathname + url.query.toString(), origin: 'https://esm.sh', pathname: url.pathname, search: url.query.toString(), hash: '', reload() { }, replace() { }, toString() { return this.href }, } }) if (ret.status === 404) { if (this.isDev) { log.warn(`page '${url.pathname}' not found`) } return await this._render404Page(url) } try { const appModule = this.#modules.get('/app.js') const { default: App } = appModule ? await import('file://' + appModule.jsFile) : {} as any const pageComponentTree: { id: string, Component?: any }[] = pageModuleTree.map(({ id }) => ({ id })) const imports = pageModuleTree.map(async ({ id }) => { const mod = this.#modules.get(id)! const { default: C } = await import('file://' + mod.jsFile) const pc = pageComponentTree.find(pc => pc.id === mod.id) if (pc) { pc.Component = C } }) await Promise.all(imports) const [html, data] = await this.#renderer.renderPage(url, App, undefined, pageComponentTree) const head = await this.#renderer.renderHead([ appModule ? this._lookupAsyncDeps(appModule.id).filter(({ url }) => reStyleModuleExt.test(url)) : [], ...pageModuleTree.map(({ id }) => this._lookupAsyncDeps(id).filter(({ url }) => reStyleModuleExt.test(url))) ].flat()) ret.head = head ret.scripts = await Promise.all(this.#renderer.renderScripts().map(async (script: Record<string, any>) => { if (!this.isDev && script.innerText) { return { ...script, innerText: (await minify(script.innerText)).code } } return script })) ret.body = `<main>${html}</main>` ret.data = data this.#rendered.get(url.pagePath)!.set(key, ret) if (this.isDev) { log.debug(`render '${url.pathname}' in ${Math.round(performance.now() - start)}ms`) } } catch (err) { ret.status = 500 ret.head = ['<title>Error 500 - Aleph.js</title>'] ret.body = `<main><pre>${colors.stripColor(err.stack)}</pre></main>` log.error(err) } return ret }
private async _render404Page(url: RouterURL = { locale: this.config.defaultLocale, pagePath: '', pathname: '/', params: {}, query: new URLSearchParams() }) { const ret: RenderResult = { url, status: 404, head: [], scripts: [], body: '<main></main>', data: null } try { const e404Module = this.#modules.get('/404.js') const { default: E404 } = e404Module ? await import('file://' + e404Module.jsFile) : {} as any const [html, data] = await this.#renderer.renderPage(url, undefined, E404, []) const head = await this.#renderer.renderHead([ e404Module ? this._lookupAsyncDeps(e404Module.id).filter(({ url }) => reStyleModuleExt.test(url)) : [] ].flat()) ret.head = head ret.scripts = await Promise.all(this.#renderer.renderScripts().map(async (script: Record<string, any>) => { if (!this.isDev && script.innerText) { return { ...script, innerText: (await minify(script.innerText)).code } } return script })) ret.body = `<main>${html}</main>` ret.data = data } catch (err) { ret.status = 500 ret.head = ['<title>Error 500 - Aleph.js</title>'] ret.body = `<main><pre>${colors.stripColor(err.stack)}</pre></main>` log.error(err) } return ret }
private async _renderLoadingPage() { if (this.#modules.has('/loading.js')) { const loadingModule = this.#modules.get('/loading.js')! const { default: Loading } = await import('file://' + loadingModule.jsFile) const url = { locale: this.config.defaultLocale, pagePath: '', pathname: '/', params: {}, query: new URLSearchParams() } const [html, data] = await this.#renderer.renderPage(url, undefined, undefined, [{ id: '/loading.js', Component: Loading }]) const head = await this.#renderer.renderHead([ this._lookupAsyncDeps(loadingModule.id).filter(({ url }) => reStyleModuleExt.test(url)) ].flat()) return { head, body: html, data, } as RenderResult } return null }
private _lookupAsyncDeps(moduleID: string, __deps: { url: string, hash: string, async?: boolean }[] = [], __tracing: Set<string> = new Set()) { const mod = this.getModule(moduleID) if (!mod) { return __deps } if (__tracing.has(moduleID)) { return __deps } __tracing.add(moduleID) __deps.push(...mod.deps.filter(({ url, async }) => !!async && __deps.findIndex(i => i.url === url) === -1)) mod.deps.forEach(({ url }) => { if (reModuleExt.test(url) && !reHttp.test(url)) { this._lookupAsyncDeps(url.replace(reModuleExt, '.js'), __deps, __tracing) } }) return __deps }}
export function injectHmr({ id, sourceFilePath, jsContent }: Module): string { let hmrImportPath = path.relative( path.dirname(sourceFilePath), '/-/deno.land/x/aleph/hmr.js' ) if (!hmrImportPath.startsWith('.') && !hmrImportPath.startsWith('/')) { hmrImportPath = './' + hmrImportPath }
const text = [ `import { createHotContext, RefreshRuntime, performReactRefresh } from ${JSON.stringify(hmrImportPath)};`, `import.meta.hot = createHotContext(${JSON.stringify(id)});` ] const reactRefresh = id.endsWith('.js') || id.endsWith('.md') || id.endsWith('.mdx') if (reactRefresh) { text.push('') text.push( `const prevRefreshReg = window.$RefreshReg$;`, `const prevRefreshSig = window.$RefreshSig$;`, `Object.assign(window, {`, ` $RefreshReg$: (type, id) => RefreshRuntime.register(type, ${JSON.stringify(id)} + " " + id),`, ` $RefreshSig$: RefreshRuntime.createSignatureFunctionForTransform`, `});`, ) } text.push('') text.push(jsContent) text.push('') if (reactRefresh) { text.push( 'window.$RefreshReg$ = prevRefreshReg;', 'window.$RefreshSig$ = prevRefreshSig;', 'import.meta.hot.accept(performReactRefresh);' ) } else { text.push('import.meta.hot.accept();') } return text.join('\n')}
function relativePath(from: string, to: string): string { let r = path.relative(from, to) if (!r.startsWith('.') && !r.startsWith('/')) { r = './' + r } return r}
function renameImportUrl(importUrl: string): string { const isRemote = reHttp.test(importUrl) const url = new URL(isRemote ? importUrl : 'file://' + importUrl) let ext = path.extname(path.basename(url.pathname)) || '.js' if (isRemote && !reModuleExt.test(ext) && !reStyleModuleExt.test(ext) && !reMDExt.test(ext)) { ext = '.js' } let pathname = util.trimSuffix(url.pathname, ext) let search = Array.from(url.searchParams.entries()).map(([key, value]) => value ? `${key}=${value}` : key) if (search.length > 0) { pathname += '@' + search.join(',') } if (isRemote) { return '/-/' + url.hostname + (url.port ? '/' + url.port : '') + pathname + ext } return pathname + ext}
function getHash(content: string, checkVersion = false) { const sha1 = new Sha1() sha1.update(content) if (checkVersion) { sha1.update(version) } return sha1.hex()}
function colorfulBytesString(bytes: number) { let cf = colors.dim if (bytes > 10 * MB) { cf = colors.red } else if (bytes > MB) { cf = colors.yellow } return cf(util.bytesString(bytes))}
async function writeTextFile(filepath: string, content: string) { const dir = path.dirname(filepath) await ensureDir(dir) await Deno.writeTextFile(filepath, content)}
async function cleanupCompilation(jsFile: string) { const dir = path.dirname(jsFile) const jsFileName = path.basename(jsFile) if (!reHashJs.test(jsFile) || !existsDirSync(dir)) { return } const jsName = jsFileName.split('.').slice(0, -2).join('.') + '.js' for await (const entry of Deno.readDir(dir)) { if (entry.isFile && (entry.name.endsWith('.js') || entry.name.endsWith('.js.map'))) { const _jsName = util.trimSuffix(entry.name, '.map').split('.').slice(0, -2).join('.') + '.js' if (_jsName === jsName && jsFileName !== entry.name) { await Deno.remove(path.join(dir, entry.name)) } } }}