Skip to main content
Module

x/aleph/project.ts

The Full-stack Framework in Deno.
Very Popular
Go to Latest
File
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
import { minify } from 'https://esm.sh/terser'import { EventEmitter } from './events.ts'import { createHtml } from './html.ts'import log from './log.ts'import route from './route.ts'import { colors, ensureDir, path, Sha1, walk } from './std.ts'import { compile } from './tsc/compile.ts'import type { APIHandle, Config, Location, RouterURL } from './types.ts'import util, { hashShort } from './util.ts'import './vendor/clean-css-builds/v4.2.2.js'import less from './vendor/less/less.js'
const reHttp = /^https?:\/\//iconst reModuleExt = /\.(js|jsx|mjs|ts|tsx)$/iconst reStyleModuleExt = /\.(css|less|sass|scss)$/iconst reHashJs = new RegExp(`\\.[0-9a-fx]{${hashShort}}\\.js$`, 'i')
const { CleanCSS } = window as anyconst cleanCSS = new CleanCSS({ compatibility: '*' /* Internet Explorer 10+ */ })
interface Module { id: string url: string isRemote: boolean deps: { url: string, hash: string }[] sourceFilePath: string sourceType: string sourceHash: string jsFile: string jsContent: string jsSourceMap: string hash: string}
interface RenderResult { code: number head: string[] body: string}
export default class Project { readonly mode: 'development' | 'production' readonly rootDir: string readonly config: Config readonly ready: Promise<void>
#modules: Map<string, Module> = new Map() #pageModules: Map<string, { moduleId: string, rendered: Map<string, RenderResult> }> = new Map() #fsWatchListeners: Array<EventEmitter> = []
constructor(dir: string, mode: 'development' | 'production') { this.mode = mode this.rootDir = path.resolve(dir) this.config = { srcDir: '/', outputDir: '/dist', baseUrl: '/', defaultLocale: 'en', ssr: { fallback: '404.html' }, buildTarget: mode === 'development' ? 'es2018' : 'es2015', sourceMap: false, importMap: { imports: {} } } this.ready = (async () => { const t = performance.now() await this._loadConfig() await this._init() log.debug('initialize project token ' + Math.round(performance.now() - t) + 'ms') })() }
get isDev() { return this.mode === 'development' }
get srcDir() { return path.join(this.rootDir, this.config.srcDir) }
get buildDir() { return path.join(this.rootDir, '.aleph', this.mode + '.' + this.config.buildTarget) }
get apiPaths() { return Array.from(this.#modules.keys()) .filter(p => p.startsWith('./api/')) .map(p => p.slice(1).replace(reModuleExt, '')) }
isHMRable(moduleId: string) { return !reHttp.test(moduleId) && ( moduleId === './404.js' || moduleId === './app.js' || moduleId === './data.js' || (moduleId === './data/index.js' && !this.#modules.has('./data.js')) || moduleId.startsWith('./pages/') || moduleId.startsWith('./components/') || reStyleModuleExt.test(moduleId) ) }
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 { modId = '.' + modId if (reHashJs.test(modId)) { const id = modId.slice(0, modId.length - (hashShort + 4)) if (reStyleModuleExt.test(id)) { modId = id } else { modId = id + '.js' } } } if (!this.#modules.has(modId) && modId == './data.js') { modId = './data/index.js' } if (!this.#modules.has(modId)) { console.warn(`can't get the module by path '${pathname}(${modId})'`) } 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 getAPIHandle(path: string): Promise<APIHandle | null> { if (path) { const importPath = '.' + path + '.js' if (this.#modules.has(importPath)) { const { default: handle } = await import("file://" + this.#modules.get(importPath)!.jsFile) return handle } } return null }
async getPageHtml(location: Location): Promise<[number, string]> { const { baseUrl, defaultLocale } = this.config const url = route( baseUrl, Array.from(this.#pageModules.keys()), { location, defaultLocale } ) if (url.pagePath === '') { return [200, this.getDefaultIndexHtml()] }
const mainModule = this.#modules.get('./main.js')! const { code, head, body } = await this._renderPage(url) const html = createHtml({ lang: url.locale, head: head, scripts: [ { type: 'application/json', id: 'ssr-data', innerText: JSON.stringify({ url }) }, { src: path.join(baseUrl, `/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' }, ], body, minify: !this.isDev }) return [code, html] }
getDefaultIndexHtml(): string { const { baseUrl, defaultLocale } = this.config const mainModule = this.#modules.get('./main.js')! const html = createHtml({ lang: defaultLocale, scripts: [ { src: path.join(baseUrl, `/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' }, ], body: `<main></main>`, minify: !this.isDev }) return html }
async getData() { const mod = this.#modules.get('./data.js') || this.#modules.get('./data/index.js') if (mod) { const { default: Data } = await import("file://" + mod.jsFile) let data: any = Data if (util.isFunction(Data)) { data = await Data() } if (util.isPlainObject(data)) { return data } else { log.warn(`module '${mod.url}' should return a plain object as default`) } } return {} }
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._newModule(url) lookup(id) }) } }
// wait project ready await this.ready
// lookup output modules lookup('./main.js')
// ensure ouput directory ready if (util.existsDir(outputDir)) { await Deno.remove(outputDir, { recursive: true }) } await Promise.all([outputDir, distDir].map(dir => ensureDir(dir)))
// copy public files const publicDir = path.join(this.rootDir, 'public') if (util.existsDir(publicDir)) { for await (const { path: p } of walk(publicDir, { includeDirs: false })) { const rp = path.resolve(util.trimPrefix(p, publicDir)) await Deno.copyFile(p, path.join(outputDir, rp)) } }
// write modules const { sourceMap } = this.config await Promise.all(Array.from(outputModules).map((moduleId) => { const { sourceFilePath, 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' return Promise.all([ writeTextFile(jsFile, jsContent), sourceMap ? writeTextFile(jsFile + '.map', jsSourceMap) : Promise.resolve(), ]) }))
// write static data if (this.#modules.has('./data.js') || this.#modules.has('./data/index.js')) { const { hash } = this.#modules.get('./data.js') || this.#modules.get('./data/index.js')! const data = this.getData() await writeTextFile(path.join(distDir, `data.${hash.slice(0, hashShort)}.js`), `export default ${JSON.stringify(data)}`) }
const { ssr } = this.config if (ssr) { for (const pathname of this.#pageModules.keys()) { const [_, html] = await this.getPageHtml({ pathname }) const htmlFile = path.join(outputDir, pathname, 'index.html') await writeTextFile(htmlFile, html) } const fallback = path.join(outputDir, util.isPlainObject(ssr) && ssr.fallback ? ssr.fallback : '404.html') await writeTextFile(fallback, this.getDefaultIndexHtml()) } else { await writeTextFile(path.join(outputDir, 'index.html'), this.getDefaultIndexHtml()) }
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.config.importMap, { imports: Object.assign({}, this.config.importMap.imports, imports) }) }
const importMapFile = path.join(this.rootDir, 'import_map.json') if (util.existsFile(importMapFile)) { const { imports } = JSON.parse(await Deno.readTextFile(importMapFile)) Object.assign(this.config.importMap, { imports: Object.assign({}, this.config.importMap.imports, imports) }) }
const config: Record<string, any> = {} for await (const { path: p } of walk(this.srcDir, { includeDirs: false, exts: ['.js', '.mjs', '.ts', '.json'], skip: [/\.d\.ts$/i], maxDepth: 1 })) { const name = path.basename(p) if (name.split('.')[0] === 'config') { if (name.endsWith('.json')) { try { const conf = JSON.parse(await Deno.readTextFile(p)) Object.assign(config, conf) log.debug(name, config) } catch (e) { log.fatal('parse config.json:', e.message) } } else { const { default: conf } = await import("file://" + p) if (util.isPlainObject(conf)) { Object.assign(config, conf) log.debug(name, config) } } } }
const { srcDir, ouputDir, baseUrl, ssr, buildTarget, sourceMap, defaultLocale } = 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 (util.isNEString(defaultLocale)) { Object.assign(this.config, { defaultLocale }) } if (typeof ssr === 'boolean') { Object.assign(this.config, { ssr }) } else if (util.isPlainObject(ssr)) { const fallback = util.isNEString(ssr.fallback) ? ssr.fallback : '404.html' const include = util.isArray(ssr.include) ? ssr.include : [] const exclude = util.isArray(ssr.exclude) ? ssr.exclude : [] Object.assign(this.config, { ssr: { fallback, include, exclude } }) } 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 }) } }
private async _init() { const walkOptions = { includeDirs: false, exts: ['.js', '.jsx', '.mjs', '.ts', '.tsx'], skip: [/\.d\.ts$/i] } const dataDir = path.join(this.srcDir, 'data') const apiDir = path.join(this.srcDir, 'api') const pagesDir = path.join(this.srcDir, 'pages')
if (!(util.existsDir(pagesDir))) { log.fatal(`'pages' directory not found.`) }
Object.assign(globalThis, { ALEPH_ENV: { appDir: this.rootDir, }, $RefreshReg$: () => { }, $RefreshSig$: () => (type: any) => type, })
for await (const { path: p, isDirectory, isFile } of walk(this.srcDir, { maxDepth: 1 })) { const name = path.basename(p) if (isDirectory && p !== this.srcDir) { switch (name) { case 'api': for await (const { path: p } of walk(apiDir, walkOptions)) { const rp = path.resolve(util.trimPrefix(p, apiDir)) await this._compile('./api/' + rp) } break case 'data': for await (const { path: p } of walk(dataDir, { ...walkOptions, maxDepth: 1 })) { const name = path.basename(p) if (name.replace(reModuleExt, '') === 'index') { await this._compile('./data/' + name) } } break } } else if (isFile && reModuleExt.test(name)) { switch (name.replace(reModuleExt, '')) { case 'app': case 'data': case '404': await this._compile('./' + name) break } } }
for await (const { path: p } of walk(pagesDir, walkOptions)) { const rp = path.resolve(util.trimPrefix(p, pagesDir)) || '/' const pagePath = rp.replace(reModuleExt, '').replace(/\s+/g, '-').replace(/\/?index$/i, '/') this.#pageModules.set(pagePath, { moduleId: './pages' + rp.replace(reModuleExt, '') + '.js', rendered: new Map() }) await this._compile('./pages' + rp) }
const preCompileUrls = [ 'https://deno.land/x/aleph/app.ts', 'https://deno.land/x/aleph/renderer.ts', 'https://deno.land/x/aleph/vendor/tslib/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._createMainModule()
log.info(colors.bold('Pages')) for (const path of this.#pageModules.keys()) { const isIndex = path == '/' log.info('○', path, isIndex ? colors.dim('(index)') : '') } for (const path of this.apiPaths) { log.info('λ', path) }
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.rootDir), '/') const validated = (() => { if (!reModuleExt.test(path) && !reStyleModuleExt.test(path)) { return false } // ignore '.aleph' and outputDir directories if (path.startsWith('.aleph/') || path.startsWith(this.config.outputDir.slice(1))) { return false } const moduleId = './' + path.replace(reModuleExt, '.js') switch (moduleId) { case './404.js': case './app.js': case './data.js': case './data/index.js': { return true } default: { if ((moduleId.startsWith('./pages/') || moduleId.startsWith('./api/')) && moduleId.endsWith('.js')) { 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 removed = !util.existsFile(p) const cleanup = () => { if (moduleId === './app.js' || moduleId === './data.js' || moduleId === './data/index.js') { this._clearPageRenderCache() } else if (moduleId.startsWith('./pages/')) { if (removed) { this._removePageModule(moduleId) } else { this._clearPageRenderCache(moduleId) } } this._createMainModule() } if (!removed) { let type = 'modify' if (!this.#modules.has(moduleId)) { type = 'add' } log.info(type, './' + path) this._compile('./' + path, { forceCompile: true }).then(({ hash }) => { const hmrable = this.isHMRable(moduleId) if (hmrable) { if (type === 'add') { this.#fsWatchListeners.forEach(e => e.emit('add', moduleId, hash)) } else { this.#fsWatchListeners.forEach(e => e.emit('modify-' + moduleId, hash)) } } cleanup() this._updateDependency('./' + path, hash, mod => { if (!hmrable && this.isHMRable(mod.id)) { this.#fsWatchListeners.forEach(e => e.emit(mod.id, 'modify', mod.hash)) } if (mod.id.startsWith('./pages/')) { this._clearPageRenderCache(mod.id) } }) }) } else if (this.#modules.has(moduleId)) { this.#modules.delete(moduleId) cleanup() if (this.isHMRable(moduleId)) { this.#fsWatchListeners.forEach(e => e.emit('remove', moduleId)) } log.info('remove', './' + path) } }, 150) } } } }
private _removePageModule(moduleId: string) { let pagePath = '' for (const [p, pm] of this.#pageModules.entries()) { if (pm.moduleId === moduleId) { pagePath = p break } } if (pagePath !== '') { this.#pageModules.delete(pagePath) } }
private _clearPageRenderCache(moduleId?: string) { for (const [_, p] of this.#pageModules.entries()) { if (moduleId === undefined || p.moduleId === moduleId) { p.rendered.clear() break } } }
private _newModule(url: string): Module { const { importMap } = this.config const isRemote = reHttp.test(url) || (url in importMap.imports && reHttp.test(importMap.imports[url])) const sourceFilePath = renameImportUrl(url) const id = (isRemote ? '//' + util.trimPrefix(sourceFilePath, '/-/') : '.' + sourceFilePath).replace(reModuleExt, '.js')
return { id, url, isRemote, sourceFilePath, sourceType: path.extname(sourceFilePath).slice(1).replace('mjs', 'js') || 'js', 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: {}, keyModules: {}, pageModules: {} } const module = this._newModule('./main.js') const deps = [ 'https://deno.land/x/aleph/vendor/tslib/tslib.js', 'https://deno.land/x/aleph/app.ts', this.isDev && 'https://deno.land/x/aleph/hmr.ts' ].filter(Boolean).map(url => ({ url: String(url), hash: this.#modules.get(String(url).replace(reHttp, '//').replace(reModuleExt, '.js'))?.hash || '' })) if (this.#modules.has('./data.js') || this.#modules.has('./data/index.js')) { const { id, url, hash } = this.#modules.get('./data.js') || this.#modules.get('./data/index.js')! config.keyModules.data = { moduleId: id, hash } deps.push({ url, hash }) } if (this.#modules.has('./app.js')) { const { url, hash } = this.#modules.get('./app.js')! config.keyModules.app = { moduleId: './app.js', hash } deps.push({ url, hash }) } if (this.#modules.has('./404.js')) { const { url, hash } = this.#modules.get('./404.js')! config.keyModules['404'] = { moduleId: './404.js', hash } deps.push({ url, hash }) } this.#pageModules.forEach(({ moduleId }, pagePath) => { const { url, hash } = this.#modules.get(moduleId)! const mod = { moduleId, hash } config.pageModules[pagePath] = mod deps.push({ url, hash }) })
module.jsContent = [ this.isDev && 'import "./-/deno.land/x/aleph/hmr.js";', 'import "./-/deno.land/x/aleph/vendor/tslib/tslib.js";', 'import { bootstrap } from "./-/deno.land/x/aleph/app.js";', `bootstrap(${JSON.stringify(config, undefined, this.isDev ? 4 : undefined)});` ].filter(Boolean).join(this.isDev ? '\n' : '') module.hash = (new Sha1()).update(module.jsContent).hex() module.jsFile = path.join(this.buildDir, `main.${module.hash.slice(0, hashShort)}.js`) module.deps = deps
await Promise.all([ writeTextFile(module.jsFile, module.jsContent), writeTextFile(path.join(this.buildDir, 'main.meta.json'), JSON.stringify({ url: './main.js', sourceHash: module.hash, hash: module.hash, deps: module.deps, }, undefined, 4)) ]) this.#modules.set(module.id, module)
return module }
private async _compile(url: string, options?: { sourceCode?: string, implicitDeps?: { url: string, hash: string }[], forceCompile?: boolean }) { const mod = this._newModule(url) if (this.#modules.has(mod.id) && !options?.forceCompile) { return this.#modules.get(mod.id)! }
const { importMap } = this.config 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 (util.existsFile(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) && util.existsFile(jsFile)) { try { mod.jsContent = await Deno.readTextFile(jsFile) if (util.existsFile(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 emptyContent = false if (options?.sourceCode) { const sourceHash = (new Sha1()).update(options.sourceCode).hex() if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) { sourceContent = options.sourceCode mod.sourceHash = sourceHash } } 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 (dlUrl.startsWith('https://esm.sh/[')) { dlUrl.replace(/\[([^\]]+)\]/, (_, s: string) => { const list = s.split(',').map(s => s.trim()) if (list.length > 0) { const mod = util.trimPrefix(url, 'https://esm.sh/').replace(/\/+$/, '') if (!list.includes(mod)) { dlUrl = url } } return _ }) } if (url.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') } 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(`${resp.status} - ${resp.statusText}`) } sourceContent = await resp.text() mod.sourceHash = (new Sha1()).update(sourceContent).hex() if (mod.sourceType === 'js') { const t = resp.headers.get('Content-Type') if (t?.startsWith('text/typescript')) { mod.sourceType = 'ts' } else if (t?.startsWith('text/jsx')) { mod.sourceType = 'jsx' } } } 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 = (new Sha1()).update(text).hex() if (mod.sourceHash !== sourceHash) { sourceContent = text mod.sourceHash = sourceHash } } 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 = (new Sha1()).update(text).hex() if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) { sourceContent = text emptyContent = text === '' mod.sourceHash = sourceHash } }
let fsync = false
// compile source code if (sourceContent != '' || emptyContent) { const t = performance.now() mod.deps = options?.implicitDeps || [] if (mod.sourceType === 'css' || mod.sourceType === 'less') { let css: string = sourceContent if (mod.sourceType === 'less') { try { // todo: sourceMap const output = await less.render(sourceContent || '/* empty content */') css = output.css } catch (error) { throw new Error(`less: ${error}`); } } if (this.isDev) { css = String(css).trim() } else { const output = cleanCSS.minify(css) css = output.styles } const hash = (new Sha1).update(css).hex() const filepath = path.join( path.dirname(mod.sourceFilePath), util.trimSuffix(path.basename(mod.sourceFilePath), '.css') + '.' + hash.slice(0, hashShort) + '.css' ) const asLink = css.length > 1024 if (asLink) { await writeTextFile(path.join(this.buildDir, filepath), css) } mod.jsContent = [ `import { applyCSS } from ${JSON.stringify(relativePath( path.dirname(path.resolve('/', mod.url)), '/-/deno.land/x/aleph/head.js' ))};`, `applyCSS(${JSON.stringify(url)}, ${asLink ? JSON.stringify(path.join(this.config.baseUrl, '_aleph', filepath)) + ', true' : JSON.stringify(this.isDev ? `\n${css}\n` : css)});`, ].join(this.isDev ? '\n' : '') mod.hash = hash mod.jsSourceMap = '' } else { const compileOptions = { target: this.config.buildTarget, mode: this.mode, reactRefresh: this.isDev && !mod.isRemote && (mod.id === './404.js' || mod.id === './app.js' || mod.id.startsWith('./pages/') || mod.id.startsWith('./components/')), rewriteImportPath: (path: string) => this._rewriteImportPath(mod, path), } 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(' ')}`) } const jsContent = outputText.replace(/import([^'"]*)("|')tslib("|')(\)|;)?/g, 'import$1' + JSON.stringify(relativePath( path.dirname(mod.sourceFilePath), '/-/deno.land/x/aleph/vendor/tslib/tslib.js' )) + '$4') 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 = (new Sha1).update(mod.jsContent).hex() }
log.debug(`${url} compiled in ${(performance.now() - t).toFixed(3)}ms`)
if (!fsync) { fsync = true } }
this.#modules.set(mod.id, mod)
// compile deps for (const dep of mod.deps) { 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(path.resolve('/', url)), path.resolve('/', dep.url.replace(reModuleExt, '')) ) 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 = (new Sha1).update(mod.jsContent).hex() } if (!fsync) { fsync = true } } }
if (fsync) { mod.jsFile = path.join(saveDir, name + (mod.isRemote ? '' : `.${mod.hash.slice(0, hashShort)}`)) + '.js' 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, trace?: Set<string>) { trace = trace || new Set() this.#modules.forEach(mod => { mod.deps.forEach(dep => { if (dep.url === depPath && dep.hash !== depHash && !trace?.has(mod.id)) { const depImportPath = relativePath( path.dirname(path.resolve('/', mod.url)), path.resolve('/', 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 = (new Sha1).update(mod.jsContent).hex() 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) trace?.add(mod.id) this._updateDependency(mod.url, mod.hash, callback, trace) log.debug('update dependency:', depPath, '->', mod.url) } }) }) }
private _rewriteImportPath(mod: Module, importPath: string): string { const { importMap } = this.config let rewrittenPath: string if (importPath in importMap.imports) { importPath = importMap.imports[importPath] } if (reHttp.test(importPath)) { if (mod.isRemote) { rewrittenPath = relativePath( path.dirname(path.resolve('/', mod.url.replace(reHttp, '-/').replace(/:(\d+)/, '/$1'))), renameImportUrl(importPath) ) } else { rewrittenPath = relativePath( path.dirname(path.resolve('/', 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: '' }) } 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: '' }) } else { mod.deps.push({ url: '.' + path.resolve('/', path.dirname(mod.url), importPath), hash: '' }) } }
if (reHttp.test(rewrittenPath)) { return rewrittenPath }
if (!rewrittenPath.startsWith('.') && !rewrittenPath.startsWith('/')) { rewrittenPath = './' + rewrittenPath } return rewrittenPath.replace(reModuleExt, '') + '.js' }
private async _renderPage(url: RouterURL) { const start = performance.now() const ret: RenderResult = { code: 200, head: [], body: '<main></main>' } const page = this.#pageModules.get(url.pagePath)! if (page.rendered.has(url.pathname)) { const cache = page.rendered.get(url.pathname)! return { ...cache } } try { const appModule = this.#modules.get('./app.js') const pageModule = this.#modules.get(page.moduleId)! const [ { renderPage, renderHead }, { default: App }, { default: Page } ] = await Promise.all([ import("file://" + this.#modules.get('//deno.land/x/aleph/renderer.js')!.jsFile), appModule ? await import("file://" + appModule.jsFile) : Promise.resolve({}), await import("file://" + pageModule.jsFile) ]) const data = await this.getData() const html = renderPage(data, url, appModule ? App : undefined, Page) const head = renderHead([ pageModule.deps.map(({ url }) => url).filter(url => reStyleModuleExt.test(url)), appModule?.deps.map(({ url }) => url).filter(url => reStyleModuleExt.test(url)) ].filter(Boolean).flat()) ret.code = 200 ret.head = head ret.body = `<main>${html}</main>` page.rendered.set(url.pathname, { ...ret }) log.debug(`render page '${url.pagePath}' in ${Math.round(performance.now() - start)}ms`) } catch (err) { ret.code = 500 ret.head = ['<title>500 Error - Aleph.js</title>'] ret.body = `<main><pre>${err.stack}</pre></main>` log.error(err.stack) } return ret }}
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://' + path.resolve('/', importUrl)) const ext = path.extname(path.basename(url.pathname)) || '.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}
async function writeTextFile(filepath: string, content: string) { const dir = path.dirname(filepath) await ensureDir(dir) await Deno.writeTextFile(filepath, content)}