From 1238dee05ea136eeb877e67a3902c85b191606ef Mon Sep 17 00:00:00 2001 From: "Daniel J. Rollins" Date: Thu, 5 Sep 2024 17:05:25 +0100 Subject: [PATCH] copy assets to sub-directory (default assets) --- src/documents.ts | 13 ++- src/plugin.ts | 3 + test/__snapshots__/test.ts.snap | 142 +++++++++++++------------------- test/test.ts | 3 +- 4 files changed, 70 insertions(+), 91 deletions(-) diff --git a/src/documents.ts b/src/documents.ts index a5ec500..15ef9bf 100644 --- a/src/documents.ts +++ b/src/documents.ts @@ -6,6 +6,7 @@ import fsp from "node:fs/promises"; export interface DocumentPluginOptions { importStubs: Record; verbose?: boolean; + assetDir: string; onEnd?: ( result: esbuild.BuildResult, importStubs: Record, @@ -23,6 +24,7 @@ export interface DocumentContextOptions { documents: string[]; build?: esbuild.PluginBuild["esbuild"]; worldDocuments?: Set; + assetDir: string; verbose?: boolean; onEnd?: ( result: esbuild.BuildResult, @@ -36,6 +38,7 @@ export async function documentContext({ options, worldDocuments = new Set(), verbose, + assetDir, onEnd, build = esbuild, }: DocumentContextOptions): Promise { @@ -46,6 +49,7 @@ export async function documentContext({ format: "iife", plugins: [ documentPlugin({ + assetDir, importStubs, verbose, onEnd, @@ -70,6 +74,7 @@ export async function documentContext({ format: "iife", plugins: [ documentPlugin({ + assetDir, importStubs, verbose, onEnd, @@ -109,7 +114,7 @@ const nonAssetExtensions = new Set([ ]); export function documentPlugin(args: DocumentPluginOptions): esbuild.Plugin { - const { verbose, importStubs, onEnd } = args; + const { verbose, importStubs, assetDir, onEnd } = args; const log = verbose ? (...args: unknown[]) => { console.log("[mml-world]:", ...args); @@ -183,7 +188,7 @@ export function documentPlugin(args: DocumentPluginOptions): esbuild.Plugin { }); // TODO: add this to the metafile - const output = path.resolve(outdir, basename(args.path)); + const output = path.resolve(outdir, assetDir, basename(args.path)); const entrypoint = path.relative(process.cwd(), args.path); assets.push({ output, entrypoint }); @@ -231,7 +236,9 @@ export function documentPlugin(args: DocumentPluginOptions): esbuild.Plugin { ...((build.initialOptions.entryPoints ?? []) as string[]), ...discoveredDocuments, ], - plugins: [documentPlugin({ importStubs, verbose, onEnd })], + plugins: [ + documentPlugin({ importStubs, verbose, assetDir, onEnd }), + ], }); return; diff --git a/src/plugin.ts b/src/plugin.ts index e146a46..109b36e 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -15,6 +15,7 @@ export interface MMLPluginOptions { outputProcessor?: OutputProcessorProvider; documentPrefix?: string; assetPrefix?: string; + assetDir?: string; importPrefix?: string; } @@ -24,6 +25,7 @@ export function mml(args: MMLPluginOptions = {}): esbuild.Plugin { outputProcessor: outputProcessorProvider, importPrefix, assetPrefix = "", + assetDir = "assets", } = args; let { documentPrefix = "ws:///" } = args; @@ -78,6 +80,7 @@ export function mml(args: MMLPluginOptions = {}): esbuild.Plugin { const documentCtx = await documentContext({ build: build.esbuild, documents, + assetDir, options: initialOptions, onEnd: async (result, importStubs) => { processor.pushResult("document", result, importStubs); diff --git a/test/__snapshots__/test.ts.snap b/test/__snapshots__/test.ts.snap index 1790dfb..bbeb31c 100644 --- a/test/__snapshots__/test.ts.snap +++ b/test/__snapshots__/test.ts.snap @@ -6,7 +6,7 @@ exports[`context rebuild: generated/relative-outpath/context/rebuild/a.html/1 1` var b_default = "ws:///b.html"; var d_default = "ws:///c/d.html"; var c = "wat-" + d_default; - var duck_default = "duck.glb"; + var duck_default = "assets/duck.glb"; console.log(b_default, d_default, duck_default); })(); " @@ -18,7 +18,7 @@ exports[`context rebuild: generated/relative-outpath/context/rebuild/a.html/2 1` var b_default = "ws:///b.html"; var d_default = "ws:///c/d.html"; var c = "wat-" + d_default; - var duck_default = "duck.glb"; + var duck_default = "assets/duck.glb"; var e_default = "ws:///e.html"; console.log(b_default, d_default, duck_default); console.log(e_default); @@ -32,12 +32,18 @@ exports[`context rebuild: generated/relative-outpath/context/rebuild/a.html/3 1` var b_default = "ws:///b.html"; var d_default = "ws:///c/d.html"; var c = "wat-" + d_default; - var duck_default = "duck.glb"; + var duck_default = "assets/duck.glb"; console.log(b_default, d_default, duck_default); })(); " `; +exports[`context rebuild: generated/relative-outpath/context/rebuild/assets/duck.glb/1 1`] = `"duck.glb"`; + +exports[`context rebuild: generated/relative-outpath/context/rebuild/assets/duck.glb/2 1`] = `"duck.glb"`; + +exports[`context rebuild: generated/relative-outpath/context/rebuild/assets/duck.glb/3 1`] = `"duck.glb"`; + exports[`context rebuild: generated/relative-outpath/context/rebuild/b.html/1 1`] = ` "" @@ -162,12 +162,16 @@ exports[`context watch: generated/relative-outpath/context/watch/a.html/2 1`] = var b_default = "ws:///b.html"; var d_default = "ws:///c/d.html"; var c = "wat-" + d_default; - var duck_default = "duck.glb"; + var duck_default = "assets/duck.glb"; console.log(b_default, d_default, duck_default); })(); " `; +exports[`context watch: generated/relative-outpath/context/watch/assets/duck.glb/1 1`] = `"duck.glb"`; + +exports[`context watch: generated/relative-outpath/context/watch/assets/duck.glb/2 1`] = `"duck.glb"`; + exports[`context watch: generated/relative-outpath/context/watch/b.html/1 1`] = ` "" `; +exports[`mml plugin with absolute outdir path with default options: generated/absolute-outpath/default-options/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with absolute outdir path with default options: generated/absolute-outpath/default-options/b.html 1`] = ` "" `; +exports[`mml plugin with absolute outdir path with import prefix: generated/absolute-outpath/import-prefix/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with absolute outdir path with import prefix: generated/absolute-outpath/import-prefix/b.html 1`] = ` "" `; +exports[`mml plugin with absolute outdir path with new import and path from output processor: generated/absolute-outpath/new-import-and-path/flump/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with absolute outdir path with new import and path from output processor: generated/absolute-outpath/new-import-and-path/flump/b.html 1`] = ` "" `; +exports[`mml plugin with absolute outdir path with new import from output processor: generated/absolute-outpath/new-import/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with absolute outdir path with new import from output processor: generated/absolute-outpath/new-import/b.html 1`] = ` "" `; +exports[`mml plugin with absolute outdir path with new path from output processor: generated/absolute-outpath/new-path/bar/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with absolute outdir path with new path from output processor: generated/absolute-outpath/new-path/bar/b.html 1`] = ` "" `; +exports[`mml plugin with absolute outdir path world: generated/absolute-outpath/world/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with absolute outdir path world: generated/absolute-outpath/world/b.html 1`] = ` "" `; +exports[`mml plugin with relative outdir path with default options: generated/relative-outpath/default-options/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with relative outdir path with default options: generated/relative-outpath/default-options/b.html 1`] = ` "" `; +exports[`mml plugin with relative outdir path with import prefix: generated/relative-outpath/import-prefix/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with relative outdir path with import prefix: generated/relative-outpath/import-prefix/b.html 1`] = ` "" `; +exports[`mml plugin with relative outdir path with new import and path from output processor: generated/relative-outpath/new-import-and-path/flump/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with relative outdir path with new import and path from output processor: generated/relative-outpath/new-import-and-path/flump/b.html 1`] = ` "" `; +exports[`mml plugin with relative outdir path with new import from output processor: generated/relative-outpath/new-import/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with relative outdir path with new import from output processor: generated/relative-outpath/new-import/b.html 1`] = ` "" `; +exports[`mml plugin with relative outdir path with new path from output processor: generated/relative-outpath/new-path/bar/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with relative outdir path with new path from output processor: generated/relative-outpath/new-path/bar/b.html 1`] = ` "" `; +exports[`mml plugin with relative outdir path world: generated/relative-outpath/world/assets/duck.glb 1`] = `"duck.glb"`; + exports[`mml plugin with relative outdir path world: generated/relative-outpath/world/b.html 1`] = ` "" `; -exports[`resultProcessor re-writes import paths for documents: generated/relative-outpath/result-processor/world/a.html 2`] = ` -"" -`; +exports[`resultProcessor re-writes import paths for documents: generated/relative-outpath/result-processor/world/assets/duck.glb 1`] = `"duck.glb"`; exports[`resultProcessor re-writes import paths for documents: generated/relative-outpath/result-processor/world/b.html 1`] = ` "" `; -exports[`resultProcessor re-writes import paths for documents: generated/relative-outpath/result-processor/world/b.html 2`] = ` -"" -`; - exports[`resultProcessor re-writes import paths for documents: generated/relative-outpath/result-processor/world/c/d.html 1`] = ` " @@ -746,19 +727,6 @@ exports[`resultProcessor re-writes import paths for documents: generated/relativ " `; -exports[`resultProcessor re-writes import paths for documents: generated/relative-outpath/result-processor/world/c/d.html 2`] = ` -" - -

I'm html!

- - -" -`; - -exports[`resultProcessor re-writes import paths for documents: generated/relative-outpath/result-processor/world/duck.glb 1`] = `"duck.glb"`; - -exports[`resultProcessor re-writes import paths for documents: generated/relative-outpath/result-processor/world/duck.glb 2`] = `"duck.glb"`; - exports[`worldContext creates world JSON: generated/relative-outpath/world-context/world.js 1`] = ` ""use strict"; var __defProp = Object.defineProperty; diff --git a/test/test.ts b/test/test.ts index 4619c1c..dd123b8 100644 --- a/test/test.ts +++ b/test/test.ts @@ -105,6 +105,7 @@ describe("resultProcessor", () => { ctx = await documentContext({ documents: ["test/src/a.ts"], options: { outdir }, + assetDir: "assets", onEnd: async (result, importStubs) => { processor.pushResult("document", result, importStubs); await processor.process(); @@ -132,6 +133,7 @@ describe("documentContext", () => { ctx = await documentContext({ documents: ["test/src/a.ts"], options: { outdir }, + assetDir: "assets", onEnd: async (_result, importStubs) => { for await (const { path, content } of walk(outdir)) { expect(content).toMatchSnapshot(path); @@ -215,7 +217,6 @@ describe("mml plugin", () => { entryPoints: ["mml:test/src/a.ts"], plugins: [ mml({ - verbose: true, outputProcessor: () => ({ onOutput(output) { return { path: path.join("bar/", output) };