1.0.0 • Published 11 months ago
@eliyya/typed-css-modules v1.0.0
withTypeCSSModule
A Next.js plugin that wraps the typed-css-modules library, enabling automatic generation of .d.ts files for CSS Modules in TypeScript projects. This plugin integrates seamlessly with your Next.js project and simplifies the usage of typed-css-modules by:
- Running it automatically for all CSS Module files in your project.
- Watching for changes in your CSS Module files and regenerating the
.d.tsfiles when necessary. - Exporting all the functionalities of
typed-css-modulesso you can use it directly if the plugin is not required.
Features
- Automatic Type Generation: No need to run
typed-css-modulesmanually. - File Watching: Watches your CSS Module files and regenerates
.d.tsfiles on change. - Customizable Extensions: Supports
.module.css,.module.scss, and other extensions. - Debounce Support: Prevents excessive executions during rapid file changes.
- Direct Access to
typed-css-modules: Exposes the original API for advanced use cases.
Installation
Install the package using npm or yarn:
npm install typed-css-modulesInclude the plugin in your next.config.mjs file:
import { withTypeCSSModule } from '@eliya/typed-css-modules'
const nextjsConfig = {
// your nextjs config
}
export default withTypeCSSModule(nextjsConfig)Usage
Plugin Mode
By default, the plugin automatically generates .d.ts files for all CSS Module files in your Next.js project.
Example next.config.mjs:
import { withTypeCSSModule } from '@eliya/typed-css-modules'
const nextjsConfig = {
// your nextjs config
}
export default withTypeCSSModule(nextjsConfig, {
extensions: ['.module.css', '.module.scss'], // optona;
debounceWait: 300, // optona;
})extensions: Specifies the file extensions to watch and process. Default:['.module.css'].debounceWait: Time in milliseconds to wait before regenerating types after a file change. Default:200.
Direct API Usage
If you prefer to use typed-css-modules directly, the plugin exports all its functionalities. For example:
import { run } from '@eliya/typed-css-modules'
run('./src', {
silent: true,
pattern: '**/*.module.css',
})This works the same way as calling run from the typed-css-modules package itself.
Example Project Structure
project-root/
├── src/
│ ├── components/
│ │ ├── Button.module.css
│ │ └── Button.module.css.d.ts <-- generated
│ └── pages/
├── next.config.mjs
└── package.jsonHow It Works
- Initial Run: When you include the plugin, it automatically generates
.d.tsfiles for all CSS Modules matching the specified extensions. - File Watching: Uses
fs.watchto monitor changes in thesrcdirectory and regenerates.d.tsfiles for modified CSS Module files. - Debounced Updates: Uses a debounce mechanism to reduce redundant type generation during rapid file modifications.
Notes
- Ensure your CSS Modules follow the naming convention (e.g.,
*.module.css). - If using custom extensions like
.module.scss, you need to configure theextensionsoption. - The plugin does not interfere with other Next.js configurations.
License
This plugin is licensed under the MIT License.
1.0.0
11 months ago