diff --git a/docs/css-modules-export-mode.md b/docs/css-modules-export-mode.md new file mode 100644 index 000000000..a39d4a280 --- /dev/null +++ b/docs/css-modules-export-mode.md @@ -0,0 +1,288 @@ +# CSS Modules Export Mode + +Most React guides and tutorials expect to import CSS Modules using a **default export object**: + +```js +import styles from './Foo.module.css'; + + Highlighted text + + +// New +
+ + Highlighted text +
+``` + +### 3. Consider Using a Codemod + +For large codebases, consider writing a codemod to automate the migration: + +```bash +# Example using jscodeshift (pseudocode) +npx jscodeshift -t css-modules-migration.js src/ +``` + +--- + +## Future Shakapacker Configuration + +In future versions of Shakapacker, this configuration may be exposed via `config/shakapacker.yml`: + +```yml +# Future configuration (not yet implemented) +css_modules: + # true -> named exports (import { bright } ...) + # false -> default export (import styles ...) + named_export: false +``` + +- **Current behavior:** Uses named exports when unset +- **Future behavior:** New app templates will default to `false` +- **Next major release:** The default will change to `false` when unset + +--- + +## Troubleshooting + +### CSS Classes Not Applying + +If your CSS classes aren't applying after the change: + +1. **Check import syntax**: Ensure you're using `import styles from ...` +2. **Verify class names**: Use `console.log(styles)` to see available classes +3. **Rebuild webpack**: Clear cache and rebuild: `rm -rf tmp/cache && bin/shakapacker` + +### TypeScript Support + +For TypeScript projects, create type definitions for your CSS modules: + +```typescript +// src/types/css-modules.d.ts +declare module '*.module.css' { + const classes: { [key: string]: string }; + export default classes; +} +``` + +### Build Performance + +The configuration changes should not impact build performance significantly. If you experience issues: + +1. Check webpack stats: `bin/shakapacker --profile` +2. Verify only necessary rules are being modified +3. Consider using webpack bundle analyzer for deeper insights + +--- + +## Summary + +- **Current default**: Named exports (`import { bright } ...`) +- **Recommended for DX**: Default export (`import styles ...`) +- **Implementation**: Override CSS loader configuration in `commonWebpackConfig.js` +- **Migration**: Update imports and class references systematically +- **Future**: Shakapacker will provide native configuration options