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

chore: Add an ESM submodule export to the build output #1129

Merged
merged 10 commits into from
Sep 26, 2024
Merged

Conversation

goulinkh
Copy link
Contributor

@goulinkh goulinkh commented Sep 9, 2024

Done

  • Leave the current behaviour as it is to avoid breaking existing projects
  • Add an additional export, ESM flavour as some frameworks (Astro.js in my case) doesn't work with CommonJS

@webteam-app
Copy link

@goulinkh goulinkh changed the title Add an ESM submodule export to the build output chore: Add an ESM submodule export to the build output Sep 9, 2024
package.json Outdated
Comment on lines 130 to 131
"build": "rm -rf dist && yarn build-cjs && yarn build-declaration && yarn build-esm && yarn build-declaration-esm",
"build-cjs": "NODE_ENV=production BABEL_ENV=cjs babel src --out-dir dist --copy-files --extensions '.js,.jsx,.ts,.tsx'",
Copy link
Member

Choose a reason for hiding this comment

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

As-is, this outputs CJS into dist and esm into dist/esm. This makes it possible for CJS output files to be in the same directory if the input filestructure is wrong.

It may be a bit cleaner to also put cjs into its own folder (dist/cjs) and update the package index to dist/cjs/index.js.

I think we'd then need to update package.json main to dist/cjs/index.js and set module to dist/esm/index.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand your point, and I agree with you. Having both types separated into two different directories is much cleaner.
The reason for leaving the cjs version as it is, is mainly to avoid breaking existing projects that are using this package with absolute paths to components. I'm sure that these imports aren't needed as everything should be importable from /dist/cjs/index.js.
Here is a set of projects that have this problem, this list is probably only a subset of the total projects that have this issue: https://github.com/search?q=org%3Acanonical+canonical%2Freact-components%2Fdist&type=code

Copy link
Contributor

Choose a reason for hiding this comment

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

I would still tend to have them in two separate folders. I believe having them in the same is suboptimal and can lead to a lot of headaches - and will impact future maintainability.

A solution could be to remap manually the user imports using the package.json exports field - it allows for a cleaner folder structure but is not necessarily elegant.

If we accept the two folders one in the other, I believe we should clearly document this somewhere as part of this PR. If this helps we could do that for the time being - pending the new architecture.

@advl
Copy link
Contributor

advl commented Sep 10, 2024

@goulinkh Have you tested locally that the esm build works as intended ?

We should also check that the CI is not affected (cc @jmuzina )

@goulinkh
Copy link
Contributor Author

goulinkh commented Sep 10, 2024

@advl Yes it works! I'm currently using it in Astro and it works as expected. My changes handle Jest, Storybook with Cypress.

@jmuzina
Copy link
Member

jmuzina commented Sep 10, 2024

We should also check that the CI is not affected (cc @jmuzina )

@advl CI is working OK, it passed the checks on this PR (which includes using the build command that has been updated to also build ESM).

package.json Outdated
Comment on lines 130 to 131
"build": "rm -rf dist && yarn build-cjs && yarn build-declaration && yarn build-esm && yarn build-declaration-esm",
"build-cjs": "NODE_ENV=production BABEL_ENV=cjs babel src --out-dir dist --copy-files --extensions '.js,.jsx,.ts,.tsx'",
Copy link
Contributor

Choose a reason for hiding this comment

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

I would still tend to have them in two separate folders. I believe having them in the same is suboptimal and can lead to a lot of headaches - and will impact future maintainability.

A solution could be to remap manually the user imports using the package.json exports field - it allows for a cleaner folder structure but is not necessarily elegant.

If we accept the two folders one in the other, I believe we should clearly document this somewhere as part of this PR. If this helps we could do that for the time being - pending the new architecture.

babel.config.js Outdated Show resolved Hide resolved
yarn.lock Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
.storybook/main.js Show resolved Hide resolved
@goulinkh goulinkh requested a review from advl September 16, 2024 15:49
@jmuzina
Copy link
Member

jmuzina commented Sep 17, 2024

Demo failed to build, rebuilding to verify things work OK

@advl advl merged commit e0b66f7 into main Sep 26, 2024
9 of 10 checks passed
Copy link

🎉 This PR is included in version 1.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

jmuzina added a commit to jmuzina/react-components that referenced this pull request Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants