Skip to content

Commit 02e71df

Browse files
committed
Use transformer config in jest.config.js instead of a separate jesttransformcss.config.js config file; resolves #21
1 parent 411c782 commit 02e71df

File tree

4 files changed

+28
-188
lines changed

4 files changed

+28
-188
lines changed

README.md

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,40 +84,30 @@ transform: {
8484

8585
By default, `jest-transform-css` will treat every file it transforms as a regular CSS file.
8686

87-
You need to opt into css-modules mode by specifying it in the configuration. Create a file called `jesttransformcss.config.js` at your project root and add
87+
You need to opt into css-modules mode by specifying it in the configuration.
88+
Add `{ modules: true }` option to `jest-transform-css` in `jest.config.js`:
8889

89-
```js
90-
// jesttransformcss.config.js
91-
92-
module.exports = {
93-
modules: true
94-
};
90+
```diff
91+
// in the Jest config
92+
transform: {
93+
- "^.+\\.css$": "jest-transform-css"
94+
+ "^.+\\.css$": ["jest-transform-css", { modules: true }]
95+
}
9596
```
9697

97-
This will enable CSS module transformation for all CSS files transformed by `jest-transform-css`.
98-
99-
If your setup uses both, CSS modules and regular CSS, then you can determine how to load each file individually by specifying a function:
98+
This will enable CSS module transformation by `jest-transform-css` for all CSS files matching the pattern.
10099

101-
```js
102-
// jesttransformcss.config.js
100+
The config also supports `generateScopedName` property to customize the generated class names. Helpful when using Jest Snapshots and not wanting unnecessary noise from hash generated classnames.
103101

104-
module.exports = {
105-
modules: filename => filename.endsWith(".mod.css")
106-
};
107102
```
108-
109-
This will load all files with `.mod.css` as CSS modules and load all other files as regular CSS. Notice that the function will only be called for whichever regex you provided in the `transform` option of the Jest config.
110-
111-
Also supports `generateScopedName` property to customize the generated class names. Helpful when using Jest Snapshots and not wanting unnecessary noise from hash generated classnames.
112-
113-
```js
114-
// jesttransformcss.config.js
115-
116-
module.exports = {
117-
modules: true,
118-
generateScopedName: "[path]_[name]_[local]"
119-
// Default value is: '[path][local]-[hash:base64:10]'
120-
};
103+
// in the Jest config
104+
transform: {
105+
"^.+\\.css$": ["jest-transform-css", {
106+
modules: true,
107+
generateScopedName: "[path]_[name]_[local]"
108+
// Default value is: '[path][local]-[hash:base64:10]'
109+
}]
110+
}
121111
```
122112
Link to all available [placeholder tokens](https://github.com/webpack/loader-utils#interpolatename) \*Note not all placeholders are working and must be tested.
123113

index.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
const fs = require("fs");
22
const crypto = require("crypto");
33
const crossSpawn = require("cross-spawn");
4-
const { cosmiconfigSync } = require("cosmiconfig");
54
const stripIndent = require("common-tags/lib/stripIndent");
65
const THIS_FILE = fs.readFileSync(__filename);
7-
const explorer = cosmiconfigSync("jesttransformcss");
8-
const transformConfig = explorer.search();
96

107
module.exports = {
118
getCacheKey: (fileData, filename, configString, options) => {
@@ -26,7 +23,7 @@ module.exports = {
2623
.update("\0", "utf8")
2724
.update(configString)
2825
.update("\0", "utf8")
29-
.update(JSON.stringify(transformConfig))
26+
.update(JSON.stringify(options.transformerConfig))
3027
// TODO load postcssrc (the config) sync and make it part of the cache
3128
// key
3229
// .update("\0", "utf8")
@@ -39,18 +36,14 @@ module.exports = {
3936

4037
process: (src, filename, config, options) => {
4138
// skip when plain CSS is used
42-
// You can create jesttransformcss.config.js in your project and add
43-
// module.exports = { modules: true };
44-
// or
45-
// module.exports = { modules: filename => filename.endsWith(".mod.css") };
46-
// to enable css module transformation. for all or for certain files.
39+
// You can pass config to the transformer in jest.config.js like so:
40+
// "^.+\\.css$": ["jest-transform-css", { modules: true }]
41+
// to enable css module transformation.
4742
const useModules =
48-
transformConfig &&
49-
transformConfig.config &&
50-
((typeof transformConfig.config.modules === "boolean" &&
51-
transformConfig.config.modules) ||
52-
(typeof transformConfig.config.modules === "function" &&
53-
transformConfig.config.modules(filename)));
43+
config &&
44+
config.transformerConfig &&
45+
((typeof config.transformerConfig.modules === "boolean" &&
46+
config.transformerConfig.modules));
5447
if (!useModules) {
5548
return {
5649
code: stripIndent`
@@ -74,7 +67,7 @@ module.exports = {
7467
${JSON.stringify({
7568
src,
7669
filename,
77-
transformConfig: transformConfig.config,
70+
transformConfig: config.transformerConfig,
7871
// options
7972
})}
8073
)
@@ -92,7 +85,7 @@ module.exports = {
9285
try {
9386
// we likely logged something to the console from postcss-runner
9487
// in order to debug, and hence the parsing fails!
95-
parsed = JSON.parse(result.stdout.toString());
88+
const parsed = JSON.parse(result.stdout.toString());
9689
css = parsed.css;
9790
tokens = parsed.tokens;
9891
if (Array.isArray(parsed.warnings))

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"license": "MIT",
88
"dependencies": {
99
"common-tags": "1.8.2",
10-
"cosmiconfig": "7.0.1",
1110
"cross-spawn": "7.0.3",
1211
"postcss-load-config": "2.0.0",
1312
"postcss-modules": "4.3.1",

yarn.lock

Lines changed: 0 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,18 @@
22
# yarn lockfile v1
33

44

5-
"@babel/code-frame@^7.0.0":
6-
version "7.16.7"
7-
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.16.7.tgz#44416b6bd7624b998f5b1af5d470856c40138789"
8-
integrity sha512-iAXqUn8IIeBTNd72xsFlgaXHkMBMt6y4HJp1tIaK465CWLT/fG1aqB7ykr95gHHmlBdGbFeWWfyB4NJJ0nmeIg==
9-
dependencies:
10-
"@babel/highlight" "^7.16.7"
11-
12-
"@babel/helper-validator-identifier@^7.16.7":
13-
version "7.16.7"
14-
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.16.7.tgz#e8c602438c4a8195751243da9031d1607d247cad"
15-
integrity sha512-hsEnFemeiW4D08A5gUAZxLBTXpZ39P+a+DGDsHw1yxqyQ/jzFEnxf5uTEGp+3bzAbNOxU1paTgYS4ECU/IgfDw==
16-
17-
"@babel/highlight@^7.16.7":
18-
version "7.16.10"
19-
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.16.10.tgz#744f2eb81579d6eea753c227b0f570ad785aba88"
20-
integrity sha512-5FnTQLSLswEj6IkgVw5KusNUUFY9ZGqe/TRFnP/BKYHYgfh7tc+C7mwiy95/yNP7Dh9x580Vv8r7u7ZfTBFxdw==
21-
dependencies:
22-
"@babel/helper-validator-identifier" "^7.16.7"
23-
chalk "^2.0.0"
24-
js-tokens "^4.0.0"
25-
26-
"@types/parse-json@^4.0.0":
27-
version "4.0.0"
28-
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
29-
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
30-
31-
ansi-styles@^3.2.1:
32-
version "3.2.1"
33-
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
34-
integrity sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==
35-
dependencies:
36-
color-convert "^1.9.0"
37-
385
argparse@^1.0.7:
396
version "1.0.10"
407
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
418
integrity sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==
429
dependencies:
4310
sprintf-js "~1.0.2"
4411

45-
callsites@^3.0.0:
46-
version "3.1.0"
47-
resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73"
48-
integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==
49-
50-
chalk@^2.0.0:
51-
version "2.4.2"
52-
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
53-
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
54-
dependencies:
55-
ansi-styles "^3.2.1"
56-
escape-string-regexp "^1.0.5"
57-
supports-color "^5.3.0"
58-
59-
color-convert@^1.9.0:
60-
version "1.9.3"
61-
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8"
62-
integrity sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==
63-
dependencies:
64-
color-name "1.1.3"
65-
66-
67-
version "1.1.3"
68-
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
69-
integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=
70-
7112
7213
version "1.8.2"
7314
resolved "https://registry.yarnpkg.com/common-tags/-/common-tags-1.8.2.tgz#94ebb3c076d26032745fd54face7f688ef5ac9c6"
7415
integrity sha512-gk/Z852D2Wtb//0I+kRFNKKE9dIIVirjoqPoA1wJU+XePVXZfGeBpk45+A1rKO4Q43prqWBNY/MiIeRLbPWUaA==
7516

76-
77-
version "7.0.1"
78-
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-7.0.1.tgz#714d756522cace867867ccb4474c5d01bbae5d6d"
79-
integrity sha512-a1YWNUV2HwGimB7dU2s1wUMurNKjpx60HxBB6xUM8Re+2s1g1IIfJvFR0/iCF+XHdE0GMTKTuLR32UQff4TEyQ==
80-
dependencies:
81-
"@types/parse-json" "^4.0.0"
82-
import-fresh "^3.2.1"
83-
parse-json "^5.0.0"
84-
path-type "^4.0.0"
85-
yaml "^1.10.0"
86-
8717
cosmiconfig@^4.0.0:
8818
version "4.0.0"
8919
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-4.0.0.tgz#760391549580bbd2df1e562bc177b13c290972dc"
@@ -115,11 +45,6 @@ error-ex@^1.3.1:
11545
dependencies:
11646
is-arrayish "^0.2.1"
11747

118-
escape-string-regexp@^1.0.5:
119-
version "1.0.5"
120-
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
121-
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
122-
12348
esprima@^4.0.0:
12449
version "4.0.1"
12550
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
@@ -132,11 +57,6 @@ generic-names@^4.0.0:
13257
dependencies:
13358
loader-utils "^3.2.0"
13459

135-
has-flag@^3.0.0:
136-
version "3.0.0"
137-
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
138-
integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0=
139-
14060
icss-replace-symbols@^1.1.0:
14161
version "1.1.0"
14262
resolved "https://registry.yarnpkg.com/icss-replace-symbols/-/icss-replace-symbols-1.1.0.tgz#06ea6f83679a7749e386cfe1fe812ae5db223ded"
@@ -154,14 +74,6 @@ import-cwd@^2.0.0:
15474
dependencies:
15575
import-from "^2.1.0"
15676

157-
import-fresh@^3.2.1:
158-
version "3.3.0"
159-
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
160-
integrity sha512-veYYhQa+D1QBKznvhUHxb8faxlrwUnxseDAbAp457E0wLNio2bOSKnjYDhMj+YiAq61xrMGhQk9iXVk5FzgQMw==
161-
dependencies:
162-
parent-module "^1.0.0"
163-
resolve-from "^4.0.0"
164-
16577
import-from@^2.1.0:
16678
version "2.1.0"
16779
resolved "https://registry.yarnpkg.com/import-from/-/import-from-2.1.0.tgz#335db7f2a7affd53aaa471d4b8021dee36b7f3b1"
@@ -184,11 +96,6 @@ isexe@^2.0.0:
18496
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
18597
integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=
18698

187-
js-tokens@^4.0.0:
188-
version "4.0.0"
189-
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
190-
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
191-
19299
js-yaml@^3.9.0:
193100
version "3.14.1"
194101
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.1.tgz#dae812fdb3825fa306609a8717383c50c36a0537"
@@ -202,16 +109,6 @@ json-parse-better-errors@^1.0.1:
202109
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
203110
integrity sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==
204111

205-
json-parse-even-better-errors@^2.3.0:
206-
version "2.3.1"
207-
resolved "https://registry.yarnpkg.com/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz#7c47805a94319928e05777405dc12e1f7a4ee02d"
208-
integrity sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==
209-
210-
lines-and-columns@^1.1.6:
211-
version "1.2.4"
212-
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"
213-
integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==
214-
215112
loader-utils@^3.2.0:
216113
version "3.2.0"
217114
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-3.2.0.tgz#bcecc51a7898bee7473d4bc6b845b23af8304d4f"
@@ -227,13 +124,6 @@ nanoid@^3.3.1:
227124
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.2.tgz#c89622fafb4381cd221421c69ec58547a1eec557"
228125
integrity sha512-CuHBogktKwpm5g2sRgv83jEy2ijFzBwMoYA60orPDR7ynsLijJDqgsi4RDGj3OJpy3Ieb+LYwiRmIOGyytgITA==
229126

230-
parent-module@^1.0.0:
231-
version "1.0.1"
232-
resolved "https://registry.yarnpkg.com/parent-module/-/parent-module-1.0.1.tgz#691d2709e78c79fae3a156622452d00762caaaa2"
233-
integrity sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==
234-
dependencies:
235-
callsites "^3.0.0"
236-
237127
parse-json@^4.0.0:
238128
version "4.0.0"
239129
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0"
@@ -242,26 +132,11 @@ parse-json@^4.0.0:
242132
error-ex "^1.3.1"
243133
json-parse-better-errors "^1.0.1"
244134

245-
parse-json@^5.0.0:
246-
version "5.2.0"
247-
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-5.2.0.tgz#c76fc66dee54231c962b22bcc8a72cf2f99753cd"
248-
integrity sha512-ayCKvm/phCGxOkYRSCM82iDwct8/EonSEgCSxWxD7ve6jHggsFl4fZVQBPRNgQoKiuV/odhFrGzQXZwbifC8Rg==
249-
dependencies:
250-
"@babel/code-frame" "^7.0.0"
251-
error-ex "^1.3.1"
252-
json-parse-even-better-errors "^2.3.0"
253-
lines-and-columns "^1.1.6"
254-
255135
path-key@^3.1.0:
256136
version "3.1.1"
257137
resolved "https://registry.yarnpkg.com/path-key/-/path-key-3.1.1.tgz#581f6ade658cbba65a0d3380de7753295054f375"
258138
integrity sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==
259139

260-
path-type@^4.0.0:
261-
version "4.0.0"
262-
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
263-
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==
264-
265140
picocolors@^1.0.0:
266141
version "1.0.0"
267142
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c"
@@ -349,11 +224,6 @@ resolve-from@^3.0.0:
349224
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-3.0.0.tgz#b22c7af7d9d6881bc8b6e653335eebcb0a188748"
350225
integrity sha1-six699nWiBvItuZTM17rywoYh0g=
351226

352-
resolve-from@^4.0.0:
353-
version "4.0.0"
354-
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-4.0.0.tgz#4abcd852ad32dd7baabfe9b40e00a36db5f392e6"
355-
integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==
356-
357227
shebang-command@^2.0.0:
358228
version "2.0.0"
359229
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"
@@ -386,13 +256,6 @@ [email protected]:
386256
resolved "https://registry.yarnpkg.com/style-inject/-/style-inject-0.3.0.tgz#d21c477affec91811cc82355832a700d22bf8dd3"
387257
integrity sha512-IezA2qp+vcdlhJaVm5SOdPPTUu0FCEqfNSli2vRuSIBbu5Nq5UvygTk/VzeCqfLz2Atj3dVII5QBKGZRZ0edzw==
388258

389-
supports-color@^5.3.0:
390-
version "5.5.0"
391-
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"
392-
integrity sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==
393-
dependencies:
394-
has-flag "^3.0.0"
395-
396259
util-deprecate@^1.0.2:
397260
version "1.0.2"
398261
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
@@ -404,8 +267,3 @@ which@^2.0.1:
404267
integrity sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==
405268
dependencies:
406269
isexe "^2.0.0"
407-
408-
yaml@^1.10.0:
409-
version "1.10.2"
410-
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
411-
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==

0 commit comments

Comments
 (0)