hanami / assets-js

esbuild plugin for Hanami Assets
3 stars 5 forks source link

Export `run` function as a streamlined but flexible main entrypoint #10

Closed timriley closed 1 year ago

timriley commented 1 year ago

Introduce a new assets.run entrypoint function that is:

Here's what a default config/assets.mjs file would look like using this API:

import * as assets from "hanami-assets";

await assets.run();

And here's how it would look to add a custom esbuild plugin:

import * as assets from "hanami-assets";
import postcss from "esbuild-postcss";

await assets.run({
  esbuildOptionsFn: (args, esbuildOptions) => {
    const plugins = [...esbuildOptions.plugins, postcss()];

    return {
      ...esbuildOptions,
      plugins,
    };
  },
});

I've tested this exact config above in a real Hanami app and can confirm it actually works in terms of activating esbuild plugins.


In terms of code changes here, they are fairly wide-ranging. Sorry for the messy diff.

Let me guide you through the new landscape, however:

That really sums it up! If you spend most of your time checking out index.ts, args.ts and skimming esbuild.ts, you'll be up to speed with the changes.


There are a few other things I took care of while I was here:


For full context of how this will be used within a full Hanami app, see https://github.com/hanami/cli/pull/109.

timriley commented 1 year ago

Lastly, apologies for the messy commit history here. This took a few twists and turns (and some of the usual "Tim figures out how to JavaScript again") before I got everything settled.

This is going to be squashed on merge anyway, and I'll make sure there's a good single commit message then. In the meantime, please see my overview in the PR description for the best way of inspecting the changes in this PR.