Skip to content

Commit

Permalink
v0.3.0-beta.10
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Sep 2, 2021
1 parent 3cbd834 commit 858be54
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
21 changes: 18 additions & 3 deletions bundler/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import type { DependencyGraph } from '../server/analyzer.ts'
import type { Aleph } from '../server/aleph.ts'
import { clearBuildCache, computeHash, getAlephPkgUri } from '../server/helper.ts'
import { ensureTextFile, existsFile, lazyRemove } from '../shared/fs.ts'
import type { BrowserName, Module } from '../types.d.ts'
import util from '../shared/util.ts'
import type { BrowserName, Module, DependencyDescriptor } from '../types.d.ts'
import { VERSION } from '../version.ts'
import { esbuild, stopEsbuild, esmLoader } from './esbuild.ts'

Expand Down Expand Up @@ -136,7 +137,7 @@ export class Bundler {
return jsFile
}

let source = await this.#aleph.resolveModuleSource(mod.specifier)
let source = 'source' in mod ? (mod as any).source : await this.#aleph.resolveModuleSource(mod.specifier)
if (source === null) {
this.#compiled.delete(mod.specifier)
throw new Error(`Unsupported module '${mod.specifier}'`)
Expand Down Expand Up @@ -175,8 +176,22 @@ export class Bundler {
}
}

let extraDeps: DependencyDescriptor[] = []
for (const { test, transform } of this.#aleph.transformListeners) {
if (test instanceof RegExp && test.test(mod.specifier)) {
const { jsBuffer, ready, ...rest } = mod
const ret = await transform({ module: { ...structuredClone(rest) }, code, bundleMode: true })
if (util.isFilledString(ret?.code)) {
code = ret!.code
}
if (Array.isArray(ret?.extraDeps)) {
extraDeps.push(...ret!.extraDeps)
}
}
}

