Adds postcss-import plugin + fixes import for react phone styles #456
+72
−45
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary & Motivation
CSS Import Rules Error
After importing
import '@turnkey/sdk-react/styles';
in thelayout.tsx
file and runningnpm run dev
, an error occurred. When bundling CSS with Rollup usingrollup-plugin-postcss
,@import
rules are not being correctly placed at the top of the final CSS bundle. This violates CSS specifications, which require@import
statements to precede all other rules (except@charset
and@layer
).Solution
Add
postcss-import
plugin to the PostCSS configuration to the Rollup setup.The
postcss-import
plugin processes@import rules
and ensures they appear at the top of the CSS file. By including this in the PostCSS plugins array, all@import
rules are handled correctly before other CSS rules are processed, preventing build errors.Additional Build Error with postcss-import
When using
postcss-import
, I encountered a build error related to importing CSS from external packages:postcss-import: Failed to find 'react-international-phone/style.css'
This occurs because
postcss-import
cannot resolve the CSS import from thenode_modules
directory when it's imported within a CSS file.Solution
Instead of importing the styles in
PhoneInput.css
:Move the import to the React component file (
PhoneInput.tsx
) directly:This approach allows the bundler to correctly resolve the CSS import from
node_modules
, fixing the build error.How I Tested These Changes
Did you add a changeset?
If updating one of our packages, you'll likely need to add a changeset to your PR. To do so, run
pnpm changeset
.pnpm changeset
will generate a file where you should write a human friendly message about the changes. Note how this (example) includes the package name (should be auto added by the command) along with the type of semver change (major.minor.patch) (which you should set).These changes will be used at release time to determine what packages to publish and how to bump their version. For more context see this comment.