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
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e1a4e1b
chore(react): updates to support Node ESM
zchenwei Jan 10, 2023
b412357
chore(react-core): updates to support Node ESM
zchenwei Jan 10, 2023
28fe304
chore(ui): updates to support Node ESM
zchenwei Jan 10, 2023
791528e
chore: update package.json files
zchenwei Jan 10, 2023
78e68b9
Merge branch 'main' into chenwz-dev-esmfix
zchenwei Jan 10, 2023
2badeb8
chore: switch back to old JSX transform
zchenwei Jan 11, 2023
3ce47f2
fix: fix yarn.lock
zchenwei Jan 11, 2023
d68bc53
fix: fix yarn.lock
zchenwei Jan 11, 2023
101daea
Merge branch 'main' into chenwz-dev-esmfix
zchenwei Jan 11, 2023
0b7fbd1
chore: remove unused import
zchenwei Jan 11, 2023
f7c0b41
chore: clean up unused rollup plugin
zchenwei Jan 11, 2023
78f8d82
chore: fix radix imports to make it work in CJS
zchenwei Jan 12, 2023
699f8e6
chore: set up tag release workflow
zchenwei Jan 12, 2023
73abf12
chore: fix size limit error
zchenwei Jan 13, 2023
5f280cb
chore: remove unrelated changes
zchenwei Jan 13, 2023
57a51ff
chore: remove tagged release workflow
zchenwei Jan 13, 2023
7b89fb3
Merge branch 'main' into chenwz-dev-esmfix
zchenwei Jan 13, 2023
8e76502
chore: update size test import paths
zchenwei Jan 13, 2023
1bb87fb
chore: add notes to Radix imports
zchenwei Jan 13, 2023
04c8ec6
Merge branch 'main' into chenwz-dev-esmfix
zchenwei Jan 18, 2023
9eda9e5
chore: update internal package.json
zchenwei Jan 18, 2023
8a8e373
Merge branch 'main' into chenwz-dev-esmfix
zchenwei Jan 24, 2023
dd68577
Create quiet-glasses-smile.md
zchenwei Jan 26, 2023
22c0060
Update quiet-glasses-smile.md
zchenwei Jan 26, 2023
773aee6
Merge branch 'main' into chenwz-dev-esmfix
zchenwei Jan 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 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.8",
"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
1 change: 1 addition & 0 deletions packages/react-core/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const config = defineConfig([
output: {
dir: 'dist/esm',
format: 'es',
entryFileNames: '[name].mjs',
preserveModules: true,
preserveModulesRoot: 'src',
sourcemap: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
UnverifiedContactMethods,
getActorContext,
} from '@aws-amplify/ui';
import isString from 'lodash/isString';
zchenwei marked this conversation as resolved.
Show resolved Hide resolved

import { areEmptyArrays, areEmptyObjects } from '../../../utils';
import { AuthenticatorLegacyField, AuthenticatorLegacyFields } from '../types';
Expand Down Expand Up @@ -79,8 +78,7 @@ const convertContactMethodsToFields = (
return (
unverifiedContactMethods &&
Object.entries(unverifiedContactMethods).map(([name, value]) => {
const valueIsString = isString(value);
if (!valueIsString || !name) {
if (typeof value !== 'string' || !name) {
return {} as AuthenticatorLegacyField;
}
return { name, label: value, type: 'radio', value };
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConsoleLogger as Logger } from '@aws-amplify/core';
import isString from 'lodash/isString';
import isString from 'lodash/isString.js';

import { MessageAction } from '../types';

Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/src/hooks/useHasValueUpdated.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import usePreviousValue from './usePreviousValue';
import isUndefined from 'lodash/isUndefined';
import isUndefined from 'lodash/isUndefined.js';

export default function useHasValueUpdated<Value>(
value: Value,
Expand Down
4 changes: 2 additions & 2 deletions packages/react-core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isEmpty from 'lodash/isEmpty';
import isObject from 'lodash/isObject';
import isEmpty from 'lodash/isEmpty.js';
import isObject from 'lodash/isObject.js';

function isEmptyArray<T>(value: T): boolean {
return Array.isArray(value) && isEmpty(value);
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",
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?

"importHelpers": true,
"moduleResolution": "node",
"noImplicitAny": true,
Expand Down
13 changes: 7 additions & 6 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.3",
"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\"'",
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!

"test:watch": "yarn test:unit:watch",
"test:unit": "jest",
"test:unit:watch": "jest --watch",
Expand Down Expand Up @@ -123,13 +124,13 @@
"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"
}
Expand Down
7 changes: 4 additions & 3 deletions packages/react/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// rollup.config.js
import { defineConfig } from 'rollup';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
// 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 +16,7 @@ const config = defineConfig([
sourcemap: false,
},
plugins: [
commonjs(),
// commonjs(),
externals({ include: /^@aws-amplify/ }),
typescript({ declarationDir: 'dist/types', sourceMap: false }),
terser(),
Expand All @@ -28,12 +28,13 @@ const config = defineConfig([
output: {
dir: 'dist/esm',
format: 'es',
entryFileNames: '[name].mjs',
preserveModules: true,
preserveModulesRoot: 'src',
sourcemap: false,
},
plugins: [
commonjs(),
// 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
3 changes: 2 additions & 1 deletion packages/react/src/components/ThemeProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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';

const { DirectionProvider } = RadixDirection;
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
3 changes: 2 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';
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

import classNames from 'classnames';

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

const { Root } = Accordion;
const ExpanderPrimitive: Primitive<ExpanderProps, typeof Root> = (
{
children,
Expand Down
3 changes: 2 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,7 @@ import { splitPrimitiveProps } from '../shared/styleUtils';
import { useStableId } from '../utils/useStableId';
import { View } from '../View';

const { Item, Header, Trigger, Content } = Accordion;
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
7 changes: 2 additions & 5 deletions packages/react/src/primitives/Menu/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
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';

const { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent } = Dropdown;
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
3 changes: 2 additions & 1 deletion packages/react/src/primitives/Menu/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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';

const { DropdownMenuItem } = Dropdown;
export const MENU_ITEM_TEST_ID = 'amplify-menu-item-test-id';

/**
Expand Down
3 changes: 2 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,7 @@ import { splitPrimitiveProps } from '../shared/styleUtils';
import { View } from '../View';
import { useStableId } from '../utils/useStableId';

const { Range, Root, Thumb, Track } = RadixSlider;
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
9 changes: 3 additions & 6 deletions packages/react/src/primitives/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import classNames from 'classnames';
import {
Root,
List,
Trigger as RadixTab,
Content as Panel,
} from '@radix-ui/react-tabs';
import * as RadixTabs from '@radix-ui/react-tabs';
import * as React from 'react';

import { ComponentClassNames } from '../shared/constants';
import { Flex } from '../Flex';
import { TabsProps, TabItemProps, Primitive } from '../types';
import { View } from '../View';

const { Root, List, Trigger: RadixTab, Content: Panel } = RadixTabs;

const isTabsType = (child: any): child is React.ReactElement<TabItemProps> => {
return (
child !== null &&
Expand Down
2 changes: 1 addition & 1 deletion packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"baseUrl": ".",
"declaration": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"jsx": "react",
"rootDir": "./src",
"outDir": "./dist",
"importHelpers": true,
Expand Down
7 changes: 4 additions & 3 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "@aws-amplify/ui",
"version": "5.5.0",
"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"
},
"./package.json": "./package.json",
Expand All @@ -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

"build:js": "rollup --config",
"build:dev:js": "tsup",
"clean": "rimraf dist node_modules",
Expand Down Expand Up @@ -64,6 +64,7 @@
"@types/webpack-env": "^1.16.2",
"@typescript-eslint/parser": "^5.20.0",
"autoprefixer": "^10.3.1",
"esbuild-register": "^3.4.2",
"fs-extra": "^10.0.0",
"glob": "^7.1.7",
"globcat": "^1.3.4",
Expand Down
1 change: 1 addition & 0 deletions packages/ui/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const config = defineConfig([
output: {
dir: 'dist/esm',
format: 'es',
entryFileNames: '[name].mjs',
preserveModules: true,
preserveModulesRoot: 'src',
sourcemap: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/helpers/authenticator/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This file contains helpers that process authenticator state machine context
*/
import includes from 'lodash/includes';
import includes from 'lodash/includes.js';

import {
LoginMechanismArray,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/helpers/authenticator/formFields/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cloneDeep from 'lodash/cloneDeep';
import cloneDeep from 'lodash/cloneDeep.js';
import { translate } from '../../../i18n/translations';
import { FormFields, FormFieldsArray } from '../../../types';

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/helpers/authenticator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Hub } from 'aws-amplify';
import { appendToCognitoUserAgent } from '@aws-amplify/auth';
import { waitFor } from 'xstate/lib/waitFor';
import { waitFor } from 'xstate/lib/waitFor.js';

import { AuthInterpreter, AuthMachineHubHandler } from '../../types';
import { ALLOWED_SPECIAL_CHARACTERS } from './constants';
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/src/machines/authenticator/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Auth } from 'aws-amplify';
import { assign, stop } from 'xstate/lib/actions';
import { actions } from 'xstate';
Copy link
Contributor

Choose a reason for hiding this comment

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


import {
ActorContextWithForms,
Expand All @@ -8,6 +8,8 @@ import {
SignUpContext,
} from '../../types';

const { assign, stop } = actions;

export const stopActor = (machineId: string) => {
return stop(machineId);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/machines/authenticator/actors/signIn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Auth } from 'aws-amplify';
import get from 'lodash/get';
import isEmpty from 'lodash/isEmpty';
import get from 'lodash/get.js';
import isEmpty from 'lodash/isEmpty.js';
import { createMachine, sendUpdate } from 'xstate';
import {
AuthChallengeName,
Expand Down
Loading