Integrating with Build Tools
CDS is designed to seamlessly integrate with modern build tools for both web and React Native applications. This page provides guidance on configuring your build environment to fully leverage CDS's capabilities, such as CSS imports for web and optimized theming for React Native.
Web Integration
Prerequisites
Ensure your web project meets the following requirements:
- Node.js: Version 16 or later.
- React: Version 17 or later.
- Build Tool: Webpack, Vite, or similar.
Enabling CSS Imports
CDS web components rely on static CSS files for styling, generated using Linaria. Your build tool must support importing CSS files.
Webpack Configuration
If you're using Webpack, ensure the following loader is configured for CSS:
Vite Configuration
For Vite, CSS imports work out of the box. If you have a custom setup, ensure the vite-plugin-css
plugin is included:
Using the ThemeProvider
Wrap your application with the ThemeProvider
to apply theming:
React Native Integration
Prerequisites
Ensure your React Native project meets the following requirements:
- React Native: Version 0.64 or later.
- Metro Bundler: Included with React Native.
Theming Setup
CDS uses a React Context for theming in React Native. Install the CDS mobile package:
Wrap your application with the ThemeProvider
:
Customizing Metro Bundler
If you encounter issues with module resolution, update your metro.config.js
file:
Common Issues and Solutions
CSS Files Not Found (Web)
If you encounter an error like Module not found: Can't resolve '*.css'
:
- Ensure
css-loader
andstyle-loader
are installed: - Verify your build tool configuration includes a CSS loader.
ThemeProvider Errors (React Native)
If the ThemeProvider
throws an error:
- Ensure
@coinbase/cds-mobile
is installed. - Verify that the
ThemeProvider
wraps your root component.
Incompatible Dependencies
If you encounter dependency conflicts, ensure your project dependencies match the supported versions for CDS:
- React:
>=17.0.0
- React Native:
>=0.64.0
- Node.js:
>=16.0.0
Optimizing Builds
Tree Shaking
CDS components are designed to be tree-shakable. To ensure unused components are removed from your production bundle:
-
Use the
import
syntax to import components individually: -
Enable tree shaking in your build tool configuration (e.g., Webpack’s
mode: 'production'
).
Minification
Use tools like Terser for JavaScript minification and CSSNano for CSS minification to reduce the size of your production build.
Best Practices
- Use the Latest Version: Keep CDS and its dependencies up to date to benefit from new features and bug fixes.
- Verify Compatibility: Test your build after integrating CDS to ensure all configurations are working as expected.
- Monitor Bundle Size: Use tools like Webpack Bundle Analyzer to check for any unexpected increases in bundle size.
Next Steps
- Styling: Learn how to style components using
StyleProps
. - API Overview: Explore the full range of CDS utilities and APIs.
- Theming: Understand how to customize your application’s theme.
Seamlessly integrate CDS with your build tools and unlock the power of the Coinbase Design System to create performant and visually consistent applications!