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!: generate cjs and type declarations #1572

Merged
merged 7 commits into from Apr 3, 2023
Merged

Conversation

jinlee93
Copy link
Contributor

@jinlee93 jinlee93 commented Mar 31, 2023

EDS-886

Summary:

  • builds both cjs and esm with rollup
    • cjs built as *.cjs files to /lib
    • esm built as *.js files to lib
    • sourcemaps also turned on
  • mitigated by not building declarations with rollup, and using tsc to build declarations separately into lib/types
  • now looks like:
    image

Test Plan:

  • Wrote automated tests
  • CI tests / new tests are not applicable
  • Manually tested my changes, but I want to keep the details secret
  • Manually tested my changes, and here are the details:
    • built and copied folder into edu_stack node_modules/@chanzuckerberg/eds
    • added a debugger line to one of the used components in the es folder to make sure the esm build was being used:
      Screenshot 2023-03-31 at 12 55 10 AM
    • also saw that sourcemaps being built correctly
    • adding debugger into a component not being used did not trigger the browser debugger, suggesting tree shaking is working
    • running npm test in edu-stack passes even withtransformIgnorePatterns: ['/node_modules/(?!(@chanzuckerberg/eds))'] removed
      • passes now without tslib since that was a peerdep of @rollup/plugin-typescript

@jinlee93 jinlee93 requested a review from a team March 31, 2023 08:18
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react",
"lib": ["es5", "es6", "dom"],
"moduleResolution": "node",
"noUnusedLocals": true,
"outDir": "lib",
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 longer using tsc to build

tsconfig.json Outdated
@@ -4,13 +4,13 @@
"allowJs": false,
"allowSyntheticDefaultImports": true,
"declaration": true,
"declarationDir": "lib/types",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

builds the declaration files to this folder

*/
declaration: false,
declarationDir: undefined,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's already an issue raised in the plugin repo rollup/plugins#1230 (comment)

format: 'cjs',
preserveModules: true,
preserveModulesRoot: 'src',
sourcemap: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can turn sourcemaps off, not sure if anyone will take a look but also wouldn't hurt to have

@@ -90,8 +93,6 @@
"react-popper": "^2.3.0",
"react-portal": "^4.2.2"
},
"entry": "lib/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.

Not sure what this line did?

Copy link
Contributor

Choose a reason for hiding this comment

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

No idea. Probs something in the BM template that doesn't do anything.

@jinlee93 jinlee93 added the do not merge PRs that should not be merged (even if the build is green or there are approvals) label Mar 31, 2023
@codecov
Copy link

codecov bot commented Mar 31, 2023

Codecov Report

Merging #1572 (2d05ebd) into ah-ssr-build (a917da5) will not change coverage.
The diff coverage is n/a.

❗ Current head 2d05ebd differs from pull request most recent head 90b865b. Consider uploading reports for the commit 90b865b to get more accurate results

@@              Coverage Diff              @@
##           ah-ssr-build    #1572   +/-   ##
=============================================
  Coverage         91.63%   91.63%           
=============================================
  Files               279      279           
  Lines              4269     4269           
  Branches            775      775           
=============================================
  Hits               3912     3912           
  Misses              331      331           
  Partials             26       26           
Impacted Files Coverage Δ
src/index.ts 100.00% <ø> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

package.json Outdated
Comment on lines 8 to 9
"main": "lib/cjs/index.js",
"module": "lib/es/index.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

These will certainly work 👍

Although in the future we may want to switch over to conditional exports.

Per https://nodejs.org/api/packages.html#dual-commonjses-module-packages (and other places in the docs), I think exports is preferred now (although main/module is still common).

Copy link
Contributor

@ahuth ahuth left a comment

Choose a reason for hiding this comment

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

Nice 🤘

@jinlee93 jinlee93 changed the title build: generate cjs and type declarations build!: generate cjs and type declarations Mar 31, 2023
"./tailwind.config.mjs": {
"import": "./tailwind.config.mjs"
}
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ahuth Not sure if I'm using conditional exports right but seems that we have to explicitly state which files are allowed to be exported

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that's true. I kind of forgot that there was more than just the index.js files (such as fonts, css, tailwind, etc).

Does this seem like it's too cumbersome, or is it okay?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's fine unless consumers want to use some of the internal components or the utils, which we haven't come across yet

preserveModules: true,
preserveModulesRoot: 'src',
sourcemap: true,
entryFileNames: '[name].cjs',
Copy link
Contributor Author

@jinlee93 jinlee93 Mar 31, 2023

Choose a reason for hiding this comment

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

This builds the cjs files using .cjs extension into the same directories, no longer requiring separate es and cjs directories... I prefer it this way but we can always go back to separate directories
image

Copy link
Contributor

Choose a reason for hiding this comment

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

Either way seems reasonable, so let's go with your preference.

