Skip to content

Commit bd61703

Browse files
committed
Merge branch 'release/2.0.0'
2 parents c804817 + 3ce5419 commit bd61703

35 files changed

+1836
-735
lines changed

.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": [
3+
"plugin:@typescript-eslint/recommended",
4+
"prettier",
5+
"prettier/@typescript-eslint"
6+
],
7+
"root": true,
8+
"rules": {
9+
"@typescript-eslint/explicit-function-return-type": 0
10+
}
11+
}

.github/workflows/ci.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ jobs:
1010
- uses: actions/setup-node@v1
1111
with:
1212
node-version: 10.x
13-
- name: Yarn install, build, and test
14-
run: |
15-
yarn install
16-
yarn build
17-
yarn test
13+
- name: Install
14+
run: yarn install --frozen-lockfile
15+
- name: Lint
16+
run: yarn lint
17+
- name: Test
18+
run: yarn test
19+
- name: Build
20+
run: yarn build

.prettierignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
src/helpers/__tests__/fixtures/test.module.less
1+
src/helpers/__tests__/fixtures/*.less
2+
src/helpers/__tests__/fixtures/*.css
3+
src/helpers/__tests__/fixtures/*.scss
4+
/lib/

README.md

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@ for [CSS Modules](https://github.com/css-modules/css-modules).
99

1010
<img src="https://raw.githubusercontent.com/mrmckeb/typescript-plugin-css-modules/master/.github/images/example.gif" alt="typescript-plugin-css-modules example" />
1111

12-
This project was inspired by this [`create-react-app` issue](https://github.com/facebook/create-react-app/issues/5677)
13-
and was based on [`css-module-types`](https://github.com/timothykang/css-module-types).
12+
## Table of contents
13+
14+
- [Installation](#installation)
15+
- [Importing CSS](#importing-css)
16+
- [Options](#options)
17+
- [Visual Studio Code](#visual-studio-code)
18+
- [Custom definitions](#custom-definitions)
19+
- [Troubleshooting](#troubleshooting)
20+
- [About this project](#about-this-project)
1421

1522
## Installation
1623

1724
To install with Yarn:
1825

1926
```sh
20-
yarn add typescript-plugin-css-modules
27+
yarn add -D typescript-plugin-css-modules
2128
```
2229

2330
To install with npm:
2431

2532
```sh
26-
npm install --save typescript-plugin-css-modules
33+
npm install -D typescript-plugin-css-modules
2734
```
2835

2936
Once installed, add this plugin to your `tsconfig.json`:
@@ -36,6 +43,8 @@ Once installed, add this plugin to your `tsconfig.json`:
3643
}
3744
```
3845

46+
If you're using Visual Studio Code, please also follow these [instructions](#visual-studio-code).
47+
3948
### Importing CSS
4049

4150
A default export is always provided for your CSS module.
@@ -58,12 +67,16 @@ const b = styles['my_other-class'];
5867

5968
### Options
6069

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). |
70+
Please note that no options are required. However, depending on your configuration, you may need to customise these options.
6571

66-
The below is an example that only matches "\*.m.css" files, and [camel-cases dashes](https://github.com/webpack-contrib/css-loader#camelcase).
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+
| `dotenvOptions` | `{}` | Provides options for [`dotenv`](https://github.com/motdotla/dotenv#options). |
78+
| `postCssOptions` | `{}` | See [`postCssOptions`](#postCssOptions) below. |
79+
| `rendererOptions` | `{}` | See [`rendererOptions`](#rendererOptions) below. |
6780

6881
```json
6982
{
@@ -72,22 +85,75 @@ The below is an example that only matches "\*.m.css" files, and [camel-cases das
7285
{
7386
"name": "typescript-plugin-css-modules",
7487
"options": {
88+
"classnameTransform": "dashes",
7589
"customMatcher": "\\.m\\.css$",
76-
"camelCase": "dashes"
90+
"customRenderer": "./myRenderer.js",
91+
"dotenvOptions": {},
92+
"postCssOptions": {},
93+
"rendererOptions": {}
7794
}
7895
}
7996
]
8097
}
8198
}
8299
```
83100

101+
#### `classnameTransform`
102+
103+
Implements the behaviour of the [`localsConvention`](https://github.com/webpack-contrib/css-loader#localsconvention) `css-loader` option.
104+
105+
Options available are: `'asIs'`, `'camelCase'`, `'camelCaseOnly'`, `'dashes'`, and `'dashesOnly'`.
106+
107+
#### `customRenderer`
108+
109+
The `customRenderer` is an advanced option, letting you provide the CSS renderer.
110+
111+
When a custom renderer is provided, not other renderers will be used.
112+
113+
The path to the `customRenderer` must be relative to the project root (i.e. `./myRenderer.js`).
114+
115+
The custom renderer itself should be a JavaScript file. The function will be called with two arguments: a `css` string, and an `options` object (see [`options.ts`](https://github.com/mrmckeb/typescript-plugin-css-modules/blob/master/src/options.ts#L36-L39)). It must be synchronous, and must return valid CSS.
116+
117+
```js
118+
module.exports = (css, { fileName, logger }) => {
119+
try {
120+
// ...process css here
121+
return renderedCss;
122+
} catch (error) {
123+
logger.error(error.message);
124+
}
125+
};
126+
```
127+
128+
You can find an example custom renderer in our test fixtures ([`customRenderer.js`](https://github.com/mrmckeb/typescript-plugin-css-modules/blob/master/src/helpers/__tests__/fixtures/customRenderer.js)).
129+
130+
The [internal `logger`](https://github.com/mrmckeb/typescript-plugin-css-modules/blob/master/src/helpers/logger.ts) is provided for [debugging](#troubleshooting).
131+
132+
#### `postCssOptions`
133+
134+
| Option | Default value | Description |
135+
| ---------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------- |
136+
| `useConfig` | `false` | Set to `true` to load plugins from your [PostCSS config](https://github.com/michael-ciniawsky/postcss-load-config#usage). |
137+
| `excludePlugins` | `false` | Only sync plugins are supported. Use this to set an array of async plugins to exclude (i.e. `['postcss-mixins']`) |
138+
139+
#### `rendererOptions`
140+
141+
| Option | Default value | Description |
142+
| ------ | ------------- | ------------------------------------------------------------------------------------ |
143+
| `less` | `{}` | Set [renderer options for Less](http://lesscss.org/usage/#less-options). |
144+
| `sass` | `{}` | Set [renderer options for Sass](https://sass-lang.com/documentation/js-api#options). |
145+
84146
### Visual Studio Code
85147

86-
By default, VSCode will use its own version of TypeScript. To make it work with this plugin, you have two options:
148+
#### Recommended usage
149+
150+
To use this plugin with Visual Studio Code, you should set your workspace's version of TypeScript, which will load plugins from your `tsconfig.json` file.
151+
152+
For instructions, see: [Using the workspace version of TypeScript](https://code.visualstudio.com/docs/languages/typescript#_using-the-workspace-version-of-typescript).
87153

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).
154+
#### Alternative usage
89155

90-
2. Add this plugin to `"typescript.tsserver.pluginPaths"` in settings. Note that this method doesn't currently support plugin options.
156+
If you aren't using any [plugin options](#options), you can simple add this plugin to `"typescript.tsserver.pluginPaths"` in settings. You cannot provide plugin options with this approach.
91157

92158
```json
93159
{
@@ -101,9 +167,9 @@ _Note: Create React App users can skip this section if you're using `react-scrip
101167

102168
If your project doesn't already have global declarations for CSS Modules, you will need to add these to help TypeScript understand the general shape of the imported CSS during build.
103169

104-
Where you store global declarations is up to you. An example might look like: `src/custom.d.ts`.
170+
Where you store global declarations is up to you. An example might look like: `./src/custom.d.ts`.
105171

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

108174
```ts
109175
declare module '*.module.css' {
@@ -129,8 +195,13 @@ declare module '*.module.less' {
129195

130196
## Troubleshooting
131197

132-
If you're having issues with this extension, you can view the TypeScript Server Log in VSCode by entering `Typescript: Open TS Server log` in the [command palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette).
198+
For troubleshooting and debugging, you can view the TypeScript Server Log in Visual Studio Code by entering `Typescript: Open TS Server log` in the [command palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette).
199+
200+
If you're not using Visual Studio Code or are having trouble with the above method, you can set the [`TSS_LOG` environment variable](https://github.com/Microsoft/TypeScript/wiki/Standalone-Server-%28tsserver%29#logging).
201+
202+
You can include these logs with any issues you open for this project.
133203

134-
If that doesn't work, or you're not using VSCode, you can set the [TSS_LOG environment variable](https://github.com/Microsoft/TypeScript/wiki/Standalone-Server-%28tsserver%29#logging).
204+
## About this project
135205

136-
You can also include this with any issues you file on this project.
206+
This project was inspired by a Create React App [issue](https://github.com/facebook/create-react-app/issues/5677)
207+
and built on prior work from [`css-module-types`](https://github.com/timothykang/css-module-types).

package.json

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-plugin-css-modules",
3-
"version": "1.3.0",
3+
"version": "2.0.0",
44
"main": "lib/index.js",
55
"author": "Brody McKee <[email protected]>",
66
"license": "MIT",
@@ -25,12 +25,13 @@
2525
],
2626
"scripts": {
2727
"build": "rm -rf ./lib && tsc",
28+
"lint": "eslint './src/**/*.ts' --max-warnings 0 && yarn prettier './**/*.{json,md,ts,yml}' -c",
2829
"prepublishOnly": "yarn build",
2930
"test": "jest ./src"
3031
},
3132
"husky": {
3233
"hooks": {
33-
"pre-commit": "pretty-quick --staged",
34+
"pre-commit": "lint-staged",
3435
"pre-push": "yarn test"
3536
}
3637
},
@@ -39,36 +40,61 @@
3940
"src/**/*.{ts}"
4041
],
4142
"preset": "ts-jest",
42-
"testEnvironment": "node"
43+
"testEnvironment": "node",
44+
"testPathIgnorePatterns": [
45+
"/node_modules/",
46+
"/fixtures/"
47+
]
48+
},
49+
"lint-staged": {
50+
"./src/**/*.ts": [
51+
"eslint --fix",
52+
"prettier --write",
53+
"git add"
54+
],
55+
"./**/*.{json,md,yml}": [
56+
"prettier --write",
57+
"git add"
58+
]
4359
},
4460
"prettier": {
4561
"arrowParens": "always",
4662
"singleQuote": true,
4763
"trailingComma": "all"
4864
},
4965
"dependencies": {
66+
"dotenv": "^8.2.0",
5067
"icss-utils": "^4.1.1",
5168
"less": "^3.9.0",
52-
"lodash": "^4.17.14",
69+
"lodash.camelcase": "^4.3.0",
5370
"postcss": "^7.0.17",
71+
"postcss-filter-plugins": "^3.0.1",
5472
"postcss-icss-selectors": "^2.0.3",
5573
"postcss-load-config": "^2.1.0",
5674
"reserved-words": "^0.1.2",
5775
"sass": "^1.22.4"
5876
},
5977
"devDependencies": {
78+
"@types/icss-utils": "^4.1.0",
6079
"@types/jest": "^24.0.15",
6180
"@types/less": "^3.0.0",
62-
"@types/lodash": "^4.14.136",
63-
"@types/node": "^10.0.0",
81+
"@types/lodash.camelcase": "^4.3.6",
82+
"@types/node": "^12.11.1",
83+
"@types/postcss-load-config": "^2.0.0",
84+
"@types/postcss-nested": "^4.1.0",
85+
"@types/reserved-words": "^0.1.0",
6486
"@types/sass": "^1.16.0",
65-
"husky": "^2.7.0",
87+
"@typescript-eslint/eslint-plugin": "^2.4.0",
88+
"@typescript-eslint/parser": "^2.4.0",
89+
"eslint": "^6.5.1",
90+
"eslint-config-prettier": "^6.4.0",
91+
"husky": "^3.0.9",
6692
"jest": "^24.8.0",
93+
"lint-staged": "^9.4.2",
94+
"postcss-import-sync2": "^1.1.0",
6795
"prettier": "^1.18.2",
68-
"pretty-quick": "^1.11.1",
6996
"ts-jest": "^24.0.2",
70-
"tslint": "^5.18.0",
71-
"typescript": "^3.5.3"
97+
"typescript": "^3.6.4"
7298
},
7399
"peerDependencies": {
74100
"typescript": "^3.0.0"

src/@types/icss-utils.d.ts

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

src/@types/postcss-filter-plugins.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare module 'postcss-filter-plugins' {
2+
import { Plugin } from 'postcss';
3+
interface Options {
4+
direction?: 'backward' | 'both' | 'forward';
5+
exclude?: string[];
6+
silent?: boolean;
7+
}
8+
const filter: (options?: Options) => Plugin<unknown>;
9+
export = filter;
10+
}

src/@types/postcss-import-sync2.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module 'postcss-import-sync2' {
2+
import { Plugin } from 'postcss';
3+
const plugin: Plugin<{}>;
4+
export = plugin;
5+
}

src/@types/postcss-load.config.d.ts

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

src/@types/postcss-nested.d.ts

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

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

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

src/@types/strip-css-singleline-comments.d.ts

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

src/config.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)