build!: Allow direct imports and component normalization
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
libSrcfolder -
srcfolder reserved for non-library code (ex: setupTests, custom rendering scenarios viavite previeworvite 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
utilsmodule - constants moved to a
constantsmodule - fixtures moved to a
fixturesmodule - base functional component and forwardRef outputs separated into their own files (X and XForwardRef)
-
_refprop added to base component that the forwardRef version forwards itsrefto (React 19 is standardizingrefbeing 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
DTSplugin is configured with an additional option to run afixCjsDeclarationsfunction for the cjs non-bundled export in order to move and rename the cjsd.tsextensions to properd.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 (https://github.com/uswds/uswds/issues/4458).
Related Issues or PRs
N/A
How To Test
- Add the jpandersen87/react-uswds#tree-shaking branch as your
@trussworks/react-uswdsdependency 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