-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export run
function as a streamlined but flexible main entrypoint
#10
Conversation
e5c84b3
to
e9fb1c6
Compare
e9fb1c6
to
f003370
Compare
Gosh, this tripped me up for like a good couple hours in total
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.
The intention is that this is run via a script only now.
Go with a shorter name for tidiness
const findEntryPoints = (root: string): Record<string, string> => { | ||
const result: Record<string, string> = {}; | ||
|
||
// TODO: should this be done explicitly within the root? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this TODO. It looks like we're finding files via these globs, but without actually specifying the root
from which it should be operating. This is probably only working because globSync
is implicitly working off the cwd?
return result; | ||
}; | ||
|
||
// TODO: feels like this really should be passed a root too, to become the cwd for globSync |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the other TODO I added here. The one other place in this file where we were doing file operations without providing an explicit root.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timriley IIRC, by adding the absolute path, Esbuild will report the absolute path in the metadata, so it's a bit cumbersome to work lately with them in the context of manifest generation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this is useful to know, thanks @jodosha!
The only downside of the current arrangement is that it's kind of contingent of the cwd also equalling the app root. In 99% of usage, I don't expect these things will diverge, but the fact that we're allowing a root:
to be provided to assets.run
means that some of these globs that reply on the cwd might start to fail.
However, I'm comfortable enough with us leaving this as a known gap for the moment. We can come back to solidify this aspect later.
And perhaps we don't document the assets.run
root:
option as "public" for now, too.
} | ||
}; | ||
|
||
// TODO: reuse the logic between these two methods below |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also added this TODO, but I'd rather leave it for another PR to refactor these. I left these lists of options mostly unchanged for the sake of continuity.
import type { JestConfigWithTsJest } from "ts-jest"; | ||
|
||
const config: JestConfigWithTsJest = { | ||
testEnvironment: "node", | ||
testMatch: ["**/test/**/*.test.ts"], | ||
// See https://kulshekhar.github.io/ts-jest/docs/guides/esm-support#esm-presets for below | ||
preset: "ts-jest/presets/default-esm", | ||
moduleNameMapper: { | ||
"^(\\.{1,2}/.*)\\.js$": "$1", | ||
}, | ||
transform: { | ||
"^.+\\.tsx?$": [ | ||
"ts-jest", | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
export default config; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole bit of boilerplate config was required after I changed the package.json
to include "type": "module"
and opt us into modern day ES Modules.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timriley On the other day you were "unreasonably excited" about these changes. Now I understand why: they are magnificent! They're taking this code base to the next level both in what they offer to end users, but also in quality of the internal code. Well done! 👏
Please check my inline comment about "bin"
in package.json
. 🙂
package.json
Outdated
@@ -1,12 +1,13 @@ | |||
{ | |||
"name": "hanami-assets", | |||
"version": "2.1.0-beta.2", | |||
"description": "Hanami Assets management via Esbuild", | |||
"type": "module", | |||
"description": "Hanami assets management via Esbuild", | |||
"bin": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timriley We must remove "bin"
as we don't have the executable anymore. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh yes, I thought I removed it and even mentioned in the PR description, but clearly did not 😆
return result; | ||
}; | ||
|
||
// TODO: feels like this really should be passed a root too, to become the cwd for globSync |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timriley IIRC, by adding the absolute path, Esbuild will report the absolute path in the metadata, so it's a bit cumbersome to work lately with them in the context of manifest generation.
Prepare for our updated approach to assets chiefly introduced with hanami/assets-js#10. Update `hanami new`: - Generate a new `config/assets.mjs` that will be needed for our new way of running the assets commands (see hanami/assets-js#10) - - Generate a leaner `package.json`, including an `"assets"` entry in the `"scripts"` section, referencing the above `config/assets.mjs` file. Update the `hanami assets compile` and `hanami assets watch` commands: - Expect to run assets via `npm run assets`, as generated in the new `package.json` above. - Have these use a single hanami-assets setting for running these commands: `package_manager_run_command` (introduced in hanami/assets#131), which defaults to `npm run`.
Introduce a new
assets.run
entrypoint function that is:Here's what a default
config/assets.mjs
file would look like using this API:And here's how it would look to add a custom esbuild plugin:
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:
run
function that serves as the main entry point, as shown above in thoseconfig/assets.mjs
examples. This accepts an object of options so that they can all be optional.node config/assets.mjs
inside Hanami apps. This makes sure t things are nicely typed when passing anesbuildOptionsFn
per the second example above.buildOptions
andwatchOptions
functions that return the combined set of options for the two modes that we use.That really sums it up! If you spend most of your time checking out
index.ts
,args.ts
and skimmingesbuild.ts
, you'll be up to speed with the changes.There are a few other things I took care of while I was here:
config/assets.mjs
script is now the one and only way to use this package.compilerOptions
to use"module": "NodeNext"
. Per their relevant docs:For full context of how this will be used within a full Hanami app, see hanami/cli#109.