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

yarn react build|dev #195

Merged
merged 25 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fc01355
Move react/styles.css into src/
ericclemmons Aug 18, 2021
f417619
No longer compile packages/ with Next.js
ericclemmons Aug 18, 2021
7c6752a
Use esnext import/export instead of module.exports/require
ericclemmons Aug 18, 2021
252603f
Target ES6/2015 instead of ES5
ericclemmons Aug 18, 2021
9c5f754
Mirror node's moduleResolution
ericclemmons Aug 18, 2021
8a2ee83
Inline @aws-amplify/ui-core & @aws-amplify/ui-theme-base into package
ericclemmons Aug 18, 2021
089024b
Build using tsup
ericclemmons Aug 18, 2021
3cbd9cc
Build styles.css
ericclemmons Aug 18, 2021
840b627
Inject react-shim
ericclemmons Aug 18, 2021
66811ff
Remove @fontsource/inter, since it can't be bundled into JS
ericclemmons Aug 18, 2021
24e36ee
Update yarn.lock
ericclemmons Aug 18, 2021
3e6be6c
Fix .page.tsx suffix for next-example
ericclemmons Aug 18, 2021
c280c22
Move tsup to root so it can be patched
ericclemmons Aug 20, 2021
5738715
Patch tsup at workspace root
ericclemmons Aug 20, 2021
f51ed18
Build theme-base && core && react on install
ericclemmons Aug 20, 2021
86b0a76
Remove core/index.ts
ericclemmons Aug 20, 2021
45b6751
Don't clear dist on dev
ericclemmons Aug 20, 2021
c8dda63
Only watch src
ericclemmons Aug 20, 2021
121f140
Don't inline core or theme-base because of CJS/require gets pulled in
ericclemmons Aug 20, 2021
9f6b965
Remove inspect from core
ericclemmons Aug 20, 2021
e796e25
Working core build
ericclemmons Aug 20, 2021
b09563a
Clarify how to build & watch each package
ericclemmons Aug 20, 2021
f4d7e5c
Use .cjs for ES5 files
ericclemmons Aug 20, 2021
17ad45c
Merge branch 'main' into tsup
ericclemmons Aug 20, 2021
11c53d1
Merge branch 'main' into tsup
ericclemmons Aug 20, 2021
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
8 changes: 4 additions & 4 deletions docs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const withNextPluginPreval = require('next-plugin-preval/config')();
const withCompileNodeModules = require('@moxy/next-compile-node-modules')({
include: [
// Using `path.dirname` because `package.json#main` doesn't exist in some packages yet
path.dirname(require.resolve('@aws-amplify/ui-core/package.json')),
Copy link
Contributor

Choose a reason for hiding this comment

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

ahhh so nice

path.dirname(require.resolve('@aws-amplify/ui-react/package.json')),
path.dirname(require.resolve('@aws-amplify/ui-theme-base/package.json')),
path.dirname(require.resolve('amplify-docs/package.json')),
path.dirname(
// `amplify-docs` aren't bundled, so they require post-processing
require.resolve('amplify-docs/package.json')
),
],
test: /\.(js|ts)x?/,
});
Expand Down
10 changes: 2 additions & 8 deletions examples/next/next.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
// TODO Remove this once @aws-amplify/ui-react is compiled
Copy link
Contributor

Choose a reason for hiding this comment

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

huzzah

Copy link
Contributor

Choose a reason for hiding this comment

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

Removing TODOs feels great!

const withCompileNodeModules = require('@moxy/next-compile-node-modules')({
include: /[\\/]packages[\\/]/,
test: /\.(js|ts)x?/,
});