@@ -64,4 +66,4 @@ export default {
xxl: '1400px',
},
},
} satisfies Config;
};
Copy link
Contributor Author

@jinlee93 jinlee93 Mar 31, 2023

Choose a reason for hiding this comment

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

@ahuth I converted this into an mjs file so that it can be used without processing in pilots, still getting typing with the jsdoc @type tag

Copy link
Contributor

@ahuth ahuth Mar 31, 2023

Choose a reason for hiding this comment

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

I think it's better to keep this TS, so that it's easier for the pilots to write their tailwind files in TS.

Meaning, the pilot swill have to process their tailwind.config.ts anyway, so it's fine for that TS file to import another TS file (the EDS tailwind config).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me, reverted to TS

Comment on lines +13 to +20
"./index.css": {
"import": "./lib/index.css",
"require": "./lib/index.css"
},
"./fonts.css": {
"import": "./lib/tokens/fonts.css",
"require": "./lib/tokens/fonts.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.

I short handed some of these styles exports so it's less unwieldy (i.e. import '@chanzuckerberg/eds/fonts.css and not import @chanzuckerberg/eds/lib/tokens/fonts.css) ... it IS breaking but the whole build feature will be breaking so might as well include it... lmk if we shouldn't break this since cost is pretty low

@jinlee93
Copy link
Contributor Author

Still getting the test errors in edu-stack
image
with conditional exports

@jinlee93 jinlee93 requested a review from ahuth March 31, 2023 21:59
@jinlee93 jinlee93 removed the do not merge PRs that should not be merged (even if the build is green or there are approvals) label Apr 3, 2023
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"suppressImplicitAnyIndexErrors": true,
"target": "es6"
"target": "es2018"
Copy link
Contributor Author

@jinlee93 jinlee93 Apr 3, 2023

Choose a reason for hiding this comment

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

Target is being bumped to remove tslib usage in running rollup build with @rollup/plugin-typescript. tslib was used for __rest utility which is the spread object functionality that comes with es2018. tslib is being grepped for as a build test

then
echo "tslib found in lib"
exit 1
fi
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jinlee93 jinlee93 merged commit 5efbba3 into ah-ssr-build Apr 3, 2023
4 checks passed
@jinlee93 jinlee93 deleted the jlee/alsobuildcjs branch April 3, 2023 23:58
@jinlee93 jinlee93 mentioned this pull request Apr 3, 2023
10 tasks
jinlee93 added a commit that referenced this pull request Apr 17, 2023
* build: use rollup to build JS and styles

- To better support server-side rendering
- By producing a single .css file
- Instead of delegating css module processing to consuming apps

* refactor(rollup): remove spritemap and commonjs from config

* build: remove styles build from main build command

* refactor: import css variables into index

* build(rollup): mark dependencies as external (#1558)

* build(rollup): mark dependencies as external

* build(rollup): use node resolve plugin

* docs(build): explain rollup external option

* build!: generate cjs and type declarations (#1572)

* build: generate cjs and esm, separate types build

* build: add sourcemaps and add declaration back to tsconfig

* build: cjs and js in the same output

* build: add conditional exports

* build: remove build types script

* refactor: revert tw config to ts

* build: bump target to es2018 and grep for tslib

* chore(release): 12.0.0-alpha.0

* refactor: dry css exports

* build!: remove some tokens from build (#1581)

* refactor: remove references to unexported files

* build!: no longer include some tokens in build

* refactor: remove unused commonjs plugin (#1582)

* chore(release): 12.0.0-alpha.1

* refactor: use named import for clsx

* chore: remove rollup plugin commonjs

* Revert "refactor: use named import for clsx"

This reverts commit d58182b.

* docs: explicate tslib grep

* fix: no need for ts expect error

---------

Co-authored-by: Andrew Huth <ahuth@chanzuckerberg.com>
@booc0mtaco booc0mtaco mentioned this pull request Apr 17, 2023
booc0mtaco added a commit that referenced this pull request Apr 17, 2023
## [12.0.0](v11.1.1...v12.0.0) (2023-04-17)


### ⚠ BREAKING CHANGES

* use rollup (#1555)

### Features

* export some subcomponents ([#1579](#1579)) ([2857ae4](2857ae4))
* **TextareaField:** add character length counter ([#1580](#1580)) ([ff6226f](ff6226f))


### Bug Fixes

* restore check for undefined any types ([#1585](#1585)) ([c7fae07](c7fae07))
* **Skeleton:** mark .Rect as deprecated ([#1586](#1586)) ([405f81b](405f81b))
* sync typography presets to documentation ([#1592](#1592)) ([b56eadb](b56eadb))
* **typography:** add missing eds-font-size-20 ([#1591](#1591)) ([de5dd03](de5dd03))


### build

* use rollup ([#1555](#1555)) ([d794696](d794696)), closes [#1558](#1558) [#1572](#1572) [#1581](#1581) [#1582](#1582)
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.

None yet

2 participants