// compile deps
await Promise.all(mod.deps.map(async dep => {
await Promise.all(mod.deps.concat(extraDeps).map(async dep => {
if (!dep.specifier.startsWith('#') && !externals.includes(dep.specifier)) {
const depMod = this.#aleph.getModule(dep.specifier)
if (depMod !== null) {
Expand Down
54 changes: 31 additions & 23 deletions server/aleph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ export class Aleph implements IAleph {
return this.#ready
}

get transformListeners() {
return this.#transformListeners
}

/** get the module by given specifier. */
getModule(specifier: string): Module | null {
if (specifier === 'app') {
Expand Down Expand Up @@ -532,21 +536,23 @@ export class Aleph implements IAleph {
if (sourceType === SourceType.Unknown) {
throw new Error("addModule: unknown souce type")
}
const source = {
code: sourceCode,
type: sourceType,
}
const module = await this.compile(specifier, {
source: {
code: sourceCode,
type: sourceType,
},
source,
forceRefresh,
})
if (specifier.startsWith('pages/') || specifier.startsWith('api/')) {
specifier = '/' + specifier
}
if (specifier.startsWith('/pages/')) {
if (specifier.startsWith('/pages/') && this.isPageModule(specifier)) {
this.#pageRouting.update(...this.createRouteUpdate(specifier))
} else if (specifier.startsWith('/api/') && !specifier.startsWith('/api/_middlewares.')) {
this.#apiRouting.update(...this.createRouteUpdate(specifier))
}
Object.assign(module, { source })
return module
}

Expand Down Expand Up @@ -1025,6 +1031,21 @@ export class Aleph implements IAleph {
})
}

resolveImport({ jsFile, sourceHash }: Module, importer: string, bundleMode?: boolean, timeStamp?: boolean): string {
const relPath = toRelativePath(
dirname(toLocalPath(importer)),
jsFile
)
if (bundleMode) {
return util.trimSuffix(relPath, '.js') + '.bundling.js'
}
let hash = '#' + sourceHash.slice(0, 8)
if (timeStamp) {
hash += '-' + Date.now()
}
return relPath + hash
}

async resolveModuleSource(specifier: string, data?: any): Promise<ModuleSource> {
let sourceCode: string = ''
let sourceType: SourceType = SourceType.Unknown
Expand Down Expand Up @@ -1152,14 +1173,9 @@ export class Aleph implements IAleph {

if (!forceRefresh && await existsFile(metaFp)) {
try {
const { specifier: _specifier, sourceHash, deps, isStyle, ssrPropsFn, ssgPathsFn, denoHooks } = JSON.parse(await Deno.readTextFile(metaFp))
if (_specifier === specifier && util.isFilledString(sourceHash) && util.isArray(deps)) {
mod.sourceHash = sourceHash
mod.deps = deps
mod.isStyle = Boolean(isStyle) || undefined
mod.ssrPropsFn = util.isFilledString(ssrPropsFn) ? ssrPropsFn : undefined
mod.ssgPathsFn = Boolean(ssgPathsFn) || undefined
mod.denoHooks = util.isFilledArray(denoHooks) ? denoHooks : undefined
const meta = JSON.parse(await Deno.readTextFile(metaFp))
if (meta.specifier === specifier && util.isFilledString(meta.sourceHash) && util.isArray(meta.deps)) {
Object.assign(mod, meta)
} else {
log.warn(`removing invalid metadata '${name}.meta.json'`)
Deno.remove(metaFp)
Expand Down Expand Up @@ -1413,22 +1429,14 @@ export class Aleph implements IAleph {
}

private async cacheModule(module: Module, sourceMap?: string) {
const { specifier, jsBuffer, jsFile } = module
const { jsBuffer, jsFile, ready, ...rest } = module
if (jsBuffer) {
const cacheFp = join(this.#buildDir, jsFile)
const metaFp = cacheFp.slice(0, -3) + '.meta.json'
await ensureDir(dirname(cacheFp))
await Promise.all([
Deno.writeFile(cacheFp, jsBuffer),
Deno.writeTextFile(metaFp, JSON.stringify({
specifier,
sourceHash: module.sourceHash,
isStyle: module.isStyle,
ssrPropsFn: module.ssrPropsFn,
ssgPathsFn: module.ssgPathsFn,
denoHooks: module.denoHooks,
deps: module.deps,
}, undefined, 2)),
Deno.writeTextFile(metaFp, JSON.stringify({ ...rest }, undefined, 2)),
sourceMap ? Deno.writeTextFile(`${cacheFp}.map`, sourceMap) : Promise.resolve(),
lazyRemove(cacheFp.slice(0, -3) + '.bundling.js'),
])
Expand Down
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Aleph {
addDist(path: string, content: Uint8Array): Promise<void>
addModule(specifier: string, sourceCode: string, forceRefresh?: boolean): Promise<Module>
fetchModule(specifier: string): Promise<{ content: Uint8Array, contentType: string | null }>
resolveImport(module: Module, importer: string, bundleMode?: boolean, timeStamp?: boolean): string
onResolve(test: RegExp, resolve: (specifier: string) => ResolveResult): void
onLoad(test: RegExp, load: (input: LoadInput) => LoadOutput | Promise<LoadOutput>): void
onTransform(test: 'hmr' | 'main' | RegExp, transform: (input: TransformInput) => TransformOutput | void | Promise<TransformOutput | void>): void
Expand Down Expand Up @@ -91,6 +92,7 @@ export type LoadOutput = {
export type TransformInput = {
module: Omit<Module, 'jsBuffer' | 'ready'>
code: string
bundleMode?: boolean
map?: string
}

Expand Down
2 changes: 1 addition & 1 deletion version.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** `VERSION` managed by https://deno.land/x/publish */
export const VERSION = '0.3.0-beta.9'
export const VERSION = '0.3.0-beta.10'

/** `prepublish` will be invoked before publish */
export async function prepublish(version: string): Promise<boolean> {
Expand Down

0 comments on commit 858be54

Please sign in to comment.