Skip to content

Commit f6ca999

Browse files
committed
Merge branch 'release/1.1.0'
2 parents cf99d4d + 598c209 commit f6ca999

20 files changed

+1666
-1324
lines changed

.npmignore

-12
This file was deleted.

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/helpers/__tests__/fixtures/test.module.less

README.md

+34-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ for [CSS Modules](https://github.com/css-modules/css-modules).
1212
This project was inspired by this [`create-react-app` issue](https://github.com/facebook/create-react-app/issues/5677)
1313
and was based on [`css-module-types`](https://github.com/timothykang/css-module-types).
1414

15-
## Usage
15+
## Installation
1616

1717
To install with Yarn:
1818

@@ -36,12 +36,32 @@ Once installed, add this plugin to your `tsconfig.json`:
3636
}
3737
```
3838

39+
### Importing CSS
40+
41+
A default export is always provided for your CSS module.
42+
43+
```tsx
44+
import styles from 'my.module.css';
45+
46+
const a = styles.myClass;
47+
const b = styles['my_other-class'];
48+
```
49+
50+
As of version 1.1.0, you can also use named exports for classes that don't contain hyphens or underscores. You can still access other classes via the default export.
51+
52+
```tsx
53+
import styles, { myClass } from 'my.module.css';
54+
55+
const a = myClass;
56+
const b = styles['my_other-class'];
57+
```
58+
3959
### Options
4060

41-
| Option | Default value | Description |
42-
| --------------- | ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
43-
| `customMatcher` | `"\\.module\\.(sa\|sc\|c)ss$"` | Change the file extensions that this plugin works with. |
44-
| `camelCase` | `false` | Implements the behaviour of the [`camelCase` CSS Loader option](https://github.com/webpack-contrib/css-loader#camelcase) (accepting the same values). |
61+
| Option | Default value | Description |
62+
| --------------- | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
63+
| `customMatcher` | `"\\.module\\.(c\|le\|sa\|sc)ss$"` | Change the file extensions that this plugin works with. |
64+
| `camelCase` | `false` | Implements the behaviour of the [`camelCase` CSS Loader option](https://github.com/webpack-contrib/css-loader#camelcase) (accepting the same values). |
4565

4666
The below is an example that only matches "\*.m.css" files, and [camel-cases dashes](https://github.com/webpack-contrib/css-loader#camelcase).
4767

@@ -65,17 +85,16 @@ The below is an example that only matches "\*.m.css" files, and [camel-cases das
6585

6686
By default, VSCode will use it's own version of TypeScript. To make it work with this plugin, you have two options:
6787

68-
1. Add this plugin to `"typescript.tsserver.pluginPaths"` in settings. Note that this method doesn't currently support
69-
plugin options. This is planned for the [November update](https://github.com/Microsoft/vscode/issues/62876).
88+
1. Use your workspace's version of TypeScript, which will load plugins from your `tsconfig.json` file. This is the recommended approach. For instructions, see: [Using the workspace version of TypeScript](https://code.visualstudio.com/docs/languages/typescript#_using-the-workspace-version-of-typescript).
89+
90+
2. Add this plugin to `"typescript.tsserver.pluginPaths"` in settings. Note that this method doesn't currently support plugin options.
7091

7192
```json
7293
{
7394
"typescript.tsserver.pluginPaths": ["typescript-plugin-css-modules"]
7495
}
7596
```
7697

77-
2. Use your workspace's version of TypeScript, which will load the plugins from your `tsconfig.json` file. For instructions, see: [Using the workspace version of TypeScript](https://code.visualstudio.com/docs/languages/typescript#_using-the-workspace-version-of-typescript).
78-
7998
### Custom definitions
8099

81100
_Note: Create React App users can skip this section if you're using `[email protected]` or higher._
@@ -84,7 +103,7 @@ If your project doesn't already have global declarations for CSS Modules, you wi
84103

85104
Where you store global declarations is up to you. An example might look like: `src/custom.d.ts`.
86105

87-
The below is an example that you can copy, or modify if you use a `customMatcher`.
106+
The below is an example that you can copy or modify. If you use a `customMatcher`, you'll need to modify it.
88107

89108
```ts
90109
declare module '*.module.css' {
@@ -101,4 +120,9 @@ declare module '*.module.sass' {
101120
const classes: { [key: string]: string };
102121
export default classes;
103122
}
123+
124+
declare module '*.module.less' {
125+
const classes: { [key: string]: string };
126+
export default classes;
127+
}
104128
```

package.json

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-plugin-css-modules",
3-
"version": "1.0.5",
3+
"version": "1.1.0",
44
"main": "lib/index.js",
55
"author": "Brody McKee <[email protected]>",
66
"license": "MIT",
@@ -12,15 +12,20 @@
1212
},
1313
"keywords": [
1414
"css",
15+
"scss",
16+
"sass",
17+
"less",
1518
"modules",
1619
"plugin",
1720
"postcss",
18-
"sass",
1921
"typescript"
2022
],
23+
"files": [
24+
"lib"
25+
],
2126
"scripts": {
2227
"build": "rm -rf ./lib && tsc",
23-
"prepublish": "yarn build",
28+
"prepublishOnly": "yarn build",
2429
"test": "jest ./src"
2530
},
2631
"husky": {
@@ -42,24 +47,27 @@
4247
"trailingComma": "all"
4348
},
4449
"dependencies": {
45-
"icss-utils": "^4.0.0",
50+
"icss-utils": "^4.1.0",
51+
"less": "^3.9.0",
4652
"lodash": "^4.17.11",
47-
"postcss": "^7.0.7",
53+
"postcss": "^7.0.16",
4854
"postcss-icss-selectors": "^2.0.3",
49-
"postcss-nested": "^4.1.1",
50-
"strip-css-singleline-comments": "^1.1.0"
55+
"reserved-words": "^0.1.2",
56+
"sass": "^1.20.1"
5157
},
5258
"devDependencies": {
53-
"@types/jest": "^23.3.12",
54-
"@types/lodash": "^4.14.119",
55-
"@types/node": "^10.12.18",
56-
"husky": "^1.3.1",
57-
"jest": "^23.6.0",
58-
"prettier": "^1.15.3",
59-
"pretty-quick": "^1.8.0",
60-
"ts-jest": "^23.10.5",
61-
"tslint": "^5.12.0",
62-
"typescript": "^3.2.2"
59+
"@types/jest": "^24.0.13",
60+
"@types/less": "^3.0.0",
61+
"@types/lodash": "^4.14.132",
62+
"@types/node": "^10.0.0",
63+
"@types/sass": "^1.16.0",
64+
"husky": "^2.3.0",
65+
"jest": "^24.8.0",
66+
"prettier": "^1.17.1",
67+
"pretty-quick": "^1.11.0",
68+
"ts-jest": "^24.0.2",
69+
"tslint": "^5.16.0",
70+
"typescript": "^3.4.5"
6371
},
6472
"peerDependencies": {
6573
"typescript": "^3.0.0"

src/@types/reserved-words.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'reserved-words' {
2+
export const check: (string: string, esVersion?: string) => boolean;
3+
}

src/helpers/__tests__/__snapshots__/cssSnapshots.test.ts.snap

+81-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`utils / cssSnapshots with file 'empty.module.less' createExports should create an exports file 1`] = `
4+
"declare const classes: {
5+
6+
};
7+
export default classes;
8+
"
9+
`;
10+
11+
exports[`utils / cssSnapshots with file 'empty.module.less' getClasses should return an object matching expected CSS 1`] = `Object {}`;
12+
313
exports[`utils / cssSnapshots with file 'empty.module.scss' createExports should create an exports file 1`] = `
414
"declare const classes: {
515
@@ -15,12 +25,19 @@ exports[`utils / cssSnapshots with file 'test.module.css' createExports should c
1525
'classA': string;
1626
'ClassB': string;
1727
'class-c': string;
28+
'class_d': string;
1829
'parent': string;
1930
'childA': string;
2031
'childB': string;
2132
'nestedChild': string;
2233
};
2334
export default classes;
35+
export const classA: string;
36+
export const ClassB: string;
37+
export const parent: string;
38+
export const childA: string;
39+
export const childB: string;
40+
export const nestedChild: string;
2441
"
2542
`;
2643

@@ -31,33 +48,90 @@ Object {
3148
"childB": "file__childB---pq4Ks",
3249
"class-c": "file__class-c---DZ1TD",
3350
"classA": "file__classA---2xcnJ",
51+
"class_d": "file__class_d---1mwNi",
3452
"nestedChild": "file__nestedChild---2d15b",
3553
"parent": "file__parent---1ATMj",
3654
}
3755
`;
3856

57+
exports[`utils / cssSnapshots with file 'test.module.less' createExports should create an exports file 1`] = `
58+
"declare const classes: {
59+
'nested-class-parent': string;
60+
'child-class': string;
61+
'selector-blue': string;
62+
'selector-green': string;
63+
'selector-red': string;
64+
'column-1': string;
65+
'column-2': string;
66+
'column-3': string;
67+
'column-4': string;
68+
'color-set': string;
69+
};
70+
export default classes;
71+
"
72+
`;
73+
74+
exports[`utils / cssSnapshots with file 'test.module.less' getClasses should return an object matching expected CSS 1`] = `
75+
Object {
76+
"child-class": "file__child-class---1mwoB",
77+
"color-set": "file__color-set---9sHH_",
78+
"column-1": "file__column-1---vHRb_",
79+
"column-2": "file__column-2---28y1r",
80+
"column-3": "file__column-3---1PsZw",
81+
"column-4": "file__column-4---2qaaI",
82+
"nested-class-parent": "file__nested-class-parent---_ft7G",
83+
"selector-blue": "file__selector-blue---3mslq",
84+
"selector-green": "file__selector-green---143xX",
85+
"selector-red": "file__selector-red---Gckob",
86+
}
87+
`;
88+
3989
exports[`utils / cssSnapshots with file 'test.module.scss' createExports should create an exports file 1`] = `
4090
"declare const classes: {
4191
'local-class-inside-global': string;
4292
'local-class': string;
4393
'local-class-2': string;
4494
'local-class-inside-local': string;
95+
'reserved-words': string;
96+
'default': string;
97+
'const': string;
4598
'nested-class-parent': string;
4699
'child-class': string;
47100
'nested-class-parent--extended': string;
101+
'section-1': string;
102+
'section-2': string;
103+
'section-3': string;
104+
'section-4': string;
105+
'section-5': string;
106+
'section-6': string;
107+
'section-7': string;
108+
'section-8': string;
109+
'section-9': string;
48110
};
49111
export default classes;
50112
"
51113
`;
52114

53115
exports[`utils / cssSnapshots with file 'test.module.scss' getClasses should return an object matching expected CSS 1`] = `
54116
Object {
55-
"child-class": "file__child-class---1mwoB",
56-
"local-class": "file__local-class---3KegX",
57-
"local-class-2": "file__local-class-2---2h6qz",
58-
"local-class-inside-global": "file__local-class-inside-global---2xH_Y",
59-
"local-class-inside-local": "file__local-class-inside-local---QdL6b",
60-
"nested-class-parent": "file__nested-class-parent---_ft7G",
61-
"nested-class-parent--extended": "file__nested-class-parent--extended---1642l",
117+
"child-class": "file__child-class---1QWYM",
118+
"const": "file__const---MIe_0",
119+
"default": "file__default---2RWlj",
120+
"local-class": "file__local-class---3SW3k",
121+
"local-class-2": "file__local-class-2----c5z7",
122+
"local-class-inside-global": "file__local-class-inside-global---1T0um",
123+
"local-class-inside-local": "file__local-class-inside-local---1Z9pB",
124+
"nested-class-parent": "file__nested-class-parent---3qXdF",
125+
"nested-class-parent--extended": "file__nested-class-parent--extended---qsVau",
126+
"reserved-words": "file__reserved-words---_rrID",
127+
"section-1": "file__section-1---1IHCS",
128+
"section-2": "file__section-2---cLFhf",
129+
"section-3": "file__section-3---1ldKa",
130+
"section-4": "file__section-4---2u0CG",
131+
"section-5": "file__section-5---1lAYL",
132+
"section-6": "file__section-6---2YZ9I",
133+
"section-7": "file__section-7---3w-OF",
134+
"section-8": "file__section-8---3RB8g",
135+
"section-9": "file__section-9---3_Mtj",
62136
}
63137
`;

src/helpers/__tests__/classTransforms.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { transformClasses } from '../classTransforms';
2+
import { CamelCaseOptions } from '../../options';
23

34
describe('utils / classTransforms', () => {
45
const classNames = [

src/helpers/__tests__/createMatchers.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { createMatchers } from '../createMatchers';
2+
import { Options } from '../../options';
23

34
describe('utils / createMatchers', () => {
45
it('should match `customMatcher` regexp', () => {
5-
const options: IOptions = { customMatcher: '\\.css$' };
6+
const options: Options = { customMatcher: '\\.css$' };
67
const { isCSS, isRelativeCSS } = createMatchers(options);
78

89
expect(isCSS('./myfile.css')).toBe(true);
@@ -14,7 +15,7 @@ describe('utils / createMatchers', () => {
1415
});
1516

1617
it('should handle bad `customMatcher` regexp', () => {
17-
const options: IOptions = { customMatcher: '$([a' };
18+
const options: Options = { customMatcher: '$([a' };
1819
const { isCSS, isRelativeCSS } = createMatchers(options);
1920

2021
expect(isCSS('./myfile.module.css')).toBe(true);

src/helpers/__tests__/cssSnapshots.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
import { readFileSync } from 'fs';
22
import { IICSSExports } from 'icss-utils';
33
import { join } from 'path';
4-
import { createExports, getClasses } from '../cssSnapshots';
4+
import { createExports, getClasses, FileTypes } from '../cssSnapshots';
55

66
const testFileNames = [
77
'test.module.css',
8+
'test.module.less',
89
'test.module.scss',
10+
'empty.module.less',
911
'empty.module.scss',
1012
];
1113

1214
describe('utils / cssSnapshots', () => {
13-
testFileNames.map((filename) => {
15+
testFileNames.forEach((filename) => {
1416
let classes: IICSSExports;
17+
const isLess = filename.includes('less');
18+
const fileType = isLess ? FileTypes.less : FileTypes.css;
1519
const testFile = readFileSync(
1620
join(__dirname, 'fixtures', filename),
1721
'utf8',
1822
);
1923

2024
beforeAll(() => {
21-
classes = getClasses(testFile);
25+
classes = getClasses(testFile, fileType);
2226
});
2327

2428
describe(`with file '${filename}'`, () => {

src/helpers/__tests__/fixtures/empty.module.less

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
.classA {
2+
color: rebeccapurple;
23
}
34

45
.ClassB {
6+
color: rebeccapurple;
57
}
68

79
.class-c {
10+
color: rebeccapurple;
11+
}
12+
13+
.class_d {
14+
color: rebeccapurple;
815
}
916

1017
.parent {
1118
.childA {
19+
color: rebeccapurple;
1220
}
1321
.childB {
1422
.nestedChild {
23+
color: rebeccapurple;
1524
}
1625
}
1726
}

0 commit comments

Comments
 (0)