Skip to content

Commit

Permalink
Use a specific interface for our esbuild options
Browse files Browse the repository at this point in the history
This will always have a plugins, so it's better to reflect that. This also makes it easier to use in `config/assets.mjs` files.
  • Loading branch information
timriley committed Oct 11, 2023
1 parent 7bf8c9e commit 6d34d18
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
9 changes: 6 additions & 3 deletions dist/esbuild.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { BuildOptions } from "esbuild";
import { BuildOptions, Plugin } from "esbuild";
import { Args } from "./args.js";
export declare const buildOptions: (root: string, args: Args) => Partial<BuildOptions>;
export declare const watchOptions: (root: string, args: Args) => Partial<BuildOptions>;
export interface EsbuildOptions extends Partial<BuildOptions> {
plugins: Plugin[];
}
export declare const buildOptions: (root: string, args: Args) => EsbuildOptions;
export declare const watchOptions: (root: string, args: Args) => EsbuildOptions;
5 changes: 3 additions & 2 deletions dist/hanami-assets.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env node
import { BuildOptions, BuildContext } from "esbuild";
import { BuildContext } from "esbuild";
import { Args } from "./args.js";
import { EsbuildOptions } from "./esbuild.js";
interface RunOptions {
root?: string;
argv?: string[];
esbuildOptionsFn?: EsbuildOptionsFn;
}
type EsbuildOptionsFn = (args: Args, esbuildOptions: Partial<BuildOptions>) => Partial<BuildOptions>;
type EsbuildOptionsFn = (args: Args, esbuildOptions: EsbuildOptions) => EsbuildOptions;
export declare const run: (options?: RunOptions) => Promise<BuildContext | void>;
export {};
14 changes: 9 additions & 5 deletions src/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import path from "path";
import { globSync } from "glob";
import { BuildOptions, Loader } from "esbuild";
import { BuildOptions, Loader, Plugin } from "esbuild";
import { Args } from "./args.js";
import hanamiEsbuild, { HanamiEsbuildPluginOptions, defaults } from "./hanami-esbuild-plugin.js";

export interface EsbuildOptions extends Partial<BuildOptions> {
plugins: Plugin[]
}

const loader: { [ext: string]: Loader } = {
".tsx": "tsx",
".ts": "ts",
Expand Down Expand Up @@ -77,14 +81,14 @@ const externalDirectories = (): string[] => {
};

// TODO: reuse the logic between these two methods below
export const buildOptions = (root: string, args: Args): Partial<BuildOptions> => {
export const buildOptions = (root: string, args: Args): EsbuildOptions => {
const pluginOptions: HanamiEsbuildPluginOptions = {
...defaults,
sriAlgorithms: args.sri || [],
};
const plugin = hanamiEsbuild(pluginOptions);

const options: Partial<BuildOptions> = {
const options: EsbuildOptions = {
bundle: true,
outdir: path.join(root, "public", "assets"),
absWorkingDir: root,
Expand All @@ -101,14 +105,14 @@ export const buildOptions = (root: string, args: Args): Partial<BuildOptions> =>
return options;
};

export const watchOptions = (root: string, args: Args): Partial<BuildOptions> => {
export const watchOptions = (root: string, args: Args): EsbuildOptions => {
const pluginOptions: HanamiEsbuildPluginOptions = {
...defaults,
hash: false,
};
const plugin = hanamiEsbuild(pluginOptions);

const options: Partial<BuildOptions> = {
const options: EsbuildOptions = {
bundle: true,
outdir: path.join(root, "public", "assets"),
absWorkingDir: root,
Expand Down
13 changes: 6 additions & 7 deletions src/hanami-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

import fs from "fs-extra";
import path from "path";
import esbuild, { BuildOptions, BuildContext } from "esbuild";
import esbuild, { BuildContext } from "esbuild";
import { Args, parseArgs } from "./args.js";
import { buildOptions, watchOptions } from "./esbuild.js";

import { EsbuildOptions, buildOptions, watchOptions } from "./esbuild.js";

interface RunOptions {
root?: string
argv?: string[]
esbuildOptionsFn?: EsbuildOptionsFn
root?: string;
argv?: string[];
esbuildOptionsFn?: EsbuildOptionsFn;
}

type EsbuildOptionsFn = (args: Args, esbuildOptions: Partial<BuildOptions>) => Partial<BuildOptions>;
type EsbuildOptionsFn = (args: Args, esbuildOptions: EsbuildOptions) => EsbuildOptions;

export const run = async function(options?: RunOptions): Promise<BuildContext | void> {
const {
Expand Down

0 comments on commit 6d34d18

Please sign in to comment.