Skip to content
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

build with esbuild; support Node 18+ #963

Merged
merged 26 commits into from
Mar 4, 2024
Merged

build with esbuild; support Node 18+ #963

merged 26 commits into from
Mar 4, 2024

Conversation

mbostock
Copy link
Member

@mbostock mbostock commented Mar 3, 2024

Fixes #850. Fixes #847.

@mbostock
Copy link
Member Author

mbostock commented Mar 3, 2024

Some additional learnings…

  • We had a number of imports of .json files without import attributes (or assertions). These only worked because tsx is loose, so we have to rewrite them to be explicit to make them standard JavaScript (since esbuild will not rewrite these imports).
  • Import attributes are only supported on more recent versions of tsx, so we have to upgrade tsx if we want to continue using it for tests.
  • However, more recent versions of tsx break code coverage with c8 (Coverage with c8 and tsx reporting as 100% since version 4.3.0 privatenumber/tsx#433), so this means we don’t have any code coverage… unless we can figure out how to run the tests using esbuild instead of tsx
  • It also means we need to upgrade Prettier. But I hate the new ternary formatting introduced in Prettier 3.1 (and more generally I hate the idea that our formatting styles are subject to the whims of the Prettier maintainers, and apparently they can evolve over time, causing churn in our codebase). So either we need to avoid import attributions so we can stick with Prettier <3.1, or plug our noises and upgrade to Prettier 3.1+, or drop Prettier entirely (perhaps in favor of some alternative but I’m not aware of one). I don’t like any of these options. (I’ll also note that there are 100+ // prettier-ignore directives in our repo, but most of them are for long lines in tests.)
  • If we drop tsx, then we won’t be able to support TypeScript project configuration files observablehq.config.ts, at least via direct import… so maybe we’ll need to use esbuild to load it on the fly? I guess we can do the latter and it shouldn’t be a big deal because we already have a runtime dependency on esbuild

Update: I bypassed most of this by using process.env.npm_package_version instead, and then baking it in using --define with esbuild. Hooray.

tsconfig.json Outdated Show resolved Hide resolved
src/bin/observable.ts Outdated Show resolved Hide resolved
@mbostock
Copy link
Member Author

mbostock commented Mar 3, 2024

Oh also, if we use import attributes, then we require Node 20.10 — I think that’s probably too bleeding edge (2023-11-22)? If we use the now-deprecated import assertions (and drop tsx at runtime), we can support Node 18. Maybe we can drop that in favor of process.env.npm_package_version? Not sure if that works in Yarn though… and also it’ll be wrong when Framework is run as a dependency, surely.

Update: process.env.npm_package_version worked great.

.DS_Store
.env
/build.test/
/build/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The leading slash here means only ignore build/ at the project root, not test/input/build/ etc.

@mbostock mbostock marked this pull request as ready for review March 3, 2024 20:16
@mbostock
Copy link
Member Author

mbostock commented Mar 3, 2024

I think the remaining issue is that I don’t know how to do the equivalent of $(find …) on Windows. We could use glob, but I still don’t know how to pass the result of that to the esbuild command.

@mbostock
Copy link
Member Author

mbostock commented Mar 4, 2024

I think I’ll have to write a script in JavaScript and use esbuild’s JavaScript API so that it works on Windows. 😞

@mbostock mbostock changed the title build with esbuild build with esbuild; support Node 18+ Mar 4, 2024
@mbostock mbostock enabled auto-merge (squash) March 4, 2024 08:15
@Fil
Copy link
Contributor

Fil commented Mar 4, 2024

I've tested it as much as I could (and could imagine how to). LGTM.

One thing that I found a bit confusing is that yarn build in this repo builds the documentation site (dist), as an alias of observable build. You need yarn prepublishOnly to build the build folder. Since this is just for us developers of Framework, it's probably OK, but we could also change this either to make it clearer by changing the commands names — or more simply by building both dist and build (it's 0.7s on my machine, so fast that it's not going to be a burden).

@mbostock
Copy link
Member Author

mbostock commented Mar 4, 2024

Can you approve if it looks good?

I thought about changing yarn dev to yarn docs:dev etc. but I don’t see a big benefit and it’ll be more disruptive. And I don’t think it makes sense to combine the two unrelated actions into one.

@mbostock mbostock merged commit 5805947 into main Mar 4, 2024
4 checks passed
@mbostock mbostock deleted the mbostock/esbuild branch March 4, 2024 14:40
@mbostock
Copy link
Member Author

mbostock commented Mar 4, 2024

Like really the current dist should be dist-docs or something, and the current build should be dist… but they’re all just names internal to this repo so I don’t think it matters.

@mbostock mbostock mentioned this pull request Mar 4, 2024
@visnup
Copy link
Member

visnup commented Mar 4, 2024

Should we add v18 to the GitHub Actions workflow matrix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants