happy-css-modules 3.2.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +23 -0
- package/README.md +307 -2
- package/bin/hcm.js +1 -1
- package/dist/cli.d.ts +0 -4
- package/dist/cli.js +111 -118
- package/dist/cli.js.map +1 -1
- package/dist/emitter/dts.js +7 -8
- package/dist/emitter/dts.js.map +1 -1
- package/dist/emitter/dts.test.js +12 -12
- package/dist/emitter/file-system.js +2 -2
- package/dist/emitter/file-system.js.map +1 -1
- package/dist/emitter/file-system.test.js +1 -1
- package/dist/emitter/file-system.test.js.map +1 -1
- package/dist/emitter/index.js +1 -1
- package/dist/emitter/index.js.map +1 -1
- package/dist/emitter/index.test.js +2 -4
- package/dist/emitter/index.test.js.map +1 -1
- package/dist/emitter/source-map.js +1 -1
- package/dist/emitter/source-map.js.map +1 -1
- package/dist/emitter/source-map.test.js +1 -1
- package/dist/emitter/source-map.test.js.map +1 -1
- package/dist/integration-test/go-to-definition.test.js +403 -105
- package/dist/integration-test/go-to-definition.test.js.map +1 -1
- package/dist/locator/index.js +10 -8
- package/dist/locator/index.js.map +1 -1
- package/dist/locator/index.test.js +304 -171
- package/dist/locator/index.test.js.map +1 -1
- package/dist/locator/postcss.js +2 -3
- package/dist/locator/postcss.js.map +1 -1
- package/dist/locator/postcss.test.js +317 -73
- package/dist/locator/postcss.test.js.map +1 -1
- package/dist/logger.js +7 -6
- package/dist/logger.js.map +1 -1
- package/dist/regression-test/issue-168.test.js +3 -5
- package/dist/regression-test/issue-168.test.js.map +1 -1
- package/dist/resolver/index.d.ts +1 -1
- package/dist/resolver/index.js +3 -3
- package/dist/resolver/index.js.map +1 -1
- package/dist/resolver/node-resolver.js +1 -1
- package/dist/resolver/node-resolver.js.map +1 -1
- package/dist/resolver/relative-resolver.js +1 -1
- package/dist/resolver/relative-resolver.js.map +1 -1
- package/dist/resolver/webpack-resolver.d.ts +1 -1
- package/dist/resolver/webpack-resolver.js +4 -4
- package/dist/resolver/webpack-resolver.js.map +1 -1
- package/dist/runner.js +52 -21
- package/dist/runner.js.map +1 -1
- package/dist/runner.test.js +59 -43
- package/dist/runner.test.js.map +1 -1
- package/dist/test-util/line-column.d.ts +9 -0
- package/dist/test-util/line-column.js +16 -0
- package/dist/test-util/line-column.js.map +1 -0
- package/dist/test-util/line-column.test.d.ts +1 -0
- package/dist/test-util/line-column.test.js +21 -0
- package/dist/test-util/line-column.test.js.map +1 -0
- package/dist/test-util/tsserver.js +11 -12
- package/dist/test-util/tsserver.js.map +1 -1
- package/dist/test-util/util.d.ts +6 -0
- package/dist/test-util/util.js +23 -6
- package/dist/test-util/util.js.map +1 -1
- package/dist/transformer/index.d.ts +0 -1
- package/dist/transformer/index.js +1 -1
- package/dist/transformer/index.js.map +1 -1
- package/dist/transformer/index.test.js +17 -17
- package/dist/transformer/index.test.js.map +1 -1
- package/dist/transformer/less-transformer.js +4 -4
- package/dist/transformer/less-transformer.js.map +1 -1
- package/dist/transformer/less-transformer.test.js +76 -51
- package/dist/transformer/less-transformer.test.js.map +1 -1
- package/dist/transformer/postcss-transformer.test.js +58 -54
- package/dist/transformer/postcss-transformer.test.js.map +1 -1
- package/dist/transformer/scss-transformer.js +11 -30
- package/dist/transformer/scss-transformer.js.map +1 -1
- package/dist/transformer/scss-transformer.test.js +106 -54
- package/dist/transformer/scss-transformer.test.js.map +1 -1
- package/dist/util.js +6 -8
- package/dist/util.js.map +1 -1
- package/dist/util.test.js +2 -2
- package/dist/util.test.js.map +1 -1
- package/package.json +59 -35
- package/src/__snapshots__/runner.test.ts.snap +1 -1
- package/src/cli.ts +119 -117
- package/src/emitter/dts.test.ts +12 -12
- package/src/emitter/dts.ts +27 -28
- package/src/emitter/file-system.test.ts +1 -1
- package/src/emitter/file-system.ts +2 -2
- package/src/emitter/index.test.ts +2 -4
- package/src/emitter/index.ts +1 -1
- package/src/emitter/source-map.test.ts +1 -1
- package/src/emitter/source-map.ts +1 -1
- package/src/integration-test/go-to-definition.test.ts +405 -105
- package/src/locator/index.test.ts +304 -171
- package/src/locator/index.ts +6 -6
- package/src/locator/postcss.test.ts +317 -73
- package/src/locator/postcss.ts +2 -3
- package/src/logger.ts +6 -6
- package/src/regression-test/issue-168.test.ts +3 -5
- package/src/resolver/index.ts +4 -4
- package/src/resolver/node-resolver.ts +1 -1
- package/src/resolver/relative-resolver.ts +1 -1
- package/src/resolver/webpack-resolver.ts +5 -5
- package/src/runner.test.ts +66 -43
- package/src/runner.ts +53 -22
- package/src/test-util/line-column.test.ts +21 -0
- package/src/test-util/line-column.ts +15 -0
- package/src/test-util/tsserver.ts +11 -12
- package/src/test-util/util.ts +24 -6
- package/src/transformer/index.test.ts +17 -17
- package/src/transformer/index.ts +1 -1
- package/src/transformer/less-transformer.test.ts +74 -47
- package/src/transformer/less-transformer.ts +1 -3
- package/src/transformer/postcss-transformer.test.ts +58 -54
- package/src/transformer/scss-transformer.test.ts +108 -54
- package/src/transformer/scss-transformer.ts +13 -30
- package/src/util.test.ts +2 -2
- package/src/util.ts +6 -8
- package/dist/test-util/jest/resolver.cjs +0 -31
- package/dist/test-util/jest/resolver.cjs.map +0 -1
- package/dist/test-util/jest/resolver.d.cts +0 -16
- package/src/test-util/jest/resolver.cjs +0 -31
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 mizdra
|
|
4
|
+
Copyright (c) [2016] [Yosuke Kurami]
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
23
|
+
|
package/README.md
CHANGED
|
@@ -1,5 +1,310 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img alt="Cover image" src="./docs/cover.svg" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h2 align="center">Happy CSS Modules</h2>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<em>Typed, definition jumpable CSS Modules.</em>
|
|
9
|
+
<br />
|
|
10
|
+
<em>Moreover, easy!</em>
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
https://user-images.githubusercontent.com/9639995/189538880-872ad38d-2c9d-4c19-b257-521018963eec.mov
|
|
14
|
+
|
|
15
|
+
## NOTICE: Migration to css-modules-kit
|
|
16
|
+
|
|
17
|
+
Consider migrating to [`mizdra/css-modules-kit`](https://github.com/mizdra/css-modules-kit), which is the successor to happy-css-modules. It offers enhanced features such as Renaming and Find All References support.
|
|
18
|
+
|
|
19
|
+
Please note that `css-modules-kit` **does not support** Sass and Less. There are no plans to add support in the future. If you depend on these preprocessors, continue using `happy-css-modules`.
|
|
20
|
+
|
|
21
|
+
See the [`css-modules-kit` repository](https://github.com/mizdra/css-modules-kit).
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- :white_check_mark: Strict type checking
|
|
26
|
+
- Generate `.d.ts` of CSS Modules for type checking
|
|
27
|
+
- :mag: Definition jumps
|
|
28
|
+
- Clicking on a property on `.jsx`/`.tsx` will jump to the source of the definition on `.module.css`.
|
|
29
|
+
- This is accomplished by generating `.d.ts.map` (a.k.a. [Declaration Map](https://www.typescriptlang.org/tsconfig#declarationMap)).
|
|
30
|
+
- :handshake: High compatibility with the ecosystem
|
|
31
|
+
- Support for Postcss/Sass/Less
|
|
32
|
+
- Implement webpack-compatible resolving algorithms
|
|
33
|
+
- Also supports [`resolve.alias`](https://webpack.js.org/configuration/resolve/#resolvealias)
|
|
34
|
+
- :beginner: Easy to use
|
|
35
|
+
- No configuration file, some simple CLI options
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```console
|
|
40
|
+
$ npm i -D happy-css-modules
|
|
41
|
+
```
|
|
2
42
|
|
|
3
43
|
## Usage
|
|
4
44
|
|
|
5
|
-
|
|
45
|
+
In the simple case, everything goes well with the following!
|
|
46
|
+
|
|
47
|
+
```console
|
|
48
|
+
$ hcm 'src/**/*.module.{css,scss,less}'
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
If you want to customize the behavior, see `--help`.
|
|
52
|
+
|
|
53
|
+
```console
|
|
54
|
+
$ hcm --help
|
|
55
|
+
Generate .d.ts and .d.ts.map for CSS modules.
|
|
56
|
+
|
|
57
|
+
Usage: hcm [options] <glob>
|
|
58
|
+
|
|
59
|
+
Options:
|
|
60
|
+
-w, --[no-]watch Watch input directory's css files or pattern (default: false)
|
|
61
|
+
--localsConvention Style of exported class names.
|
|
62
|
+
[choices: camelCase, camelCaseOnly, dashes, dashesOnly]
|
|
63
|
+
--[no-]declarationMap Create sourcemaps for d.ts files (default: true)
|
|
64
|
+
--sassLoadPaths The option compatible with sass's `--load-path`.
|
|
65
|
+
--lessIncludePaths The option compatible with less's `--include-path`.
|
|
66
|
+
--webpackResolveAlias The option compatible with webpack's `resolve.alias`.
|
|
67
|
+
--postcssConfig The option compatible with postcss's `--config`.
|
|
68
|
+
--[no-]arbitraryExtensions Generate `.d.css.ts` instead of `.css.d.ts` (default: false)
|
|
69
|
+
--[no-]cache Only generate .d.ts and .d.ts.map for changed files (default: true)
|
|
70
|
+
--cacheStrategy Strategy for the cache to use for detecting changed files.
|
|
71
|
+
[choices: content, metadata] (default: content)
|
|
72
|
+
--logLevel What level of logs to report.
|
|
73
|
+
[choices: debug, info, silent] (default: info)
|
|
74
|
+
-o, --outDir Output directory for generated files.
|
|
75
|
+
-h, --help Show help
|
|
76
|
+
-v, --version Show version number
|
|
77
|
+
|
|
78
|
+
Examples:
|
|
79
|
+
hcm 'src/**/*.module.css'
|
|
80
|
+
hcm 'src/**/*.module.{css,scss,less}'
|
|
81
|
+
hcm 'src/**/*.module.css' --watch
|
|
82
|
+
hcm 'src/**/*.module.css' --no-declarationMap
|
|
83
|
+
hcm 'src/**/*.module.css' --sassLoadPaths src/style
|
|
84
|
+
hcm 'src/**/*.module.css' --lessIncludePaths src/style
|
|
85
|
+
hcm 'src/**/*.module.css' --webpackResolveAlias '{"@": "src"}'
|
|
86
|
+
hcm 'src/**/*.module.css' --no-cache
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## How docs definition jumps work?
|
|
90
|
+
|
|
91
|
+
In addition to `.module.css.d.ts`, happy-css-modules also generates a `.module.css.d.ts.map` file (a.k.a. [Declaration Map](https://www.typescriptlang.org/tsconfig#declarationMap)). This file is a Source Map that contains code mapping information from generated (`.module.css.d.ts`) to source (`.module.css`).
|
|
92
|
+
|
|
93
|
+
When tsserver (TypeScript Language Server for VSCode) tries to jump to the code on `.module.css.d.ts`, it restores the original location from this Source Map and redirects to the code on` .module.css`. happy-css-modules uses this mechanism to realize definition jump.
|
|
94
|
+
|
|
95
|
+

|
|
96
|
+
|
|
97
|
+
The case of multiple definitions is a bit more complicated. This is because the Source Map specification does not allow for a 1:N mapping of the generated:original locations. Therefore, happy-css-modules define multiple definitions of the same property type and map each property to a different location in `.module.css`.
|
|
98
|
+
|
|
99
|
+

|
|
100
|
+
|
|
101
|
+
## How to use `--outDir` option
|
|
102
|
+
|
|
103
|
+
Use `--outDir` to output `.module.css.d.ts` and `.module.css.d.ts.map` in a separate directory. This is useful for keeping the `src/` directory clean.
|
|
104
|
+
|
|
105
|
+
However, by default tsc and tsserver cannot load it. To enable tsc or tsserver to load them, use the [`rootDirs`](https://www.typescriptlang.org/tsconfig/#rootDirs) option in `tsconfig.json`. An example is given below.
|
|
106
|
+
|
|
107
|
+
```json
|
|
108
|
+
// package.json
|
|
109
|
+
{
|
|
110
|
+
"scripts": {
|
|
111
|
+
"gen": "hcm -o generated/hcm 'src/**/*.module.css'"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
// tsconfig.json
|
|
118
|
+
{
|
|
119
|
+
"compilerOptions": {
|
|
120
|
+
"rootDirs": ["src", "generated/hcm/src"]
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Node.js API (Experimental)
|
|
126
|
+
|
|
127
|
+
> **Warning**
|
|
128
|
+
> This feature is experimental and may change significantly. The API is not stable and may have breaking changes even in minor or patch version updates.
|
|
129
|
+
|
|
130
|
+
`happy-css-modules` provides Node.js API for programmatically generating .d.ts and .d.ts.map.
|
|
131
|
+
|
|
132
|
+
See [src/index.ts](https://github.com/mizdra/happy-css-modules/blob/main/src/index.ts) for available API.
|
|
133
|
+
|
|
134
|
+
### Example: Custom `hcm` commands
|
|
135
|
+
|
|
136
|
+
You can create your own customized `hcm` commands. We also provide a `parseArgv` utility that parses `process.argv` and extracts options.
|
|
137
|
+
|
|
138
|
+
```javascript
|
|
139
|
+
#!/usr/bin/env ts-node
|
|
140
|
+
// scripts/hcm.ts
|
|
141
|
+
|
|
142
|
+
import { run, parseArgv } from 'happy-css-modules';
|
|
143
|
+
|
|
144
|
+
// Write your code here...
|
|
145
|
+
|
|
146
|
+
run({
|
|
147
|
+
// Inherit default CLI options (e.g. --watch).
|
|
148
|
+
...parseArgv(process.argv),
|
|
149
|
+
// Add custom CLI options.
|
|
150
|
+
cwd: __dirname,
|
|
151
|
+
}).catch((e) => {
|
|
152
|
+
console.error(e);
|
|
153
|
+
process.exit(1);
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Example: Custom transformer
|
|
158
|
+
|
|
159
|
+
With the `transformer` option, you can use AltCSS, which is not supported by `happy-css-modules`.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
#!/usr/bin/env ts-node
|
|
163
|
+
|
|
164
|
+
import { run, parseArgv, createDefaultTransformer, type Transformer } from 'happy-css-modules';
|
|
165
|
+
import sass from 'sass';
|
|
166
|
+
import { promisify } from 'util';
|
|
167
|
+
|
|
168
|
+
const defaultTransformer = createDefaultTransformer();
|
|
169
|
+
const render = promisify(sass.render);
|
|
170
|
+
|
|
171
|
+
// The custom transformer supporting sass indented syntax
|
|
172
|
+
const transformer: Transformer = async (source, options) => {
|
|
173
|
+
if (from.endsWith('.sass')) {
|
|
174
|
+
const result = await render({
|
|
175
|
+
// Use indented syntax.
|
|
176
|
+
// ref: https://sass-lang.com/documentation/syntax#the-indented-syntax
|
|
177
|
+
indentedSyntax: true,
|
|
178
|
+
data: source,
|
|
179
|
+
file: options.from,
|
|
180
|
+
outFile: 'DUMMY',
|
|
181
|
+
// Output sourceMap.
|
|
182
|
+
sourceMap: true,
|
|
183
|
+
// Resolve import specifier using resolver.
|
|
184
|
+
importer: (url, prev, done) => {
|
|
185
|
+
options
|
|
186
|
+
.resolver(url, { request: prev })
|
|
187
|
+
.then((resolved) => done({ file: resolved }))
|
|
188
|
+
.catch((e) => done(e));
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
return { css: result.css, map: result.sourceMap!, dependencies: result.loadedUrls };
|
|
192
|
+
}
|
|
193
|
+
// Fallback to default transformer.
|
|
194
|
+
return await defaultTransformer(source, from);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
run({ ...parseArgv(process.argv), transformer }).catch((e) => {
|
|
198
|
+
console.error(e);
|
|
199
|
+
process.exit(1);
|
|
200
|
+
});
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Example: Custom resolver
|
|
204
|
+
|
|
205
|
+
With the `resolver` option, you can customize the resolution algorithm for import specifier (such as `@import "specifier"`).
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
#!/usr/bin/env ts-node
|
|
209
|
+
|
|
210
|
+
import { run, parseArgv, createDefaultResolver, type Resolver } from 'happy-css-modules';
|
|
211
|
+
import { exists } from 'fs/promises';
|
|
212
|
+
import { resolve, join } from 'path';
|
|
213
|
+
|
|
214
|
+
const cwd = process.cwd();
|
|
215
|
+
const runnerOptions = parseArgv(process.argv);
|
|
216
|
+
const { sassLoadPaths, lessIncludePaths, webpackResolveAlias } = runnerOptions;
|
|
217
|
+
// Some runner options must be passed to the default resolver.
|
|
218
|
+
const defaultResolver = createDefaultResolver({ cwd, sassLoadPaths, lessIncludePaths, webpackResolveAlias });
|
|
219
|
+
const stylesDir = resolve(__dirname, 'src/styles');
|
|
220
|
+
|
|
221
|
+
const resolver: Resolver = async (specifier, options) => {
|
|
222
|
+
// If the default resolver cannot resolve, fallback to a customized resolve algorithm.
|
|
223
|
+
const resolvedByDefaultResolver = await defaultResolver(specifier, options);
|
|
224
|
+
if (resolvedByDefaultResolver === false) {
|
|
225
|
+
// Search for files in `src/styles` directory.
|
|
226
|
+
const path = join(stylesDir, specifier);
|
|
227
|
+
if (await exists(path)) return path;
|
|
228
|
+
}
|
|
229
|
+
// Returns `false` if specifier cannot be resolved.
|
|
230
|
+
return false;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
run({ ...runnerOptions, resolver, cwd }).catch((e) => {
|
|
234
|
+
console.error(e);
|
|
235
|
+
process.exit(1);
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Example: Get locations for selectors exported by CSS Modules
|
|
240
|
+
|
|
241
|
+
`Locator` can be used to get location for selectors exported by CSS Modules.
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import { Locator } from 'happy-css-modules';
|
|
245
|
+
import { resolve } from 'path';
|
|
246
|
+
import assert from 'assert';
|
|
247
|
+
|
|
248
|
+
const locator = new Locator({
|
|
249
|
+
// You can customize the transformer and resolver used by the locator.
|
|
250
|
+
// transformer: createDefaultTransformer(),
|
|
251
|
+
// resolver: createDefaultResolver(),
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
// Process https://github.com/mizdra/happy-css-modules/blob/main/example/02-import/2.css
|
|
255
|
+
const filePath = resolve('example/02-import/2.css'); // Convert to absolute path
|
|
256
|
+
const result = await locator.load(filePath);
|
|
257
|
+
|
|
258
|
+
assert.deepEqual(result, {
|
|
259
|
+
dependencies: ['/Users/mizdra/src/github.com/mizdra/example/02-import/3.css'],
|
|
260
|
+
tokens: [
|
|
261
|
+
{
|
|
262
|
+
name: 'b',
|
|
263
|
+
originalLocation: {
|
|
264
|
+
filePath: '/Users/mizdra/src/github.com/mizdra/example/02-import/3.css',
|
|
265
|
+
start: { line: 1, column: 1 },
|
|
266
|
+
end: { line: 1, column: 2 },
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
name: 'a',
|
|
271
|
+
originalLocation: {
|
|
272
|
+
filePath: '/Users/mizdra/src/github.com/mizdra/example/02-import/2.css',
|
|
273
|
+
start: { line: 3, column: 1 },
|
|
274
|
+
end: { line: 3, column: 2 },
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
],
|
|
278
|
+
});
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## About the origins of this project
|
|
282
|
+
|
|
283
|
+
This project was born as a PoC for [Quramy/typed-css-modules#177](https://github.com/Quramy/typed-css-modules/issues/177). That is why this project forks [`Quramy/typed-css-modules`](https://github.com/Quramy/typed-css-modules). Due to refactoring, only a small amount of code now comes from `Quramy/typed-css-modules`, but its contributions can still be found in the credits of the license.
|
|
284
|
+
|
|
285
|
+
Thank you [@Quramy](https://github.com/Quramy)!
|
|
286
|
+
|
|
287
|
+
## Prior art
|
|
288
|
+
|
|
289
|
+
There is a lot of excellent prior art.
|
|
290
|
+
|
|
291
|
+
- ✅ Supported
|
|
292
|
+
- 🔶 Partially supported
|
|
293
|
+
- 🛑 Not supported
|
|
294
|
+
- ❓ Unknown
|
|
295
|
+
|
|
296
|
+
| Repository | Strict type checking | Definition jumps | Sass | Less | `resolve.alias` | How implemented |
|
|
297
|
+
| :------------------------------------------------------------------------------------------------ | :------------------: | :--------------: | :--: | :--: | :-------------: | :---------------------------------------: |
|
|
298
|
+
| [Quramy/typed-css-modules](https://github.com/Quramy/typed-css-modules) | ✅ | 🛑 | 🛑 | 🛑 | 🛑 | CLI Tool |
|
|
299
|
+
| [skovy/typed-scss-modules](https://github.com/skovy/typed-scss-modules) | ✅ | 🛑 | ✅ | 🛑 | 🛑 | CLI Tool |
|
|
300
|
+
| [qiniu/typed-less-modules](https://github.com/qiniu/typed-less-modules) | ✅ | 🛑 | 🛑 | ✅ | 🛑 | CLI Tool |
|
|
301
|
+
| [mrmckeb/typescript-plugin-css-modules](https://github.com/mrmckeb/typescript-plugin-css-modules) | 🔶<sup>\*1</sup> | 🔶<sup>\*2</sup> | ✅ | ✅ | 🛑 | TypeScript Language Service<sup>\*3</sup> |
|
|
302
|
+
| [clinyong/vscode-css-modules](https://github.com/clinyong/vscode-css-modules) | 🛑 | ✅ | ✅ | ✅ | 🛑 | VSCode Extension |
|
|
303
|
+
| [Viijay-Kr/react-ts-css](https://github.com/Viijay-Kr/react-ts-css) | 🔶<sup>\*1</sup> | ✅ | ✅ | ✅ | ❓ | VSCode Extension |
|
|
304
|
+
| [mizdra/happy-css-modules](https://github.com/mizdra/happy-css-modules) | ✅ | ✅ | ✅ | ✅ | ✅ | CLI Tool + Declaration Map |
|
|
305
|
+
|
|
306
|
+
- \*1: Warnings are displayed in the editor, but not at compile time.
|
|
307
|
+
- \*2: Not supported for `.less` definition jumps.
|
|
308
|
+
- \*3: The TypeScript language service can display warnings in the editor, but not at compile time. It is also complicated to set up.
|
|
309
|
+
|
|
310
|
+
Another known tool for generating `.css.d.ts` is [wix/stylable](https://github.com/wix/stylable) , which does not use CSS Modules.
|
package/bin/hcm.js
CHANGED
package/dist/cli.d.ts
CHANGED
package/dist/cli.js
CHANGED
|
@@ -1,127 +1,120 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { hideBin } from 'yargs/helpers';
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
3
2
|
import { DEFAULT_ARBITRARY_EXTENSIONS } from './config.js';
|
|
4
3
|
import { getPackageJson } from './util.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
const HELP_TEXT = `
|
|
5
|
+
Generate .d.ts and .d.ts.map for CSS modules.
|
|
6
|
+
|
|
7
|
+
Usage: hcm [options] <glob>
|
|
8
|
+
|
|
9
|
+
Options:
|
|
10
|
+
-w, --[no-]watch Watch input directory's css files or pattern (default: false)
|
|
11
|
+
--localsConvention Style of exported class names.
|
|
12
|
+
[choices: camelCase, camelCaseOnly, dashes, dashesOnly]
|
|
13
|
+
--[no-]declarationMap Create sourcemaps for d.ts files (default: true)
|
|
14
|
+
--sassLoadPaths The option compatible with sass's \`--load-path\`.
|
|
15
|
+
--lessIncludePaths The option compatible with less's \`--include-path\`.
|
|
16
|
+
--webpackResolveAlias The option compatible with webpack's \`resolve.alias\`.
|
|
17
|
+
--postcssConfig The option compatible with postcss's \`--config\`.
|
|
18
|
+
--[no-]arbitraryExtensions Generate \`.d.css.ts\` instead of \`.css.d.ts\` (default: false)
|
|
19
|
+
--[no-]cache Only generate .d.ts and .d.ts.map for changed files (default: true)
|
|
20
|
+
--cacheStrategy Strategy for the cache to use for detecting changed files.
|
|
21
|
+
[choices: content, metadata] (default: content)
|
|
22
|
+
--logLevel What level of logs to report.
|
|
23
|
+
[choices: debug, info, silent] (default: info)
|
|
24
|
+
-o, --outDir Output directory for generated files.
|
|
25
|
+
-h, --help Show help
|
|
26
|
+
-v, --version Show version number
|
|
27
|
+
|
|
28
|
+
Examples:
|
|
29
|
+
hcm 'src/**/*.module.css'
|
|
30
|
+
hcm 'src/**/*.module.{css,scss,less}'
|
|
31
|
+
hcm 'src/**/*.module.css' --watch
|
|
32
|
+
hcm 'src/**/*.module.css' --no-declarationMap
|
|
33
|
+
hcm 'src/**/*.module.css' --sassLoadPaths src/style
|
|
34
|
+
hcm 'src/**/*.module.css' --lessIncludePaths src/style
|
|
35
|
+
hcm 'src/**/*.module.css' --webpackResolveAlias '{"@": "src"}'
|
|
36
|
+
hcm 'src/**/*.module.css' --no-cache
|
|
37
|
+
`.trim();
|
|
9
38
|
export function parseArgv(argv) {
|
|
10
39
|
const pkgJson = getPackageJson();
|
|
11
|
-
const
|
|
12
|
-
.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.option('cache', {
|
|
64
|
-
type: 'boolean',
|
|
65
|
-
default: true,
|
|
66
|
-
describe: 'Only generate .d.ts and .d.ts.map for changed files.',
|
|
67
|
-
})
|
|
68
|
-
.option('cacheStrategy', {
|
|
69
|
-
choices: ['content', 'metadata'],
|
|
70
|
-
// NOTE: This is a workaround for `parsedArgv.cacheStrategy` type breaks.
|
|
71
|
-
default: 'content',
|
|
72
|
-
describe: 'Strategy for the cache to use for detecting changed files.',
|
|
73
|
-
})
|
|
74
|
-
.option('logLevel', {
|
|
75
|
-
choices: ['debug', 'info', 'silent'],
|
|
76
|
-
default: 'info',
|
|
77
|
-
describe: 'What level of logs to report.',
|
|
78
|
-
})
|
|
79
|
-
.option('outDir', {
|
|
80
|
-
type: 'string',
|
|
81
|
-
alias: 'o',
|
|
82
|
-
describe: 'Output directory for generated files.',
|
|
83
|
-
})
|
|
84
|
-
.alias('h', 'help')
|
|
85
|
-
.alias('v', 'version')
|
|
86
|
-
.version(pkgJson.version)
|
|
87
|
-
.check((argv) => {
|
|
88
|
-
const patterns = argv._;
|
|
89
|
-
// TODO: support multiple patterns
|
|
90
|
-
if (patterns.length !== 1)
|
|
91
|
-
throw new Error('Only one pattern is allowed.');
|
|
92
|
-
if (argv.webpackResolveAlias) {
|
|
93
|
-
let parsedWebpackResolveAlias;
|
|
94
|
-
try {
|
|
95
|
-
parsedWebpackResolveAlias = JSON.parse(argv.webpackResolveAlias);
|
|
96
|
-
}
|
|
97
|
-
catch (e) {
|
|
98
|
-
throw new Error('--webpackResolveAlias must be a valid JSON string.');
|
|
99
|
-
}
|
|
100
|
-
if (typeof parsedWebpackResolveAlias !== 'object' || parsedWebpackResolveAlias === null)
|
|
101
|
-
throw new Error('--webpackResolveAlias must be an object');
|
|
102
|
-
if (!Object.keys(parsedWebpackResolveAlias).every((key) => typeof key === 'string'))
|
|
103
|
-
throw new Error('--webpackResolveAlias must be an object of string keys');
|
|
104
|
-
if (!Object.values(parsedWebpackResolveAlias).every((value) => typeof value === 'string'))
|
|
105
|
-
throw new Error('--webpackResolveAlias must be an object of string values');
|
|
40
|
+
const { values, positionals } = parseArgs({
|
|
41
|
+
args: argv.slice(2),
|
|
42
|
+
options: {
|
|
43
|
+
watch: { type: 'boolean', short: 'w', default: false },
|
|
44
|
+
localsConvention: { type: 'string' },
|
|
45
|
+
declarationMap: { type: 'boolean', default: true },
|
|
46
|
+
sassLoadPaths: { type: 'string', multiple: true },
|
|
47
|
+
lessIncludePaths: { type: 'string', multiple: true },
|
|
48
|
+
webpackResolveAlias: { type: 'string' },
|
|
49
|
+
postcssConfig: { type: 'string' },
|
|
50
|
+
arbitraryExtensions: { type: 'boolean', default: DEFAULT_ARBITRARY_EXTENSIONS },
|
|
51
|
+
cache: { type: 'boolean', default: true },
|
|
52
|
+
cacheStrategy: { type: 'string', default: 'content' },
|
|
53
|
+
logLevel: { type: 'string', default: 'info' },
|
|
54
|
+
outDir: { type: 'string', short: 'o' },
|
|
55
|
+
help: { type: 'boolean', short: 'h' },
|
|
56
|
+
version: { type: 'boolean', short: 'v' },
|
|
57
|
+
},
|
|
58
|
+
allowPositionals: true,
|
|
59
|
+
allowNegative: true,
|
|
60
|
+
});
|
|
61
|
+
if (values.help) {
|
|
62
|
+
// oxlint-disable-next-line no-console
|
|
63
|
+
console.log(HELP_TEXT);
|
|
64
|
+
// oxlint-disable-next-line unicorn/no-process-exit
|
|
65
|
+
process.exit(0);
|
|
66
|
+
}
|
|
67
|
+
if (values.version) {
|
|
68
|
+
// oxlint-disable-next-line no-console
|
|
69
|
+
console.log(pkgJson.version);
|
|
70
|
+
// oxlint-disable-next-line unicorn/no-process-exit
|
|
71
|
+
process.exit(0);
|
|
72
|
+
}
|
|
73
|
+
if (positionals.length !== 1)
|
|
74
|
+
throw new Error('Only one pattern is allowed.');
|
|
75
|
+
const validLocalsConventions = ['camelCase', 'camelCaseOnly', 'dashes', 'dashesOnly'];
|
|
76
|
+
if (values.localsConvention !== undefined && !validLocalsConventions.includes(values.localsConvention)) {
|
|
77
|
+
throw new Error(`--localsConvention must be one of: ${validLocalsConventions.join(', ')}`);
|
|
78
|
+
}
|
|
79
|
+
const validCacheStrategies = ['content', 'metadata'];
|
|
80
|
+
if (!validCacheStrategies.includes(values.cacheStrategy)) {
|
|
81
|
+
throw new Error(`--cacheStrategy must be one of: ${validCacheStrategies.join(', ')}`);
|
|
82
|
+
}
|
|
83
|
+
const validLogLevels = ['debug', 'info', 'silent'];
|
|
84
|
+
if (!validLogLevels.includes(values.logLevel)) {
|
|
85
|
+
throw new Error(`--logLevel must be one of: ${validLogLevels.join(', ')}`);
|
|
86
|
+
}
|
|
87
|
+
let webpackResolveAlias;
|
|
88
|
+
if (values.webpackResolveAlias) {
|
|
89
|
+
let parsed;
|
|
90
|
+
try {
|
|
91
|
+
parsed = JSON.parse(values.webpackResolveAlias);
|
|
106
92
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
93
|
+
catch {
|
|
94
|
+
throw new Error('--webpackResolveAlias must be a valid JSON string.');
|
|
95
|
+
}
|
|
96
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
97
|
+
throw new Error('--webpackResolveAlias must be an object');
|
|
98
|
+
if (!Object.keys(parsed).every((key) => typeof key === 'string'))
|
|
99
|
+
throw new Error('--webpackResolveAlias must be an object of string keys');
|
|
100
|
+
if (!Object.values(parsed).every((value) => typeof value === 'string'))
|
|
101
|
+
throw new Error('--webpackResolveAlias must be an object of string values');
|
|
102
|
+
webpackResolveAlias = parsed;
|
|
103
|
+
}
|
|
111
104
|
return {
|
|
112
|
-
pattern:
|
|
113
|
-
watch:
|
|
114
|
-
localsConvention:
|
|
115
|
-
declarationMap:
|
|
116
|
-
sassLoadPaths:
|
|
117
|
-
lessIncludePaths:
|
|
118
|
-
webpackResolveAlias
|
|
119
|
-
postcssConfig:
|
|
120
|
-
arbitraryExtensions:
|
|
121
|
-
cache:
|
|
122
|
-
cacheStrategy:
|
|
123
|
-
logLevel:
|
|
124
|
-
outDir:
|
|
105
|
+
pattern: positionals[0],
|
|
106
|
+
watch: values.watch,
|
|
107
|
+
localsConvention: values.localsConvention,
|
|
108
|
+
declarationMap: values.declarationMap,
|
|
109
|
+
sassLoadPaths: values.sassLoadPaths,
|
|
110
|
+
lessIncludePaths: values.lessIncludePaths,
|
|
111
|
+
webpackResolveAlias,
|
|
112
|
+
postcssConfig: values.postcssConfig,
|
|
113
|
+
arbitraryExtensions: values.arbitraryExtensions,
|
|
114
|
+
cache: values.cache,
|
|
115
|
+
cacheStrategy: values.cacheStrategy,
|
|
116
|
+
logLevel: values.logLevel,
|
|
117
|
+
outDir: values.outDir,
|
|
125
118
|
};
|
|
126
119
|
}
|
|
127
120
|
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAE3D,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE3C,MAAM,SAAS,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiCjB,CAAC,IAAI,EAAE,CAAC;AAET,MAAM,UAAU,SAAS,CAAC,IAAc;IACtC,MAAM,OAAO,GAAG,cAAc,EAAE,CAAC;IAEjC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,SAAS,CAAC;QACxC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;QACnB,OAAO,EAAE;YACP,KAAK,EAAE,EAAE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