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: Bundle lib/api with ts-up #19269

Merged
merged 4 commits into from
Oct 3, 2022
Merged

Build: Bundle lib/api with ts-up #19269

merged 4 commits into from
Oct 3, 2022

Conversation

ndelangen
Copy link
Member

@ndelangen ndelangen commented Sep 27, 2022

related: #18732

@ndelangen ndelangen self-assigned this Sep 27, 2022
@ndelangen ndelangen added the maintenance User-facing maintenance tasks label Sep 27, 2022
Base automatically changed from tech/cleanup-old-bundle-script to next September 27, 2022 11:52
@ndelangen ndelangen changed the title migrate lib/api to ts-up build lib/api with ts-up Sep 27, 2022
@ndelangen
Copy link
Member Author

@shilman I think this works and is ready for merge!

@shilman shilman changed the title build lib/api with ts-up Build: Bundle lib/api with ts-up Sep 27, 2022
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

Looking good but some questions!

@@ -1,5 +1,7 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="./typings.d.ts" />
/// <reference types="node" />
/// <reference types="webpack-env" />
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this?

Copy link
Member Author

Choose a reason for hiding this comment

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

@@ -51,6 +51,9 @@
"@storybook/theming": "7.0.0-alpha.34",
"global": "^4.4.0"
},
"devDependencies": {
"@types/webpack-env": "^1.16.0"
Copy link
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think there's a bug with NX where it builds dependents without the --optimized flag

That might be the cause of a few issues... one of those issues I got fixed by adding this, but it's not really what i want to do..

I've reached out to Katarina already

Copy link
Member

Choose a reason for hiding this comment

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

Can you explain a little more why that leads to needing to add this? I'm not really following..

code/lib/api/src/shortcut.ts Show resolved Hide resolved
Copy link
Member

@tmeasday tmeasday left a comment

Choose a reason for hiding this comment

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

Similar questions to @shilman I guess, I'm not following the type stuff.

@@ -51,6 +51,9 @@
"@storybook/theming": "7.0.0-alpha.34",
"global": "^4.4.0"
},
"devDependencies": {
"@types/webpack-env": "^1.16.0"
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain a little more why that leads to needing to add this? I'm not really following..

Comment on lines +27 to +31
"./shortcut": {
"require": "./dist/shortcut.js",
"import": "./dist/shortcut.mjs",
"types": "./dist/shortcut.d.ts"
},
Copy link
Member

Choose a reason for hiding this comment

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

Is this something addons use?

Copy link
Member Author

Choose a reason for hiding this comment

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

lib/ui uses it

@@ -66,6 +66,8 @@
"@storybook/semver": "^7.3.2",
"@storybook/theming": "7.0.0-alpha.34",
"@testing-library/react": "^11.2.2",
"@types/node": "^14.0.10 || ^16.0.0",
"@types/webpack-env": "^1.16.0",
Copy link
Member

Choose a reason for hiding this comment

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

Don't we build these with esbuild, not webpack? Is it weird to depend on webpack env types?

Copy link
Member Author

Choose a reason for hiding this comment

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

module.hot.accept does not exist in node, that's a webpack runtime thing

Copy link
Member

Choose a reason for hiding this comment

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

We use that in lib/ui???

@@ -66,6 +66,8 @@
"@storybook/semver": "^7.3.2",
"@storybook/theming": "7.0.0-alpha.34",
"@testing-library/react": "^11.2.2",
"@types/node": "^14.0.10 || ^16.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't run in the node context, right? What node types are we using?

@ndelangen ndelangen merged commit ac4622d into next Oct 3, 2022
@ndelangen ndelangen deleted the tech/tsup-lib-api branch October 3, 2022 15:11
@ndelangen ndelangen mentioned this pull request Oct 3, 2022
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"

Choose a reason for hiding this comment

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

FYI, this export map is incorrect - the types condition will never be matched, since import or require will always be matched first (so we'll always be looking at the default location for the declaration file - adjacent to the source file). Moreover, you very likely want a dedicated declaration file for each of the .js and .mjs entrypoints, so TS knows the correct module format of the import for analysis. So you want something more like

  "exports": {
    ".": {
      "require": { "types": "./dist/index.d.ts", "default": "./dist/index.js" },
      "import": { "types": "./dist/index.d.mts", "default": "./dist/index.mjs" }
    },
    "./shortcut": {
      "require": { "types": "./dist/shortcut.d.ts", "default": "./dist/shortcut.js" },
      "import":  { "types": "./dist/shortcut.d.mts", "default": "./dist/shortcut.mjs" }
    },
    "./package.json": "./package.json"
  },

Naturally, you'll need those .d.mts files to actually exist.

Copy link
Member Author

@ndelangen ndelangen Oct 11, 2022

Choose a reason for hiding this comment

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

Thank you for this comment!

Moreover, you very likely want a dedicated declaration file for each of the .js and .mjs entrypoints

Can you explain why? what would be the difference in content of these 2 files?
What does typescript use the "correct module format of the import for analysis" for?

so we'll always be looking at the default location for the declaration file - adjacent to the source file

So this is why it works out in the end, right?
Could we simply remove the "types" line, then?

Choose a reason for hiding this comment

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

Can you explain why? what would be the difference in content of these 2 files?

The .d.ts file will be recognized as representing a common js module, while the .d.mts file will be recognized as representing a ecmascript module, which have a complex non-transparent interop scheme in node. Notably, it's an error to load esm from a common js module via require (and this can't be papered over with an import helper), so detecting this in advance requires source-accurate declaration file module format indicators.

The index.d.mts file can likely be export * from "./index.js"; if the named exports are meant to be similar. If there's meant to be a default member, you may need that, too.

What does typescript use the "correct module format of the import for analysis" for?

Knowing if the default import from an es module should be the whole module (because the target is cjs), knowing if the import should be an error (because the target is esm and the require'ing module is cjs).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you so much for taking the time to explain of of this to me (and others reading)!

Would you be able to assist us with this migration?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance User-facing maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants