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!: Allow direct imports and component normalization #2899

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from

Conversation

jpandersen87
Copy link
Contributor

@jpandersen87 jpandersen87 commented Apr 26, 2024

Summary

This PR makes project-wide changes to allow importing from files directly in addition to the index. Build and project configuration changes were made to ensure the package passes tests from https://arethetypeswrong.github.io/. All components were modified for one of the following reasons:

  • "use client" directive added for special components
  • library-specific source code moved to libSrc folder
  • src folder reserved for non-library code (ex: setupTests, custom rendering scenarios via vite preview or vite dev)
  • folder normalization (camelcase folder names for folders directly containing a component file only, otherwise lowercase)
  • missing exports added
  • component props types normalized (BaseXProps and XProps)
  • sibling components defined in the same file were moved to dedicated folder/file
  • utility functions moved to a utils module
  • constants moved to a constants module
  • fixtures moved to a fixtures module
  • base functional component and forwardRef outputs separated into their own files (X and XForwardRef)
    • _ref prop added to base component that the forwardRef version forwards its ref to (React 19 is standardizing ref being a prop on functional components and removing the need of forwardRef)
  • component modules only have default export
  • components with a clear parent element to spread props on that were not already spreading additional props now do
  • prop spreading on components happens after our predefined props instead of before, to ensure overridability

Some areas where we could use one of the react-uswds components instead of a jsx element (ex: Button, Grid) were updated to use those components instead.

A dedicated build script for handling the logic of building the library in various formats and related dependencies were added. Other build notes:

  • The DTS plugin is configured with an additional option to run a fixCjsDeclarations function for the cjs non-bundled export in order to move and rename the cjs d.ts extensions to proper d.cts

The project also now uses the USWDS' compile package via gulpfile to create the default settings css file (with all assets generated exposed).

In order to create compatible typescript declarations for both cjs and es modes, the module setting for typescript was changed to node16. This makes it required to include the extension on all relative imports (reflective of the fact this package is now esm by default and es modules have that import extension requirement. The isolatedModules option was also enabled, requiring type import/exports to be explicitly marked as type.

The .usa-tooltip__body override was removed as its issue appears to have been resolved (uswds/uswds#4458).

Related Issues or PRs

N/A

How To Test

  • Add the jpandersen87/react-uswds#tree-shaking branch as your @trussworks/react-uswds dependency instead of from npm for your project
  • Verify your project continues to work (except for cases where you directly imported types from files that have had their paths changed from normalization)
  • Verify you can directly import from files instead of from index

- do not directly import uswds image files (so as not to create library copies, defeating the purpose of peer dependency)
- use custom user-configurable uswds image object to use for references to uswds images (icons, govbanner)
- icons reworked based on above
- remove uswds css logic from vite config
- remove vite settings that manipulated output filenames (let vite organize output as close to default as possible)
- limit library formats to cjs/es for now
- use @uswds/compile for creating uswds assets (provide uswds assets from a fresh init, preventing those assets from being processed via vite and being mutated in unexpected ways)
- package.json exports (we can be backward-compatible with new output structure)
- package.json scripts (use glob pattern matching for eslint)
- export everything
- helper fns moved to local utils
- comp props type added / updated
- use env var to determine lib format for build
- output formats in dedicated subfolders
- only one type set exported
- fix govbanner
- extensions added to all relative imports
- type-fest added
- prop type adjustements
- use component instead of raw elements when relevant
- remove use of file/svg mocks
- establish lib for lib export code, and src for other things (ex: setupTests)
- commanderjs, tsx added for scripting
- build cjs and es formats unbundled and bundled
- separate inner component functions from forwardref version
- ensure index exports point to forwardref versions
- add base props to all components
@jpandersen87 jpandersen87 changed the title !build: Allow direct imports and component normalization build!: Allow direct imports and component normalization Apr 26, 2024
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

1 participant