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 tooling changes #2922

Merged
merged 12 commits into from
Jun 3, 2024
Merged

Build tooling changes #2922

merged 12 commits into from
Jun 3, 2024

Conversation

vladmoroz
Copy link
Collaborator

Changing build tooling to esbuild and (temporarily) tsup in order to support 'use client' directives up next. (Currently, Parcel strips them out, see parcel-bundler/parcel#9050). Also sets us free from a bunch of other workarounds related to the current setup.

Comment on lines -21 to -23
"// build": "For context on tsconfig replacements in build scripts, see https://github.com/radix-ui/primitives/pull/361#discussion_r555004944",
"build": "yarn build:config && yarn build:packages && yarn build:cleanup",
"build:config": "mv tsconfig.json tsconfig.tmp.json && mv tsconfig.production.json tsconfig.json",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This dance with configs isn't needed anymore

Comment on lines -23 to -36
module.exports = {
presets: [
[
'@parcel/babel-preset-env',
{
bugfixes: true,
targets: {
browsers: 'Chrome >= 74, Safari >= 13.1, iOS >= 13.3, Firefox >= 78, Edge >= 79',
node: 12,
},
},
],
'@babel/preset-react',
],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Downtranspilation is retained in esbuild via a newer ES2022 target. Realistically, with the JS features we use, we'd support browsers up to 4 years old.

Comment on lines -5 to -21
const pureDisplayNames = () => ({
visitor: {
AssignmentExpression(path) {
if (
path.node.left.type === 'MemberExpression' &&
path.node.left.property.name === 'displayName' &&
path.node.right.name
) {
const COMPONENT = path.node.left.object.name;
const DISPLAY_NAME = path.node.right.name;
const ast = buildAssign({ COMPONENT, DISPLAY_NAME });
path.replaceWith(ast);
path.addComment('leading', '#__PURE__');
}
},
},
});
Copy link
Collaborator Author

@vladmoroz vladmoroz May 28, 2024

Choose a reason for hiding this comment

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

Have dug up some history on this one:

Since each package is bundled into a single file, pure annotations were added in key places to support tree-shaking. This doesn't seem bulletproof, as I've seen them missing in some places, and not every build tool supports the annotations.

I'm thinking that instead, we should make tree-shaking possible naturally via:

  • Have a file per part
  • Don't bundle

That will allow build tools to include or omit the relevant imports naturally, and allow us to simplify our toolchain as well. We've already worked out how to do that in Radix Themes, including dual CJS/ESM in that setup (which is currently the only thing that bundling really simplifies), so I already have a good idea on the next steps.

@@ -1,8 +1,6 @@
import React from 'react';
Copy link
Collaborator Author

@vladmoroz vladmoroz May 28, 2024

Choose a reason for hiding this comment

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

Had to please the pre-push hooks and remove the unused import (how has this stuff worked before?)

Other changes in this file are due to my formatting on save

"build": "yarn build:config && yarn build:packages && yarn build:cleanup",
"build:config": "mv tsconfig.json tsconfig.tmp.json && mv tsconfig.production.json tsconfig.json",
"build:packages": "parcel build 'packages/*/*/' --no-cache && yarn build:fix-type-defs",
"build:fix-type-defs": "node ./scripts/fix-type-defs-imports",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No need to fix the bugged imports anymore

Comment on lines +30 to +46
// tsup is used to emit d.ts files only (esbuild can't do that).
//
// Notes:
// 1. Emitting d.ts files is super slow for whatever reason.
// 2. It could have fully replaced esbuild (as it uses that internally),
// but at the moment its esbuild version is somewhat outdated.
// It’s also harder to configure and esbuild docs are more thorough.
await tsup.build({
entry: [file],
format: ['cjs', 'esm'],
dts: { only: true },
outDir: dist,
silent: true,
external: [/@radix-ui\/.+/],
});
console.log(`Built ${path}/dist/index.d.ts`);
}
Copy link
Collaborator Author

@vladmoroz vladmoroz May 28, 2024

Choose a reason for hiding this comment

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

tsup gets the job done, but this is temporary—we'll probably just use tsc to compile the type definitions when stuff is updated to not need bundling in the first place:
#2922 (comment)

Comment on lines +20 to +29
swc: () => ({
jsc: {
transform: {
react: {
// Do not require importing React into scope to use JSX
runtime: 'automatic',
},
},
},
}),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Somehow:

  • pre-push hooks didn't like unused React imports in some stories (how did folks push to their branches before?)
  • Stories without React in scope won't compile

@vladmoroz
Copy link
Collaborator Author

@hadihallak yep, I confirmed that "splitting" and React in the external don't do anything right now, this was a remnant from a different setup I was exploring when these were useful

@vladmoroz vladmoroz merged commit 107dd05 into main Jun 3, 2024
5 checks passed
@vladmoroz vladmoroz deleted the vlad/esbuild branch June 3, 2024 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants