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: updates to support Node ESM #3287

Merged
merged 25 commits into from
Jan 27, 2023
Merged

build: updates to support Node ESM #3287

merged 25 commits into from
Jan 27, 2023

Conversation

zchenwei
Copy link
Contributor

@zchenwei zchenwei commented Jan 11, 2023

Description of changes

This PR aims to add Node ESM support to @aws-amplify/ui-react package, following Node ESM spec.

Issue #, if available

Fixes #3155
Fixes #3206

Description of how you validated changes

Verified the above mentioned issues are fixed without applying any workaround

Changes are tested with the following frameworks/tools with tagging release node-esm

Name Tested? Notes
Next.js@11, 12, 13
Gatsby Works with ESM. Not support loading CJS build
Remix
Astro Works with ESM. Not support loading CJS build
webpack5
Vite Works with ESM. Not support loading CJS build
Rollup Works with ESM. Not support loading CJS build
esbuild
Parcel
Snowpack Need --polyfill-node to fix JS incompatibility in dev mode, but is a known issue

Checklist

  • PR description included
  • yarn test passes
  • Tests are updated
  • No side effects or sideEffects field updated

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@changeset-bot
Copy link

changeset-bot bot commented Jan 11, 2023

🦋 Changeset detected

Latest commit: 773aee6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@aws-amplify/ui-react-core Patch
@aws-amplify/ui-react Patch
@aws-amplify/ui Patch
@aws-amplify/ui-react-native Patch
@aws-amplify/ui-vue Patch
@aws-amplify/ui-angular Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -9,7 +9,7 @@
"baseUrl": ".",
"declaration": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"jsx": "react",
Copy link
Contributor Author

@zchenwei zchenwei Jan 11, 2023

Choose a reason for hiding this comment

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

Unfortunately we will have to switch back to old JSX transform because its ESM support only landed in 18 and the team does not plan to backport it to older version <- facebook/react#20304

Unless we drop the support for React 16 and 17, we cannot leverage the new JSX transform in order to support Node ESM

Copy link
Contributor Author

@zchenwei zchenwei Jan 11, 2023

Choose a reason for hiding this comment

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

Otherwise errors like following will show up and it is dumb for us to add .js file extension to every single line of them

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Volumes/workplace/amplify-ui/packages/react/node_modules/react/jsx-runtime' imported from /Volumes/workplace/amplify-ui/packages/react/dist/esm/components/Authenticator/Authenticator.mjs
Did you mean to import react/node_modules/react/jsx-runtime.js?

@@ -40,7 +40,8 @@
"dev:build": "tsup",
"clean": "rimraf dist node_modules",
"lint": "tsc --noEmit --project tsconfig.dev.json && eslint src --ext .js,.ts,.tsx",
"test": "yarn test:unit",
"test": "yarn test:unit && yarn test:esm",
"test:esm": "node --input-type=module --eval 'import \"@aws-amplify/ui-react\"'",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simple ESM test to prevent regression

Copy link
Contributor

Choose a reason for hiding this comment

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

This is nice!

@@ -1,5 +1,5 @@
import * as React from 'react';
import { Root } from '@radix-ui/react-accordion';
import * as Accordion from '@radix-ui/react-accordion';
Copy link
Contributor Author

@zchenwei zchenwei Jan 11, 2023

Choose a reason for hiding this comment

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

Have to use name space imports for radix components because it does not support Node ESM and so Node will reference its CJS build(main field) instead(Node does not know module field). Name space imports works in both format.

Copy link
Contributor

Choose a reason for hiding this comment

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

Another reason to get rid of radix when we can...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree

@@ -1,14 +1,9 @@
{
"ts-node": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do not need this awkward override any more

@@ -33,7 +33,7 @@
"scripts": {
"prebuild": "rimraf dist",
"build": "yarn build:css && yarn build:js",
"build:css": "ts-node --transpile-only ./scripts/generateCSS.ts",
"build:css": "node -r esbuild-register ./scripts/generateCSS.ts",
Copy link
Contributor Author

@zchenwei zchenwei Jan 11, 2023

Choose a reason for hiding this comment

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

Use esbuild-register to have better ESM support otherwise it will throw error in our ESM build

@zchenwei zchenwei temporarily deployed to ci January 11, 2023 02:12 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 02:12 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 02:12 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 02:56 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 02:56 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 02:56 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 02:56 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 18:28 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 18:28 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 18:56 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 11, 2023 18:56 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 26, 2023 18:24 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 26, 2023 18:24 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 26, 2023 18:24 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 26, 2023 18:24 — with GitHub Actions Inactive
wlee221
wlee221 previously approved these changes Jan 26, 2023
0618
0618 previously approved these changes Jan 27, 2023
reesscot
reesscot previously approved these changes Jan 27, 2023
calebpollman
calebpollman previously approved these changes Jan 27, 2023
@zchenwei zchenwei dismissed stale reviews from calebpollman, reesscot, 0618, and wlee221 via 773aee6 January 27, 2023 17:49
@zchenwei zchenwei temporarily deployed to ci January 27, 2023 18:10 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 27, 2023 18:10 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 27, 2023 18:10 — with GitHub Actions Inactive
@zchenwei zchenwei temporarily deployed to ci January 27, 2023 18:10 — with GitHub Actions Inactive
@zchenwei zchenwei merged commit 412538b into main Jan 27, 2023
@zchenwei zchenwei deleted the chenwz-dev-esmfix branch January 27, 2023 19:11
@github-actions github-actions bot mentioned this pull request Jan 27, 2023
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.

Can't build with Astro ESM build cannot be loaded by Node.js
6 participants