Skip to content

Commit fa058dd

Browse files
authored
feat: support .cts (#252)
add support for loading cts files
1 parent d977c08 commit fa058dd

8 files changed

Lines changed: 75 additions & 20 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,18 @@ In this case create JS file named:
9191
- `.postcssrc.mjs`
9292
- `.postcssrc.cjs`
9393
- `.postcssrc.ts`
94+
- `.postcssrc.cts`
9495
- `postcss.config.js`
9596
- `postcss.config.mjs`
9697
- `postcss.config.cjs`
9798
- `postcss.config.ts`
99+
- `postcss.config.cts`
98100

99101
```
100102
Project (Root)
101103
|– client
102104
|– public
103-
|- (.postcssrc|postcss.config).(js|mjs|cjs|ts)
105+
|- (.postcssrc|postcss.config).(js|mjs|cjs|ts|cts)
104106
|- package.json
105107
```
106108

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"exclude": [
6666
"**/*.test.*"
6767
],
68-
"lines": 97.64,
68+
"lines": 97.8,
6969
"check-coverage": true
7070
},
7171
"prettier": {

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ const addTypeScriptLoader = (options = {}, loader) => {
8585
`.${moduleName}rc.yaml`,
8686
`.${moduleName}rc.yml`,
8787
`.${moduleName}rc.ts`,
88+
`.${moduleName}rc.cts`,
8889
`.${moduleName}rc.js`,
8990
`.${moduleName}rc.cjs`,
9091
`.${moduleName}rc.mjs`,
9192
`${moduleName}.config.ts`,
93+
`${moduleName}.config.cts`,
9294
`${moduleName}.config.js`,
9395
`${moduleName}.config.cjs`,
9496
`${moduleName}.config.mjs`
@@ -100,7 +102,8 @@ const addTypeScriptLoader = (options = {}, loader) => {
100102
'.js': importDefault,
101103
'.cjs': importDefault,
102104
'.mjs': importDefault,
103-
'.ts': loader
105+
'.ts': loader,
106+
'.cts': loader
104107
}
105108
}
106109
}
@@ -112,7 +115,10 @@ const withTypeScriptLoader = (rcFunc) => {
112115

113116
try {
114117
// Register TypeScript compiler instance
115-
registerer = require('ts-node').register()
118+
registerer = require('ts-node').register({
119+
// transpile to cjs even if compilerOptions.module in tsconfig is not Node16/NodeNext.
120+
moduleTypes: { '**/*.cts': 'cjs' }
121+
})
116122

117123
return require(configFile)
118124
} catch (err) {

test/ts.test.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ describe('postcss.config.ts - {Object} - Load Config', test => {
1313
syntax: true
1414
}
1515

16-
const expected = config => {
16+
test('Load Config - postcss.config.ts (CommonJS)', async () => {
17+
const config = await postcssrc(ctx, 'test/ts/object/cjs-in-ts')
18+
assertExpectedConfig(config, 'test/ts/object/cjs-in-ts/postcss.config.ts')
19+
})
20+
21+
test('Load Config - postcss.config.cts', async () => {
22+
const config = await postcssrc(ctx, 'test/ts/object/cts')
23+
assertExpectedConfig(config, 'test/ts/object/cts/postcss.config.cts')
24+
})
25+
26+
function assertExpectedConfig (config, expectedPath) {
1727
is(config.options.parser, require('sugarss'))
1828
is(config.options.syntax, require('sugarss'))
1929
is(config.options.map, false)
@@ -24,13 +34,9 @@ describe('postcss.config.ts - {Object} - Load Config', test => {
2434
is(typeof config.plugins[0], 'function')
2535
is(typeof config.plugins[1], 'function')
2636

27-
is(config.file, path.resolve('test/ts/object', 'postcss.config.ts'))
37+
is(config.file, path.resolve(expectedPath))
2838
}
2939

30-
test('Async', () => {
31-
return postcssrc(ctx, 'test/ts/object').then(expected)
32-
})
33-
3440
test.run()
3541
})
3642

@@ -40,7 +46,7 @@ test('postcss.config.ts - {Object} - Process CSS', () => {
4046
syntax: false
4147
}
4248

43-
return postcssrc(ctx, 'test/ts/object').then(config => {
49+
return postcssrc(ctx, 'test/ts/object/cjs-in-ts').then(config => {
4450
return postcss(config.plugins)
4551
.process(fixture('ts/object', 'index.css'), config.options)
4652
.then(result => {
@@ -56,7 +62,7 @@ test('postcss.config.ts - {Object} - Process SSS', () => {
5662
syntax: false
5763
}
5864

59-
return postcssrc(ctx, 'test/ts/object').then(config => {
65+
return postcssrc(ctx, 'test/ts/object/cjs-in-ts').then(config => {
6066
return postcss(config.plugins)
6167
.process(fixture('ts/object', 'index.sss'), config.options)
6268
.then(result => {
@@ -71,7 +77,17 @@ describe('postcss.config.ts - {Array} - Load Config', () => {
7177
syntax: true
7278
}
7379

74-
const expected = config => {
80+
test('Load Config - postcss.config.ts (CommonJS)', async () => {
81+
const config = await postcssrc(ctx, 'test/ts/array/cjs-in-ts')
82+
assertExpectedConfig(config, 'test/ts/array/cjs-in-ts/postcss.config.ts')
83+
})
84+
85+
test('Load Config - postcss.config.cts', async () => {
86+
const config = await postcssrc(ctx, 'test/ts/array/cts')
87+
assertExpectedConfig(config, 'test/ts/array/cts/postcss.config.cts')
88+
})
89+
90+
function assertExpectedConfig (config, expectedPath) {
7591
is(config.options.parser, require('sugarss'))
7692
is(config.options.syntax, require('sugarss'))
7793
is(config.options.map, false)
@@ -82,13 +98,9 @@ describe('postcss.config.ts - {Array} - Load Config', () => {
8298
is(typeof config.plugins[0], 'object')
8399
is(typeof config.plugins[1], 'object')
84100

85-
is(config.file, path.resolve('test/ts/array', 'postcss.config.ts'))
101+
is(config.file, path.resolve(expectedPath))
86102
}
87103

88-
test('Async', () => {
89-
return postcssrc(ctx, 'test/ts/array').then(expected)
90-
})
91-
92104
test.run()
93105
})
94106

@@ -98,7 +110,7 @@ test('postcss.config.ts - {Array} - Process CSS', () => {
98110
syntax: false
99111
}
100112

101-
return postcssrc(ctx, 'test/ts/array').then(config => {
113+
return postcssrc(ctx, 'test/ts/array/cjs-in-ts').then(config => {
102114
return postcss(config.plugins)
103115
.process(fixture('ts/array', 'index.css'), config.options)
104116
.then(result => {
@@ -114,7 +126,7 @@ test('postcss.config.ts - {Array} - Process SSS', () => {
114126
syntax: false
115127
}
116128

117-
return postcssrc(ctx, 'test/ts/array').then(config => {
129+
return postcssrc(ctx, 'test/ts/array/cjs-in-ts').then(config => {
118130
return postcss(config.plugins)
119131
.process(fixture('ts/array', 'index.sss'), config.options)
120132
.then(result => {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import postcssImport from 'postcss-import';
2+
import postcssNested from 'postcss-nested';
3+
import cssnano from 'cssnano';
4+
import { ConfigFn } from '../../../src';
5+
6+
const config: ConfigFn = ctx => ({
7+
parser: ctx.parser ? 'sugarss' : false,
8+
syntax: ctx.syntax ? 'sugarss' : false,
9+
map: ctx.map ? 'inline' : false,
10+
from: './test/ts/array/fixtures/index.css',
11+
to: './test/ts/array/expect/index.css',
12+
plugins: [
13+
postcssImport(),
14+
postcssNested({ preserveEmpty: true }),
15+
ctx.env === 'production' ? cssnano() : false
16+
]
17+
});
18+
19+
export default config;
File renamed without changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ConfigFn } from '../../../src';
2+
3+
const config: ConfigFn = ctx => ({
4+
parser: ctx.parser ? 'sugarss' : false,
5+
syntax: ctx.syntax ? 'sugarss' : false,
6+
map: ctx.map ? 'inline' : false,
7+
from: './test/ts/object/fixtures/index.css',
8+
to: './test/ts/object/expect/index.css',
9+
plugins: {
10+
'postcss-import': {},
11+
'postcss-nested': {},
12+
cssnano: ctx.env === 'production' ? {} : false
13+
}
14+
})
15+
16+
export = config;

0 commit comments

Comments
 (0)