Skip to content

Commit e186919

Browse files
authored
💥 localsConvention option (#17)
Replaces `--dashes` option with proper `--localsConvention` option that matches PostCSS and other libraries. Changes the default to `dashesOnly` which roughly matches the previous `--dashes` implementation. BREAKING CHANGE: CLI change Adds `lodash.camelcase` to match PostCSS implementation. +semver:minor (0.x semver) Closes #2
1 parent 775b0b3 commit e186919

16 files changed

+270
-90
lines changed

.github/workflows/pipeline.yaml

+7-7
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ jobs:
7878
- name: Run css-typed (the test)
7979
# `node dist/main.js` is executing local `css-typed` as if installed (same as `bin`)
8080
# Use `-I '//.*'` to ignore the first line (comment) which has generated path and timestamp
81-
# Test --dashes in both positions
81+
# Test `--localsConvention` in both positions
8282
run: |
83-
cp src/fixtures/kebab-case/kebab-case.css "$RUNNER_TEMP/kebab-case.css"
83+
cp src/fixtures/casing/casing.css "$RUNNER_TEMP/casing.css"
8484
8585
node dist/main.js "$RUNNER_TEMP/*.css"
86-
diff --strip-trailing-cr -uI '//.*' src/fixtures/kebab-case/kebab-case-default.d.css.ts "$RUNNER_TEMP/kebab-case.d.css.ts"
86+
diff --strip-trailing-cr -uI '//.*' src/fixtures/casing/dashesOnly.d.css.ts "$RUNNER_TEMP/casing.d.css.ts"
8787
88-
node dist/main.js "$RUNNER_TEMP/*.css" --dashes
89-
diff --strip-trailing-cr -uI '//.*' src/fixtures/kebab-case/kebab-case-dashes.d.css.ts "$RUNNER_TEMP/kebab-case.d.css.ts"
88+
node dist/main.js "$RUNNER_TEMP/*.css" --localsConvention camelCaseOnly
89+
diff --strip-trailing-cr -uI '//.*' src/fixtures/casing/camelCaseOnly.d.css.ts "$RUNNER_TEMP/casing.d.css.ts"
9090
91-
node dist/main.js --dashes "$RUNNER_TEMP/*.css"
92-
diff --strip-trailing-cr -uI '//.*' src/fixtures/kebab-case/kebab-case-dashes.d.css.ts "$RUNNER_TEMP/kebab-case.d.css.ts"
91+
node dist/main.js --localsConvention camelCaseOnly "$RUNNER_TEMP/*.css"
92+
diff --strip-trailing-cr -uI '//.*' src/fixtures/casing/camelCaseOnly.d.css.ts "$RUNNER_TEMP/casing.d.css.ts"
9393
9494
Publish:
9595
if: ${{ github.ref == 'refs/heads/main' }}

README.md

+27-18
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,44 @@ Configure TypeScript to allow arbitrary extensions (TS 5+).
4747
```
4848

4949
Add `*.d.css.ts` to your `.gitignore` if appropriate.
50+
(See [#4] for more information about alternative output directory.)
5051

5152
```shell
5253
echo '*.d.css.ts' >> .gitignore
5354
```
5455

56+
[#4]: https://github.com/connorjs/css-typed/issues/4
57+
5558
## Options
5659

5760
The following table lists the options `css-typed` supports.
58-
Prior to the `1.0` release, these may change often.
61+
Also run `css-typed -h` on the command line.
5962

60-
| CLI option | Description |
61-
| :--------: | :---------------------------------------- |
62-
| `--dashes` | Specifies the convention used for locals. |
63+
| CLI option | Default | Description |
64+
| :------------------: | :----------: | :----------------------------- |
65+
| `--localsConvention` | `dashesOnly` | Style of exported class names. |
6366

64-
### Dashes
67+
### localsConvention
6568

66-
_Inspired by [postcss localsConvention](https://github.com/madyankin/postcss-modules/tree/master#localsconvention).
67-
Prior to `v1.0`, this option will evolve to more closely match the `localsConvention` option._
69+
Inspired by [postcss localsConvention](https://github.com/madyankin/postcss-modules#localsconvention).
70+
Adds `none` option value to use the class name as-is.
6871

69-
The `--dashes` option changes the style of exported classnames, the exports in your TS.
72+
The `--localsConvention` option changes the style of exported class names, the exports in your TS (i.e., the JS names).
7073

71-
By default, `css-typed` will emit class names as-is if the name represents a valid JS/TS identifier.
72-
_Note: The logic for “valid” only checks hyphens (dashes, `-`) as of `v0.2.2`._
74+
`css-typed` will only camelize dashes in class names by default (the `dashesOnly` option value).
75+
It will not preserve the original class name.
76+
For example, `my-class` becomes `myClass` and you cannot use `my-class` in JS/TS code.
7377

74-
When passed `dashes`, it will transform `kebab-case` classes (dashed names) to `camelCase`.
75-
For example, `my-class` becomes `myClass`.
78+
Modern bundlers or build system such as Vite and Gatsby support this transformation.
79+
The default matches CSS naming practices (`kebab-case`).
7680

77-
Use `--dashes` when your bundler or build system supports that transformation.
78-
For example, Vite and Gatsby support this.
81+
> **IMPORTANT**
82+
>
83+
> Note that the non-`*Only` values MAY have TypeScript bugs.
84+
> TypeScript 5.6 may help with the named exports for these.
85+
>
86+
> If you encounter a bug, please file an issue.
87+
> In the mean-time, consider using `camelCaseOnly` instead (or `dashesOnly` which is the default).
7988
8089
## Recipes
8190

@@ -86,8 +95,8 @@ To run it as part of your build, you will likely include it as a run script, may
8695
```json
8796
{
8897
"scripts": {
89-
"codegen": "css-typed 'src/**/*.css'",
90-
"pretsc": "css-typed 'src/**/*.css'",
98+
"codegen": "css-typed \"src/**/*.css\"",
99+
"pretsc": "css-typed \"src/**/*.css\"",
91100
"tsc": "tsc"
92101
}
93102
}
@@ -101,8 +110,8 @@ Feel free to [nodemon] or similar.
101110
```json
102111
{
103112
"scripts": {
104-
"codegen": "css-typed 'src/**/*.css'",
105-
"codegen:watch": "nodemon -x 'npm run codegen' -w src -e css"
113+
"codegen": "css-typed \"src/**/*.css\"",
114+
"codegen:watch": "nodemon -x \"npm run codegen\" -w src -e css"
106115
}
107116
}
108117
```

package-lock.json

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"@commander-js/extra-typings": "^12.1.0",
4848
"commander": "^12.1.0",
4949
"css-tree": "^2.3.1",
50-
"glob": "^11.0.0"
50+
"glob": "^11.0.0",
51+
"lodash.camelcase": "^4.3.0"
5152
},
5253
"devDependencies": {
5354
"@types/css-tree": "^2.3.8",
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
2+
3+
const lowercase: string;
4+
const UPPERCASE: string;
5+
const uppercase: string;
6+
const camelCase: string;
7+
const PascalCase: string;
8+
const pascalCase: string;
9+
const _kebabcase: string;
10+
const kebabCase: string;
11+
const snake_case: string;
12+
const snakeCase: string;
13+
const SCREAM_CASE: string;
14+
const screamCase: string;
15+
const _TRAINCASE: string;
16+
const trainCase: string;
17+
18+
export = {
19+
lowercase,
20+
UPPERCASE,
21+
uppercase,
22+
camelCase,
23+
PascalCase,
24+
pascalCase,
25+
"kebab-case": _kebabcase,
26+
kebabCase,
27+
snake_case,
28+
snakeCase,
29+
SCREAM_CASE,
30+
screamCase,
31+
"TRAIN-CASE": _TRAINCASE,
32+
trainCase,
33+
};
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
2+
3+
export const lowercase: string;
4+
export const uppercase: string;
5+
export const camelCase: string;
6+
export const pascalCase: string;
7+
export const kebabCase: string;
8+
export const snakeCase: string;
9+
export const screamCase: string;
10+
export const trainCase: string;

src/fixtures/casing/casing.css

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Example classes with various casing conventions. */
2+
3+
.lowercase,
4+
.UPPERCASE,
5+
.camelCase,
6+
.PascalCase,
7+
.kebab-case,
8+
.snake_case,
9+
.SCREAM_CASE,
10+
.TRAIN-CASE {
11+
display: flex;
12+
}

src/fixtures/casing/dashes.d.css.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
2+
3+
const lowercase: string;
4+
const UPPERCASE: string;
5+
const camelCase: string;
6+
const PascalCase: string;
7+
const _kebabcase: string;
8+
const kebabCase: string;
9+
const snake_case: string;
10+
const SCREAM_CASE: string;
11+
const _TRAINCASE: string;
12+
const TRAINCASE: string;
13+
14+
export = {
15+
lowercase,
16+
UPPERCASE,
17+
camelCase,
18+
PascalCase,
19+
"kebab-case": _kebabcase,
20+
kebabCase,
21+
snake_case,
22+
SCREAM_CASE,
23+
"TRAIN-CASE": _TRAINCASE,
24+
TRAINCASE,
25+
};
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
2+
3+
export const lowercase: string;
4+
export const UPPERCASE: string;
5+
export const camelCase: string;
6+
export const PascalCase: string;
7+
export const kebabCase: string;
8+
export const snake_case: string;
9+
export const SCREAM_CASE: string;
10+
export const TRAINCASE: string;

src/fixtures/casing/none.d.css.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
2+
3+
const lowercase: string;
4+
const UPPERCASE: string;
5+
const camelCase: string;
6+
const PascalCase: string;
7+
const _kebabcase: string;
8+
const snake_case: string;
9+
const SCREAM_CASE: string;
10+
const _TRAINCASE: string;
11+
12+
export = {
13+
lowercase,
14+
UPPERCASE,
15+
camelCase,
16+
PascalCase,
17+
"kebab-case": _kebabcase,
18+
snake_case,
19+
SCREAM_CASE,
20+
"TRAIN-CASE": _TRAINCASE,
21+
};

src/fixtures/kebab-case/kebab-case-dashes.d.css.ts

-7
This file was deleted.

src/fixtures/kebab-case/kebab-case-default.d.css.ts

-4
This file was deleted.

src/fixtures/kebab-case/kebab-case.css

-22
This file was deleted.

0 commit comments

Comments
 (0)