Skip to content

Commit ed1f56c

Browse files
committed
Refactor options and upgrade dependencies
1 parent f65594a commit ed1f56c

File tree

11 files changed

+1303
-1599
lines changed

11 files changed

+1303
-1599
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ const b = styles['my_other-class'];
6969

7070
Please note that no options are required. However, depending on your configuration, you may need to customise these options.
7171

72-
| Option | Default value | Description |
73-
| ----------------------------- | ---------------------------------- | ---------------------------------------------------------------------------- |
74-
| `classnameTransform` | `asIs` | See [`classnameTransform`](#classnameTransform) below. |
75-
| `customMatcher` | `"\\.module\\.(c\|le\|sa\|sc)ss$"` | Changes the file extensions that this plugin processes. |
76-
| `customRenderer` | `false` | See [`customRenderer`](#customRenderer) below. |
77-
| `customTypescriptTransformer` | `false` | See [`customTypescriptTransformer`](#customTypescriptTransformer) below. |
78-
| `dotenvOptions` | `{}` | Provides options for [`dotenv`](https://github.com/motdotla/dotenv#options). |
79-
| `postCssOptions` | `{}` | See [`postCssOptions`](#postCssOptions) below. |
80-
| `rendererOptions` | `{}` | See [`rendererOptions`](#rendererOptions) below. |
72+
| Option | Default value | Description |
73+
| -------------------- | ---------------------------------- | ---------------------------------------------------------------------------- |
74+
| `classnameTransform` | `asIs` | See [`classnameTransform`](#classnameTransform) below. |
75+
| `customMatcher` | `"\\.module\\.(c\|le\|sa\|sc)ss$"` | Changes the file extensions that this plugin processes. |
76+
| `customRenderer` | `false` | See [`customRenderer`](#customRenderer) below. |
77+
| `customTemplate` | `false` | See [`customTemplate`](#customTemplate) below. |
78+
| `dotenvOptions` | `{}` | Provides options for [`dotenv`](https://github.com/motdotla/dotenv#options). |
79+
| `postCssOptions` | `{}` | See [`postCssOptions`](#postCssOptions) below. |
80+
| `rendererOptions` | `{}` | See [`rendererOptions`](#rendererOptions) below. |
8181

8282
```json
8383
{
@@ -118,7 +118,7 @@ The custom renderer itself should be a JavaScript file. The function will be cal
118118
```js
119119
module.exports = (css, { fileName, logger }) => {
120120
try {
121-
// ...process css here
121+
// ...process css here.s
122122
return renderedCss;
123123
} catch (error) {
124124
logger.error(error.message);
@@ -130,32 +130,32 @@ You can find an example custom renderer in our test fixtures ([`customRenderer.j
130130

131131
The [internal `logger`](https://github.com/mrmckeb/typescript-plugin-css-modules/blob/master/src/helpers/logger.ts) is provided for [debugging](#troubleshooting).
132132

133-
#### `customTypescriptTransformer`
133+
#### `customTemplate`
134134

135-
The `customTypescriptTransformer` is an advanced option, letting you provide a transformer of the generated typescript declarations.
135+
The `customTemplate` is an advanced option, letting you provide a template for the generated TypeScript declarations.
136136

137-
When a custom typescript transformer is provided, its output is used as the virtual typescript file.
137+
When a custom template is provided, its output is used as the virtual declaration (`*.d.ts`) file.
138138

139-
The path to the `customTypescriptTransformer` must be relative to the project root (i.e. `./myTypescriptTransformer.js`).
139+
The path to the `customTemplate` must be relative to the project root (i.e. `./customTemplate.js`).
140140

141141
The custom renderer itself should be a JavaScript file. The function will be called with two arguments: a `dts` string, and an `options` object (see [`options.ts`](https://github.com/mrmckeb/typescript-plugin-css-modules/blob/master/src/options.ts#L43-L52)). It must be synchronous, and must return valid TypeScript Declaration code (code found in a .d.ts file only).
142142

143143
```js
144144
module.exports = (dts, { classes, fileName, logger }) => {
145145
try {
146-
// ...transform the dts here
147-
return transformedDts;
146+
// ...create your template here.
147+
return customTemplate;
148148
} catch (error) {
149149
logger.error(error.message);
150150
}
151151
};
152152
```
153153

154-
You can find an example custom typescript transformer in our test fixtures ([`customTypescriptTransformer.js`](https://github.com/mrmckeb/typescript-plugin-css-modules/blob/master/src/helpers/__tests__/fixtures/customTypescriptTransformer.js)).
154+
You can find an example custom template in our test fixtures ([`customTemplate.js`](https://github.com/mrmckeb/typescript-plugin-css-modules/blob/master/src/helpers/__tests__/fixtures/customTemplate.js)).
155155

156156
The [internal `logger`](https://github.com/mrmckeb/typescript-plugin-css-modules/blob/master/src/helpers/logger.ts) is provided for [debugging](#troubleshooting).
157157

158-
The `classes` object represents all the classnames extracted form the CSS Module. They are available if you want to add a custom representation of the CSS classes.
158+
The `classes` object represents all the classnames extracted from the CSS Module. They are available if you want to add a custom representation of the CSS classes.
159159

160160
#### `postCssOptions`
161161

@@ -199,7 +199,7 @@ If your project doesn't already have global declarations for CSS Modules, you wi
199199

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

202-
The below is an example that you can copy or modify. If you use a [`customMatcher`], you'll need to modify this.
202+
The below is an example that you can copy or modify. If you use a `customMatcher`, you'll need to modify this.
203203

204204
```ts
205205
declare module '*.module.css' {

package.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
],
2626
"scripts": {
2727
"build": "rm -rf ./lib && tsc",
28-
"lint": "eslint './src/**/*.ts' --max-warnings 0 && yarn prettier './**/*.{json,md,ts,yml}' -c",
28+
"lint": "eslint './src/**/*.{js,ts}' --max-warnings 0 && yarn prettier './**/*.{json,md,js,ts,yml}' -c",
2929
"prepublishOnly": "yarn build",
3030
"test": "jest ./src"
3131
},
@@ -64,42 +64,42 @@
6464
"trailingComma": "all"
6565
},
6666
"resolutions": {
67-
"postcss": "7.0.23"
67+
"postcss": "7.0.27"
6868
},
6969
"dependencies": {
7070
"dotenv": "^8.2.0",
7171
"icss-utils": "^4.1.1",
72-
"less": "^3.9.0",
72+
"less": "^3.11.1",
7373
"lodash.camelcase": "^4.3.0",
74-
"postcss": "^7.0.23",
74+
"postcss": "^7.0.27",
7575
"postcss-filter-plugins": "^3.0.1",
7676
"postcss-icss-selectors": "^2.0.3",
7777
"postcss-load-config": "^2.1.0",
7878
"reserved-words": "^0.1.2",
79-
"sass": "^1.23.7"
79+
"sass": "^1.26.1"
8080
},
8181
"devDependencies": {
8282
"@types/icss-utils": "^4.1.0",
83-
"@types/jest": "^24.0.23",
83+
"@types/jest": "^25.1.3",
8484
"@types/less": "^3.0.1",
8585
"@types/lodash.camelcase": "^4.3.6",
8686
"@types/node": "^12.12.17",
8787
"@types/postcss-load-config": "^2.0.1",
8888
"@types/postcss-nested": "^4.1.0",
8989
"@types/reserved-words": "^0.1.0",
9090
"@types/sass": "^1.16.0",
91-
"@typescript-eslint/eslint-plugin": "^2.11.0",
92-
"@typescript-eslint/parser": "^2.11.0",
91+
"@typescript-eslint/eslint-plugin": "^2.21.0",
92+
"@typescript-eslint/parser": "^2.21.0",
9393
"bootstrap": "4.4.1",
94-
"eslint": "^6.7.2",
95-
"eslint-config-prettier": "^6.7.0",
96-
"husky": "^3.1.0",
97-
"jest": "^24.8.0",
98-
"lint-staged": "^9.5.0",
94+
"eslint": "^6.8.0",
95+
"eslint-config-prettier": "^6.10.0",
96+
"husky": "^4.2.3",
97+
"jest": "^25.1.0",
98+
"lint-staged": "^10.0.8",
9999
"postcss-import-sync2": "^1.1.0",
100100
"prettier": "^1.19.1",
101-
"ts-jest": "^24.2.0",
102-
"typescript": "^3.7.3"
101+
"ts-jest": "^25.2.1",
102+
"typescript": "^3.8.2"
103103
},
104104
"peerDependencies": {
105105
"typescript": "^3.0.0"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = (dts, { classes, logger }) => {
2+
logger.log('Example log');
3+
return [
4+
'/* eslint-disable */',
5+
dts,
6+
'export const __cssModule: true;',
7+
`export type AllClassNames = '${Object.keys(classes).join("' | '")}';`,
8+
].join('\n');
9+
};

src/helpers/__tests__/fixtures/customTypescriptTransformer.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/helpers/__tests__/getDtsSnapshot.test.ts

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,35 @@ const testFileNames = [
2121
'import.module.less',
2222
];
2323

24-
const mockLogger: Logger = {
24+
const logger: Logger = {
2525
log: jest.fn(),
2626
error: jest.fn(),
2727
};
2828

29-
const mockOptions: Options = {};
29+
const options: Options = {};
3030

3131
const processor = postcss([
3232
postcssImportSync(),
3333
postcssIcssSelectors({ mode: 'local' }),
3434
]);
3535

3636
describe('utils / cssSnapshots', () => {
37-
testFileNames.forEach((fileName) => {
37+
testFileNames.forEach((testFile) => {
3838
let classes: CSSExports;
39-
const fullFileName = join(__dirname, 'fixtures', fileName);
40-
const testFile = readFileSync(fullFileName, 'utf8');
39+
const fileName = join(__dirname, 'fixtures', testFile);
40+
const css = readFileSync(fileName, 'utf8');
4141

4242
beforeAll(() => {
43-
classes = getClasses(
43+
classes = getClasses({
44+
css,
45+
fileName,
46+
logger,
47+
options,
4448
processor,
45-
testFile,
46-
fullFileName,
47-
mockOptions,
48-
mockLogger,
49-
);
49+
});
5050
});
5151

52-
describe(`with file '${fileName}'`, () => {
52+
describe(`with file '${testFile}'`, () => {
5353
describe('getClasses', () => {
5454
it('should return an object matching expected CSS', () => {
5555
expect(classes).toMatchSnapshot();
@@ -58,88 +58,94 @@ describe('utils / cssSnapshots', () => {
5858

5959
describe('createExports', () => {
6060
it('should create an exports file', () => {
61-
const exports = createExports(classes, {}, fullFileName, mockLogger);
61+
const exports = createExports({
62+
classes,
63+
fileName,
64+
logger,
65+
options: {},
66+
});
6267
expect(exports).toMatchSnapshot();
6368
});
6469
});
6570

6671
describe('with a custom typescript transformer', () => {
6772
it('should transform the generated dts', () => {
68-
const customTypescriptTransformer = join(
73+
const customTemplate = join(
6974
__dirname,
7075
'fixtures',
71-
'customTypescriptTransformer.js',
76+
'customTemplate.js',
7277
);
73-
const overrideOptions: Options = { customTypescriptTransformer };
7478

75-
const dts = createExports(
79+
const options: Options = { customTemplate };
80+
81+
const dts = createExports({
7682
classes,
77-
overrideOptions,
78-
fullFileName,
79-
mockLogger,
80-
);
83+
fileName,
84+
logger,
85+
options,
86+
});
8187
expect(dts).toMatchSnapshot();
8288
});
8389
});
8490
});
8591
});
8692

8793
describe('with a Bootstrap import', () => {
88-
const fullFileName = join(__dirname, 'fixtures', 'bootstrap.module.scss');
89-
const testFile = readFileSync(fullFileName, 'utf8');
94+
const fileName = join(__dirname, 'fixtures', 'bootstrap.module.scss');
95+
const css = readFileSync(fileName, 'utf8');
9096

9197
it('should find external files', () => {
92-
const classes = getClasses(
98+
const classes = getClasses({
99+
css,
100+
fileName,
101+
logger,
102+
options,
93103
processor,
94-
testFile,
95-
fullFileName,
96-
mockOptions,
97-
mockLogger,
98-
);
104+
});
99105

100106
expect(classes.test).toMatchSnapshot();
101107
});
102108
});
103109

104110
describe('with a custom renderer', () => {
105-
const fullFileName = 'exampleFileContents';
106-
const testFile = 'exampleFileName';
111+
const fileName = 'exampleFileContents';
112+
const css = 'exampleFileName';
107113
const customRenderer = join(__dirname, 'fixtures', 'customRenderer.js');
108114

115+
const options: Options = { customRenderer };
116+
109117
it('should process a file and log', () => {
110-
const classes = getClasses(
118+
const classes = getClasses({
119+
css,
120+
fileName,
121+
logger,
122+
options,
111123
processor,
112-
testFile,
113-
fullFileName,
114-
{ customRenderer },
115-
mockLogger,
116-
);
124+
});
117125

118126
expect(classes).toMatchSnapshot();
119-
expect(mockLogger.log).toHaveBeenCalledWith('Example log');
127+
expect(logger.log).toHaveBeenCalledWith('Example log');
120128
});
121129
});
122130

123131
describe('with includePaths in sass options', () => {
124-
const fullFileName = join(
125-
__dirname,
126-
'fixtures',
127-
'include-path.module.scss',
128-
);
129-
const testFile = readFileSync(fullFileName, 'utf8');
132+
const fileName = join(__dirname, 'fixtures', 'include-path.module.scss');
133+
const css = readFileSync(fileName, 'utf8');
134+
135+
const options: Options = {
136+
rendererOptions: {
137+
sass: { includePaths: [join(__dirname, 'external')] },
138+
},
139+
};
130140

131141
it('should find external file from includePaths', () => {
132-
const classes = getClasses(
142+
const classes = getClasses({
143+
css,
144+
fileName,
145+
logger,
146+
options,
133147
processor,
134-
testFile,
135-
fullFileName,
136-
{
137-
rendererOptions: {
138-
sass: { includePaths: [join(__dirname, 'external')] },
139-
},
140-
},
141-
mockLogger,
142-
);
148+
});
143149

144150
expect(classes).toMatchSnapshot();
145151
});

0 commit comments

Comments
 (0)