module.exports = withCompileNodeModules({
module.exports = {
reactStrictMode: true,
pageExtensions: ['page.tsx'],
});
};
1 change: 0 additions & 1 deletion examples/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
},
"dependencies": {
"@aws-amplify/ui-react": "0.0.1",
"@moxy/next-compile-node-modules": "^2.1.1",
"@types/node": "^15.12.4",
"@types/react": "^17.0.11",
"next": "latest",
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion packages/react/index.tsx

This file was deleted.

25 changes: 19 additions & 6 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@
"private": true,
"name": "@aws-amplify/ui-react",
"version": "0.0.1",
"main": "dist/index.js",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.js",
"exports": {
".": "./dist/index.js",
"./styles.css": "./dist/index.css"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

import "@aws-amplify/ui-react/styles.css" correctly resolves to dist/index.css.

Neat, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

neat

},
"types": "dist/index.d.ts",
"files": [
"dist/*.{js,ts}",
"dist/**/*.{js,ts}"
"dist"
],
"scripts": {
"build": "tsc",
"prebuild": "rimraf dist",
"build": "tsup --minify",
"predev": "rimraf dist",
"dev": "tsup --watch",
"update:icons": "node scripts/updateIcons.js",
"build:icons": "yarn run clean:icons && node scripts/generateIcons.js",
"clean": "rimraf dist",
"clean:icons": "rimraf ./src/primitives/Icon/icons",
"postinstall": "patch-package",
"primitives:new": "hygen primitives new",
"test": "yarn test:unit",
"test:watch": "yarn test:unit:watch",
Expand All @@ -26,8 +35,9 @@
"qrcode": "^1.4.4"
},
"peerDependencies": {
"@aws-amplify/ui-core": "^0.0.1",
"@aws-amplify/ui-theme-base": "^0.0.1"
"aws-amplify": "3.x.x || 4.x.x",
"react": ">= 16.8.0",
"react-dom": ">= 16.8.0"
Comment on lines +40 to +42
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This way we don't bundle dependencies.

},
"workspaces": {
"nohoist": [
Expand All @@ -36,6 +46,8 @@
]
},
"devDependencies": {
"@aws-amplify/ui-core": "^0.0.1",
"@aws-amplify/ui-theme-base": "^0.0.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Build tools like microbundle, tsup, etc. externalize dependencies and inline devDependencies.

This is helpful for using packages that we don't want to publish.

Copy link
Contributor

Choose a reason for hiding this comment

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

Broader question: do we want to publish ui-theme-base? I noticed we publish @aws-amplify/ui which is kind of similar to what ui-theme-base is now. I don't have a strong opinion, just wanted to bring it up. Should we rename ui-theme-base to just ui?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, any cross-platform/framework dependencies (like the state machine, design tokens, or even specs (we ran into an issue with that!)) ideally lives in @aws-amplify/ui-core.

But you're right to ask about @aws-amplify/ui.

ui-core would be a new package, but I don't think it makes sense to have. ui-core is kinda our equivalent of ui, so you're right – maybe we rename & merge ui-core and theme-base into ui.

The only thing that I found weird is aws-amplify installs it by default:
https://github.com/aws-amplify/amplify-js/blob/da8ef238928fdcad7ab307350ffff8448eec5b26/packages/aws-amplify/package.json#L47

But I have no idea why, or see the need for it.

The more I type it out, the more it makes sense for @aws-amplify/ui to contain our core/shared assets and @aws-amplify/ui-* be framework-specific stuff. core just seemed to be common convention in the ecosystem (e.g. @babel/core).

Let's queue this up for another cleanup task.

"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
"@testing-library/react-hooks": "^7.0.1",
Expand All @@ -47,6 +59,7 @@
"jest": "^27.0.4",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.3",
"tsup": "^4.14.0",
"typescript": "^4.3.2"
}
}
12 changes: 12 additions & 0 deletions packages/react/patches/tsup+4.14.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/node_modules/tsup/dist/index.js b/node_modules/tsup/dist/index.js
index 1fd3ea9..8f84cf0 100644
--- a/node_modules/tsup/dist/index.js
+++ b/node_modules/tsup/dist/index.js
@@ -13633,6 +13633,7 @@ async function runEsbuild(options, { format, css }) {
jsxFactory: options.jsxFactory,
jsxFragment: options.jsxFragment,
sourcemap: options.sourcemap,
+ inject: ['./src/react-shim.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.

target: options.target === "es5" ? "es2016" : options.target,
plugins: [
externalPlugin({
2 changes: 2 additions & 0 deletions packages/react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './styles.css';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is really nice – building (via PostCSS) styles as part of a single package & process.

Copy link
Contributor

Choose a reason for hiding this comment

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

Dumb question: if we import the styles here, developers using the React components would no longer need to import the styles like import '@aws-amplify/ui-react/styles.css' anymore? And would developers be able to get un-styled React components?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Somehow the SCSS stuff just worked?? I need to check the docs to be sure...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question @dbanksdesign! No, import '@aws-amplify/ui-react/styles.css' is still required.

What import does here is allow the bundler (tsup, microbundle, whatever) automatically output both JS & CSS with a single process. (Compared to having to watch CSS and JS separately)

This is consistent with how, say, an application is bundled with webpack: even though your app has JS, CSS, SVGs, PNGs, whatever, the bundler identifies all dependencies and outputs them based on your configuration.


export * from './components';
export * from './hooks';
export * from './primitives';
Expand Down
9 changes: 9 additions & 0 deletions packages/react/src/react-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This shim exists for `esbuild` to `inject` into every .tsx file.
* Otherwise, `import * as React from 'react'` would need to be added manually
*
* See: https://esbuild.github.io/content-types/#auto-import-for-jsx
*/

import * as React from 'react';
export { React };
2 changes: 2 additions & 0 deletions packages/react/src/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import './components/Authenticator/styles.css';
@import './primitives/styles.css';
2 changes: 0 additions & 2 deletions packages/react/styles.css

This file was deleted.

6 changes: 4 additions & 2 deletions packages/react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"compilerOptions": {
"moduleResolution": "node",
"baseUrl": ".",
Comment on lines +3 to +4
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 thought this was the default, but tsup & vite weren't picking up on node_modules until I added this.

"declaration": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"rootDir": "./src",
"outDir": "./dist",
"skipLibCheck": true,
"sourceMap": true,
"module": "commonjs",
"target": "es5"
"module": "esnext",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, TIL : https://stackoverflow.com/questions/41993811/understanding-target-and-module-in-tsconfig

module basically means module.exports or import/export or import():

microsoft/TypeScript#24082

"target": "es2015"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

target means the language features:

https://stackoverflow.com/questions/41993811/understanding-target-and-module-in-tsconfig

Targeting evergreen browsers, this could even be esnext but I figure we'll standup more examples (CRA, Nuxt, etc.) to determine what that impact is.

But the less TS has to transpile, the faster our builds will be.

},
"exclude": ["node_modules"],
"include": ["src/**/*.ts", "src/**/*.tsx"]
Expand Down
10 changes: 10 additions & 0 deletions packages/react/tsup.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @type {import("tsup").Options}
*/
module.exports = {
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 did it this way for type safety vs. tsup src/index.tsx --format cjs,esm ...

dts: true,
entryPoints: ['src/index.tsx'],
format: ['cjs', 'esm'],
sourcemap: 'both',
splitting: false,
};
3 changes: 0 additions & 3 deletions packages/theme-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,5 @@
"rimraf": "^3.0.2",
"sass": "^1.35.2",
"style-dictionary": "^3.0.1"
},
"dependencies": {
"@fontsource/inter": "^4.5.0"
Comment on lines -27 to -29
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The bundler had issues with WOFF files inside of this dependency.

Admittedly, this is weird to me as part of a library. Because fonts require asset files (e.g. .woff, .ttf, etc.), these have to be bundled by the client app.

Normally, I see styles only specify font-family and leave it to the consumer to determine where the fonts are located:

Because fonts can be very expensive, I think we should revisit #165 and try to make bundle-size as much of an opt-in as possible, like https://tailwindui.com/documentation#optional-add-the-inter-font-family.

If a customer doesn't want it, font-family will naturally go to the next available font.

If a customer wants to load it in a blocking way, they can use <link>.

If a customer wants to load it in a non-blocking way, they can do what Next.js does:

https://nextjs.org/docs/basic-features/font-optimization

}
}
2 changes: 0 additions & 2 deletions packages/theme-base/src/css/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*/

@import '../../dist/variables.scss';
@import '~@fontsource/inter/index.css';
@import '~@fontsource/inter/variable.css';
Comment on lines -6 to -7
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, with this change we will need to update our theme instructions to add the Inter font (see above comment)


html {
font-family: var(--amplify-fonts-default-static);
Expand Down
Loading