Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/great-weeks-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@css-modules-kit/codegen': minor
---

feat: exit with error when `cmkOptions.enabled` is false
5 changes: 5 additions & 0 deletions .changeset/silly-results-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@css-modules-kit/codegen': minor
---

feat: warn when `cmkOptions.enabled` is not set
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ In TypeScript, the `include`/`exclude` properties specify which `*.ts` files to
}
```

### `cmkOptions.enabled`

Type: `boolean`, Default: `true`

Enables or disables css-modules-kit. When set to `false`, codegen will exit with an error. Currently, both codegen and the ts-plugin will work even if this option is omitted, but in the future, they will not work unless this option is set to `true`. For more details, see [#289](https://github.com/mizdra/css-modules-kit/issues/289).

```jsonc
{
"compilerOptions": {
// ...
},
"cmkOptions": {
"enabled": true,
},
}
```

### `cmkOptions.dtsOutDir`

Type: `string`, Default: `"generated"`
Expand Down
45 changes: 14 additions & 31 deletions docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,50 +47,33 @@ Configure npm-script to run `cmk` command before building and type checking. Thi

## Configure `tsconfig.json`

Finally, you need to configure your tsconfig.json so that the ts-plugin and codegen work correctly.

- Set the `include` option so that files like `*.module.css` are considered for type-checking:
- For example: `["src"]`, `["src/**/*"]`, or omitting the `include` option (which is equivalent to `["**/*"]`)
- Not recommended: `["src/**/*.ts"]`, `["src/index.ts"]`
Finally, you need to configure your tsconfig.json so that css-modules-kit works correctly.

- Set `cmkOptions.enabled` to `true` to enable css-modules-kit.
- Omit the `include` options or ensure that `*.module.css` files are included when specifying them explicitly.
- ✅ Good cases:
- Omit `include` (equivalent to `["**/*"]`)
- Use patterns like `["src"]`, `["src/**/*"]`
- ❌ Bad cases:
- `["src/**/*.ts"]`
- `["src/index.ts"]` (excludes `*.module.css` files)
- Set the `rootDirs` option to include both the directory containing `tsconfig.json` and the `generated` directory.
- Example: `[".", "generated"]`

Below is an example configuration:

```jsonc
{
// Omitting the `include` option is equivalent to using `["**/*"]`
"compilerOptions": {
/* Projects */
"rootDirs": [".", "generated"],

/* Language and Environment */
"target": "ESNext",
"lib": ["ESNext"],

/* Modules */
"module": "NodeNext",
"moduleResolution": "NodeNext",

/* Emit */
"noEmit": true,

/* Interop Constraints */
"verbatimModuleSyntax": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,

/* Type Checking */
"strict": true,

/* Completeness */
"skipLibCheck": true,
// ...
},
"cmkOptions": {
"enabled": true,
},
}
```

This completes the minimal setup.

## Install linter plugin (Optional)

We provide linter plugin for CSS Modules. Currently, we support the following linters:
Expand Down
3 changes: 3 additions & 0 deletions examples/1-basic/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"incremental": false,
"rootDirs": [".", "generated"],
"types": [] // Simplify tsserver.log
},
"cmkOptions": {
"enabled": true
}
}
1 change: 1 addition & 0 deletions examples/2-named-exports/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"types": [] // Simplify tsserver.log
},
"cmkOptions": {
"enabled": true,
"namedExports": true
}
}
3 changes: 3 additions & 0 deletions examples/3-import-alias/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"types": [], // Simplify tsserver.log

"paths": { "@/*": ["./*"] }
},
"cmkOptions": {
"enabled": true
}
}
3 changes: 3 additions & 0 deletions examples/4-multiple-tsconfig/tsconfig.dir-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"types": [], // Simplify tsserver.log

"paths": { "@/*": ["./*"] }
},
"cmkOptions": {
"enabled": true
}
}
3 changes: 3 additions & 0 deletions examples/4-multiple-tsconfig/tsconfig.dir-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"types": [], // Simplify tsserver.log

"paths": { "@/*": ["./*"] }
},
"cmkOptions": {
"enabled": true
}
}
3 changes: 3 additions & 0 deletions examples/5-normal-css/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"incremental": false,
"rootDirs": [".", "generated"],
"types": [] // Simplify tsserver.log
},
"cmkOptions": {
"enabled": true
}
}
3 changes: 3 additions & 0 deletions examples/6-project-external-file/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"incremental": false,
"rootDirs": [".", "generated"],
"types": [] // Simplify tsserver.log
},
"cmkOptions": {
"enabled": true
}
}
8 changes: 5 additions & 3 deletions packages/codegen/e2e-test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ test('generates .d.ts', async () => {
"noEmit": true,
"paths": { "@/*": ["./src/*"] },
"rootDirs": [".", "generated"]
}
},
"cmkOptions": { "enabled": true }
}
`,
});
Expand Down Expand Up @@ -91,7 +92,7 @@ test('prints version number', () => {
test('reports CSS syntax error', async () => {
const iff = await createIFF({
'src/a.module.css': `badword`,
'tsconfig.json': '{}',
'tsconfig.json': '{ "cmkOptions": { "enabled": true } }',
});
const cmk = spawnSync('node', [binPath, '--pretty'], { cwd: iff.rootDir });
expect(cmk.status).toBe(1);
Expand Down Expand Up @@ -138,7 +139,8 @@ test('generates .d.ts with circular import', async () => {
"lib": ["ES2015"],
"noEmit": true,
"rootDirs": [".", "generated"]
}
},
"cmkOptions": { "enabled": true }
}
`,
});
Expand Down
12 changes: 11 additions & 1 deletion packages/codegen/src/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SystemError } from '@css-modules-kit/core';
import type { CMKConfig } from '@css-modules-kit/core';
import { relative, SystemError } from '@css-modules-kit/core';

export class ParseCLIArgsError extends SystemError {
constructor(cause: unknown) {
Expand All @@ -17,3 +18,12 @@ export class ReadCSSModuleFileError extends SystemError {
super('READ_CSS_MODULE_FILE_ERROR', `Failed to read CSS Module file ${fileName}.`, cause);
}
}

export class CMKDisabledError extends SystemError {
constructor(config: CMKConfig) {
super(
'CMK_DISABLED_ERROR',
`css-modules-kit is disabled by configuration. Set \`"cmkOptions": { "enabled": true }\` in ${relative(config.basePath, config.configFileName)} to enable it.`,
);
}
}
2 changes: 1 addition & 1 deletion packages/codegen/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { runCMK, runCMKInWatchMode } from './runner.js';
export { type Logger, createLogger } from './logger/logger.js';
export { WriteDtsFileError, ReadCSSModuleFileError } from './error.js';
export { WriteDtsFileError, ReadCSSModuleFileError, CMKDisabledError } from './error.js';
export { parseCLIArgs, printHelpText, printVersion } from './cli.js';
export { shouldBePretty } from './3rd-party/typescript.js';
3 changes: 3 additions & 0 deletions packages/codegen/src/logger/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('createLogger', () => {
start: { line: 1, column: 2 },
length: 3,
},
{ text: 'text4', category: 'warning' },
];
logger.logDiagnostics(diagnostics);
expect(stripVTControlCharacters(stderrWriteSpy.mock.lastCall![0] as string)).toMatchInlineSnapshot(`
Expand All @@ -35,6 +36,8 @@ describe('createLogger', () => {

a.module.css(1,2): error: text3

warning: text4

"
`);
});
Expand Down
Loading