Skip to content

Commit

Permalink
build: updates to support Node ESM (#3287)
Browse files Browse the repository at this point in the history
* chore(react): updates to support Node ESM

* chore(react-core): updates to support Node ESM

* chore(ui): updates to support Node ESM

* chore: update package.json files

* chore: switch back to old JSX transform

* fix: fix yarn.lock

* fix: fix yarn.lock

* chore: remove unused import

* chore: clean up unused rollup plugin

* chore: fix radix imports to make it work in CJS

* chore: set up tag release workflow

* chore: fix size limit error

* chore: remove unrelated changes

* chore: remove tagged release workflow

* chore: update size test import paths

* chore: add notes to Radix imports

* chore: update internal package.json

* Create quiet-glasses-smile.md

* Update quiet-glasses-smile.md
  • Loading branch information
zchenwei authored Jan 27, 2023
1 parent 9ce2d01 commit 412538b
Show file tree
Hide file tree
Showing 35 changed files with 143 additions and 89 deletions.
24 changes: 24 additions & 0 deletions .changeset/quiet-glasses-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
"@aws-amplify/ui-react-core": patch
"@aws-amplify/ui-react": patch
"@aws-amplify/ui": patch
---

build: updates to support Node ESM

Confirmed that both #3155 and #3206 are fixed without having to apply any workaround

Also, test out the changes with the following frameworks/tools:

| 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 |
| webpack || |
| 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](https://github.com/FredKSchott/snowpack/discussions/718) |
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ Amplify UI is an open-source UI library with cloud-connected components that are

## Component Matrix

| **Connected Components** | **React** | **React Native** | **Angular** | **Vue** |
| :----------------------- | :-------: | :--------------: | :---------: | :---------:|
| Authenticator | | 🚧 | | |
| In-App Messaging | | | | |
| Geo (MapView) | | | | |
| Account Settings | 🚧 | | | |
| Storage (FileUploader) | 🚧 | | | |
| **Connected Components** | **React** | **React Native** | **Angular** | **Vue** |
| :----------------------- | :-------: | :--------------: | :---------: | :-----: |
| Authenticator | | 🚧 | ||
| In-App Messaging | | | | |
| Geo (MapView) | | | | |
| Account Settings | 🚧 | | | |
| Storage (FileUploader) | 🚧 | | | |

| **Primitives** | **React** | **Angular** | **Vue** |
| :--------------- | :-------: | :---------: | :-----: |
Expand Down
5 changes: 2 additions & 3 deletions packages/react-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "@aws-amplify/ui-react-core",
"version": "2.1.10",
"main": "dist/index.js",
"module": "dist/esm/index.js",
"module": "dist/esm/index.mjs",
"react-native": "dist/index.js",
"exports": {
".": {
"import": "./dist/esm/index.js",
"import": "./dist/esm/index.mjs",
"require": "./dist/index.js"
},
"./package.json": "./package.json"
Expand Down Expand Up @@ -38,7 +38,6 @@
"react": ">= 16.14.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.1",
"@rollup/plugin-typescript": "^8.3.1",
"@testing-library/react": "^12.0.0",
"@testing-library/react-hooks": "^7.0.2",
Expand Down
4 changes: 1 addition & 3 deletions packages/react-core/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// rollup.config.js
import { defineConfig } from 'rollup';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import externals from 'rollup-plugin-node-externals';

const config = defineConfig([
Expand All @@ -14,7 +13,6 @@ const config = defineConfig([
sourcemap: false,
},
plugins: [
commonjs(),
externals({ include: /^@aws-amplify/ }),
typescript({ declarationDir: 'dist/types', sourceMap: false }),
],
Expand All @@ -25,12 +23,12 @@ const config = defineConfig([
output: {
dir: 'dist/esm',
format: 'es',
entryFileNames: '[name].mjs',
preserveModules: true,
preserveModulesRoot: 'src',
sourcemap: false,
},
plugins: [
commonjs(),
externals({ include: /^@aws-amplify/ }),
typescript({ outDir: 'dist/esm', declaration: false, sourceMap: false }),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
InAppMessageInteractionEvent,
Notifications,
} from '@aws-amplify/notifications';
import isNil from 'lodash/isNil';
import isNil from 'lodash/isNil.js';

import { RenderNothing } from '../../../components';
import { useInAppMessaging } from '../useInAppMessaging';
Expand Down
3 changes: 1 addition & 2 deletions packages/react-core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import isEmpty from 'lodash/isEmpty';

import isEmpty from 'lodash/isEmpty.js';
import { isObject, isString } from '@aws-amplify/ui';

function isEmptyArray<T>(value: T): boolean {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"baseUrl": ".",
"declaration": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"jsx": "react",
"importHelpers": true,
"moduleResolution": "node",
"noImplicitAny": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"private": true,
"name": "@aws-amplify/ui-react-internal",
"main": "../dist/internal.js",
"module": "../dist/esm/internal.js",
"module": "../dist/esm/internal.mjs",
"types": "../dist/types/internal.d.ts"
}
20 changes: 10 additions & 10 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "@aws-amplify/ui-react",
"version": "4.3.5",
"main": "dist/index.js",
"module": "dist/esm/index.js",
"module": "dist/esm/index.mjs",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/esm/index.js",
"import": "./dist/esm/index.mjs",
"require": "./dist/index.js"
},
"./internal": {
"import": "./dist/esm/internal.js",
"import": "./dist/esm/internal.mjs",
"require": "./dist/internal.js"
},
"./styles.css": "./dist/styles.css",
Expand Down Expand Up @@ -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\"'",
"test:watch": "yarn test:unit:watch",
"test:unit": "jest",
"test:unit:watch": "jest --watch",
Expand Down Expand Up @@ -79,7 +80,6 @@
}
},
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.1",
"@rollup/plugin-typescript": "^8.3.1",
"@size-limit/preset-big-lib": "^7.0.8",
"@svgr/core": "^5.5.0",
Expand Down Expand Up @@ -123,31 +123,31 @@
"size-limit": [
{
"name": "Authenticator",
"path": "dist/esm/index.js",
"path": "dist/esm/index.mjs",
"import": "{ Authenticator }",
"limit": "120 kB"
},
{
"name": "Geo",
"path": "dist/esm/index.js",
"path": "dist/esm/index.mjs",
"import": "{ MapView, LocationSearch }",
"limit": "330 kB"
},
{
"name": "Storage - FileUploader",
"path": "dist/esm/index.js",
"path": "dist/esm/index.mjs",
"import": "{ FileUploader }",
"limit": "140 kB"
},
{
"name": "AccountSettings",
"path": "dist/esm/index.js",
"path": "dist/esm/index.mjs",
"import": "{ AccountSettings }",
"limit": "60 kB"
},
{
"name": "InAppMessaging",
"path": "dist/esm/index.js",
"path": "dist/esm/index.mjs",
"import": "{ InAppMessagingProvider, InAppMessageDisplay }",
"limit": "110 kB"
}
Expand Down
4 changes: 1 addition & 3 deletions packages/react/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// rollup.config.js
import { defineConfig } from 'rollup';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
import styles from 'rollup-plugin-styles';
import externals from 'rollup-plugin-node-externals';
Expand All @@ -16,7 +15,6 @@ const config = defineConfig([
sourcemap: false,
},
plugins: [
commonjs(),
externals({ include: /^@aws-amplify/ }),
typescript({ declarationDir: 'dist/types', sourceMap: false }),
terser(),
Expand All @@ -28,12 +26,12 @@ const config = defineConfig([
output: {
dir: 'dist/esm',
format: 'es',
entryFileNames: '[name].mjs',
preserveModules: true,
preserveModulesRoot: 'src',
sourcemap: false,
},
plugins: [
commonjs(),
externals({ include: /^@aws-amplify/ }),
typescript({ outDir: 'dist/esm', declaration: false, sourceMap: false }),
terser(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import isEqual from 'lodash/isEqual';
import isEqual from 'lodash/isEqual.js';

import { Logger } from 'aws-amplify';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useMemo, useRef } from 'react';
import isEmpty from 'lodash/isEmpty';
import isEmpty from 'lodash/isEmpty.js';
import { MessageComponentBaseProps } from '@aws-amplify/ui-react-core';

import { useMessageImage } from '../useMessageImage';
Expand Down
10 changes: 9 additions & 1 deletion packages/react/src/components/ThemeProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import * as React from 'react';
import { DirectionProvider } from '@radix-ui/react-direction';
import * as RadixDirection from '@radix-ui/react-direction';

import { createTheme, Theme, WebTheme } from '@aws-amplify/ui';

import { AmplifyContext } from './AmplifyContext';

// Radix packages don't support ESM in Node, in some scenarios(e.g. SSR),
// the module will be imported as CommonJS module, in which we have to reference the `default`
let sanitizedRadixDirection = { default: undefined, ...RadixDirection };
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sanitizedRadixDirection =
sanitizedRadixDirection.default ?? sanitizedRadixDirection;
const { DirectionProvider } = sanitizedRadixDirection;

export type ColorMode = 'system' | 'light' | 'dark';
export type Direction = 'ltr' | 'rtl';

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/primitives/Collection/Collection.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import debounce from 'lodash/debounce';
import debounce from 'lodash/debounce.js';
import * as React from 'react';

import { Flex } from '../Flex';
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/primitives/Expander/Expander.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Root } from '@radix-ui/react-accordion';
import * as Accordion from '@radix-ui/react-accordion';
import classNames from 'classnames';

import { useDeprecationWarning } from '../../hooks/useDeprecationWarning';
Expand All @@ -9,6 +9,13 @@ import { ExpanderProps } from '../types/expander';
import { Primitive } from '../types/view';
import { splitPrimitiveProps } from '../utils/splitPrimitiveProps';

// Radix packages don't support ESM in Node, in some scenarios(e.g. SSR),
// the module will be imported as CommonJS module, in which we have to reference the `default`
let sanitizedAccordion = { default: undefined, ...Accordion };
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sanitizedAccordion = sanitizedAccordion.default ?? sanitizedAccordion;
const { Root } = sanitizedAccordion;

const ExpanderPrimitive: Primitive<ExpanderProps, 'div'> = (
{
children,
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/primitives/Expander/ExpanderItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Item, Header, Trigger, Content } from '@radix-ui/react-accordion';
import * as Accordion from '@radix-ui/react-accordion';
import classNames from 'classnames';

import { ComponentClassNames } from '../shared/constants';
Expand All @@ -10,6 +10,13 @@ import { splitPrimitiveProps } from '../utils/splitPrimitiveProps';
import { useStableId } from '../utils/useStableId';
import { View } from '../View';

// Radix packages don't support ESM in Node, in some scenarios(e.g. SSR),
// the module will be imported as CommonJS module, in which we have to reference the `default`
let sanitizedAccordion = { default: undefined, ...Accordion };
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sanitizedAccordion = sanitizedAccordion.default ?? sanitizedAccordion;
const { Item, Header, Trigger, Content } = sanitizedAccordion;

export const EXPANDER_ITEM_TEST_ID = 'expander-item';
export const EXPANDER_HEADER_TEST_ID = 'expander-header';
export const EXPANDER_ICON_TEST_ID = 'expander-icon';
Expand Down
14 changes: 9 additions & 5 deletions packages/react/src/primitives/Menu/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import * as React from 'react';
import classNames from 'classnames';
import {
DropdownMenu,
DropdownMenuTrigger,
DropdownMenuContent,
} from '@radix-ui/react-dropdown-menu';
import * as Dropdown from '@radix-ui/react-dropdown-menu';

import { ButtonGroup } from '../ButtonGroup';
import { ComponentClassNames } from '../shared/constants';
import { IconMenu } from '../Icon/internal';
import { MenuButton } from './MenuButton';
import { MenuProps, Primitive } from '../types';

// Radix packages don't support ESM in Node, in some scenarios(e.g. SSR),
// the module will be imported as CommonJS module, in which we have to reference the `default`
let sanitizedDropdown = { default: undefined, ...Dropdown };
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sanitizedDropdown = sanitizedDropdown.default ?? sanitizedDropdown;
const { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent } =
sanitizedDropdown;

export const MENU_TRIGGER_TEST_ID = 'amplify-menu-trigger-test-id';
export const MENU_ITEMS_GROUP_TEST_ID = 'amplify-menu-items-group-test-id';

Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/primitives/Menu/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import * as React from 'react';
import classNames from 'classnames';
import { DropdownMenuItem } from '@radix-ui/react-dropdown-menu';
import * as Dropdown from '@radix-ui/react-dropdown-menu';

import { ComponentClassNames } from '../shared/constants';
import { MenuButton } from './MenuButton';
import { MenuItemProps } from '../types';

// Radix packages don't support ESM in Node, in some scenarios(e.g. SSR),
// the module will be imported as CommonJS module, in which we have to reference the `default`
let sanitizedDropdown = { default: undefined, ...Dropdown };
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sanitizedDropdown = sanitizedDropdown.default ?? sanitizedDropdown;
const { DropdownMenuItem } = sanitizedDropdown;

export const MENU_ITEM_TEST_ID = 'amplify-menu-item-test-id';

/**
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/primitives/SliderField/SliderField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import { Range, Root, Thumb, Track } from '@radix-ui/react-slider';
import * as RadixSlider from '@radix-ui/react-slider';
import * as React from 'react';

import { classNameModifier } from '../shared/utils';
Expand All @@ -15,6 +15,13 @@ import { splitPrimitiveProps } from '../utils/splitPrimitiveProps';
import { View } from '../View';
import { useStableId } from '../utils/useStableId';

// Radix packages don't support ESM in Node, in some scenarios(e.g. SSR),
// the module will be imported as CommonJS module, in which we have to reference the `default`
let sanitizedRadixSlider = { default: undefined, ...RadixSlider };
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sanitizedRadixSlider = sanitizedRadixSlider.default ?? sanitizedRadixSlider;
const { Range, Root, Thumb, Track } = sanitizedRadixSlider;

export const SLIDER_LABEL_TEST_ID = 'slider-label';
export const SLIDER_ROOT_TEST_ID = 'slider-root';
export const SLIDER_TRACK_TEST_ID = 'slider-track';
Expand Down
Loading

0 comments on commit 412538b

Please sign in to comment.