Skip to content

Commit

Permalink
copy assets to sub-directory (default assets)
Browse files Browse the repository at this point in the history
  • Loading branch information
deej-io committed Sep 5, 2024
1 parent 2a496c3 commit 1238dee
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 91 deletions.
13 changes: 10 additions & 3 deletions src/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fsp from "node:fs/promises";
export interface DocumentPluginOptions {
importStubs: Record<string, string>;
verbose?: boolean;
assetDir: string;
onEnd?: (
result: esbuild.BuildResult,
importStubs: Record<string, string>,
Expand All @@ -23,6 +24,7 @@ export interface DocumentContextOptions {
documents: string[];
build?: esbuild.PluginBuild["esbuild"];
worldDocuments?: Set<string>;
assetDir: string;
verbose?: boolean;
onEnd?: (
result: esbuild.BuildResult,
Expand All @@ -36,6 +38,7 @@ export async function documentContext({
options,
worldDocuments = new Set<string>(),
verbose,
assetDir,
onEnd,
build = esbuild,
}: DocumentContextOptions): Promise<DocumentContext> {
Expand All @@ -46,6 +49,7 @@ export async function documentContext({
format: "iife",
plugins: [
documentPlugin({
assetDir,
importStubs,
verbose,
onEnd,
Expand All @@ -70,6 +74,7 @@ export async function documentContext({
format: "iife",
plugins: [
documentPlugin({
assetDir,
importStubs,
verbose,
onEnd,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface MMLPluginOptions {
outputProcessor?: OutputProcessorProvider;
documentPrefix?: string;
assetPrefix?: string;
assetDir?: string;
importPrefix?: string;
}

Expand All @@ -24,6 +25,7 @@ export function mml(args: MMLPluginOptions = {}): esbuild.Plugin {
outputProcessor: outputProcessorProvider,
importPrefix,
assetPrefix = "",
assetDir = "assets",
} = args;
let { documentPrefix = "ws:///" } = args;

Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 1238dee

Please sign in to comment.