This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement *.pcci.ts / *.js.ts bundling #40
- Loading branch information
Showing
4 changed files
with
239 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { fs } from "../deps.ts"; | ||
import * as govn from "../../governance/mod.ts"; | ||
import * as br from "../../core/resource/module/bundle.ts"; | ||
import * as p from "../../core/std/persist.ts"; | ||
|
||
export function bundleProducer<State>( | ||
destRootPath: string, | ||
state: State, | ||
options?: { | ||
readonly namingStrategy?: p.LocalFileSystemNamingStrategy< | ||
govn.RouteSupplier<govn.RouteNode> | ||
>; | ||
readonly eventsEmitter?: govn.FileSysPersistEventsEmitterSupplier; | ||
}, | ||
// deno-lint-ignore no-explicit-any | ||
): govn.ResourceRefinery<any> { | ||
const namingStrategy = options?.namingStrategy || | ||
p.routePersistForceExtnNamingStrategy(".js"); | ||
return async (resource) => { | ||
if (br.isBundleResource(resource)) { | ||
await p.persistFlexibleFileCustom( | ||
resource, | ||
namingStrategy( | ||
resource as unknown as govn.RouteSupplier<govn.RouteNode>, | ||
destRootPath, | ||
), | ||
{ | ||
ensureDirSync: fs.ensureDirSync, | ||
functionArgs: [state], | ||
eventsEmitter: options?.eventsEmitter, | ||
}, | ||
); | ||
} | ||
return resource; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { fs } from "../../deps.ts"; | ||
import * as govn from "../../../governance/mod.ts"; | ||
import * as c from "../../../core/std/content.ts"; | ||
import * as fsrf from "../../originate/file-sys-globs.ts"; | ||
import * as route from "../../../core/std/route.ts"; | ||
import * as nature from "../../../core/std/nature.ts"; | ||
import * as p from "../../../core/std/persist.ts"; | ||
import * as safety from "../../../lib/safety/mod.ts"; | ||
|
||
export interface FileSysResourceBundleConstructor<State> { | ||
( | ||
we: fsrf.FileSysGlobWalkEntry<BundleResource>, | ||
options: route.FileSysRouteOptions, | ||
imported: govn.ExtensionModule, | ||
state: State, | ||
): Promise<BundleResource>; | ||
} | ||
|
||
export interface BundleResource | ||
extends | ||
govn.TextSupplier, | ||
govn.TextSyncSupplier, | ||
govn.NatureSupplier< | ||
& govn.MediaTypeNature<govn.TextSupplier & govn.TextSyncSupplier> | ||
& govn.FileSysPersistenceSupplier<BundleResource> | ||
>, | ||
govn.RouteSupplier, | ||
Partial<govn.FrontmatterSupplier<govn.UntypedFrontmatter>>, | ||
Partial<govn.DiagnosticsSupplier> { | ||
readonly isClientJavascriptBundle?: boolean; | ||
} | ||
|
||
export const isBundleResourceType = safety.typeGuard<BundleResource>( | ||
"nature", | ||
"text", | ||
"textSync", | ||
"isClientJavascriptBundle", | ||
); | ||
|
||
export function isBundleResource(o: unknown): o is BundleResource { | ||
if (isBundleResourceType(o)) { | ||
return o.isClientJavascriptBundle ? true : false; | ||
} | ||
return false; | ||
} | ||
|
||
export const bundleMediaTypeNature: govn.MediaTypeNature<BundleResource> = { | ||
mediaType: "text/javascript", | ||
guard: (o: unknown): o is BundleResource => { | ||
if ( | ||
nature.isNatureSupplier(o) && nature.isMediaTypeNature(o.nature) && | ||
o.nature.mediaType === bundleMediaTypeNature.mediaType && | ||
(c.isTextSupplier(o) && c.isTextSyncSupplier(o)) | ||
) { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}; | ||
|
||
export const bundleContentNature: | ||
& govn.MediaTypeNature<govn.TextResource> | ||
& govn.TextSuppliersFactory | ||
& govn.FileSysPersistenceSupplier<BundleResource> | ||
& govn.RenderTargetsSupplier<govn.MediaTypeNature<govn.TextResource>> = { | ||
mediaType: bundleMediaTypeNature.mediaType, | ||
guard: bundleMediaTypeNature.guard, | ||
prepareText: nature.prepareText, | ||
renderTargets: [bundleMediaTypeNature], | ||
persistFileSysRefinery: (rootPath, namingStrategy, eventsEmitter) => { | ||
return async (resource) => { | ||
if (c.isTextSupplier(resource)) { | ||
await p.persistFlexibleFileCustom( | ||
resource, | ||
namingStrategy(resource, rootPath), | ||
{ ensureDirSync: fs.ensureDirSync, eventsEmitter }, | ||
); | ||
} | ||
return resource; | ||
}; | ||
}, | ||
persistFileSys: async ( | ||
resource, | ||
rootPath, | ||
namingStrategy, | ||
eventsEmitter, | ||
) => { | ||
if (c.isTextSupplier(resource)) { | ||
await p.persistFlexibleFileCustom( | ||
resource, | ||
namingStrategy(resource, rootPath), | ||
{ ensureDirSync: fs.ensureDirSync, eventsEmitter }, | ||
); | ||
} | ||
}, | ||
}; | ||
|
||
export function bundleFileSysResourceFactory( | ||
isClientJavascriptBundle: boolean, | ||
refine?: govn.ResourceRefinery<BundleResource>, | ||
): fsrf.FileSysGlobWalkEntryFactory<BundleResource> { | ||
return { | ||
construct: async (origination, options) => { | ||
// doing this will make PCII available at the server side | ||
const imported = await options.extensionsManager.importModule( | ||
origination.path, | ||
); | ||
let content = "// bundler did not run?"; | ||
if (imported.isValid) { | ||
try { | ||
const { files, diagnostics } = await Deno.emit(origination.path, { | ||
bundle: "classic", | ||
compilerOptions: { | ||
lib: ["deno.unstable", "deno.window"], | ||
}, | ||
}); | ||
if (diagnostics.length) { | ||
content = Deno.formatDiagnostics(diagnostics); | ||
} else { | ||
content = files["deno:///bundle.js"]; | ||
} | ||
} catch (e) { | ||
content = `// Error emitting ${origination.path}: ${e}`; | ||
} | ||
} else { | ||
content = | ||
`// Invalid Module ${origination.path}: ${imported.importError}`; | ||
} | ||
const nature = bundleContentNature; | ||
const result: | ||
& BundleResource | ||
& govn.RouteSupplier = { | ||
isClientJavascriptBundle, | ||
nature, | ||
route: { | ||
...origination.route, | ||
nature, | ||
}, | ||
// deno-lint-ignore require-await | ||
text: async () => content, | ||
textSync: () => content, | ||
}; | ||
return result; | ||
}, | ||
refine, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters