Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for TypeScript based configs #632

Merged
merged 8 commits into from
Apr 3, 2023
16,190 changes: 1,588 additions & 14,602 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"cosmiconfig": "^8.1.3",
"cosmiconfig-typescript-loader": "^4.3.0",
"klona": "^2.0.6",
"semver": "^7.3.8"
},
Expand Down Expand Up @@ -74,6 +75,7 @@
"postcss-dark-theme-class": "^0.7.3",
"postcss-import": "^15.1.0",
"postcss-js": "^4.0.1",
"postcss-load-config": "^4.0.1",
"postcss-nested": "^6.0.1",
"postcss-short": "^5.0.0",
"prettier": "^2.8.7",
Expand Down
13 changes: 13 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Module from "module";

import { klona } from "klona/full";
import { cosmiconfig, defaultLoaders } from "cosmiconfig";
import { TypeScriptLoader } from "cosmiconfig-typescript-loader";

import SyntaxError from "./Error";

Expand Down Expand Up @@ -59,16 +60,25 @@ async function loadConfig(loaderContext, config, postcssOptions) {
`.${moduleName}rc.js`,
`.${moduleName}rc.mjs`,
`.${moduleName}rc.cjs`,
`.${moduleName}rc.ts`,
`.${moduleName}rc.mts`,
`.${moduleName}rc.cts`,
`.config/${moduleName}rc`,
`.config/${moduleName}rc.json`,
`.config/${moduleName}rc.yaml`,
`.config/${moduleName}rc.yml`,
`.config/${moduleName}rc.js`,
`.config/${moduleName}rc.mjs`,
`.config/${moduleName}rc.cjs`,
`.config/${moduleName}rc.ts`,
`.config/${moduleName}rc.mts`,
`.config/${moduleName}rc.cts`,
`${moduleName}.config.js`,
`${moduleName}.config.mjs`,
`${moduleName}.config.cjs`,
`${moduleName}.config.ts`,
`${moduleName}.config.mts`,
`${moduleName}.config.cts`,
],
loaders: {
".js": async (...args) => {
Expand Down Expand Up @@ -122,6 +132,9 @@ async function loadConfig(loaderContext, config, postcssOptions) {

return result;
},
".cts": TypeScriptLoader(),
".mts": TypeScriptLoader(),
".ts": TypeScriptLoader(),
},
});

Expand Down
38 changes: 38 additions & 0 deletions test/config-autoload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ describe("autoload config", () => {
);
});

it('should load "postcss.config.cts" with "Object" syntax of plugins', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "ts/object")
);

expect(loadedConfig.config.map).toEqual(false);
expect(loadedConfig.config.from).toEqual(
"./test/fixtures/config-autoload/ts/object/index.css"
);
expect(loadedConfig.config.to).toEqual(
"./test/fixtures/config-autoload/ts/object/expect/index.css"
);
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(2);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "ts/object", "postcss.config.cts")
);
});

it('should load "postcss.config.js" with "Array" syntax of plugins', async () => {
const loadedConfig = await loadConfig(
loaderContext,
Expand All @@ -97,6 +116,25 @@ describe("autoload config", () => {
);
});

it('should load "postcss.config.ts" with "Array" syntax of plugins', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "ts/array")
);

expect(loadedConfig.config.map).toEqual(false);
expect(loadedConfig.config.from).toEqual(
"./test/fixtures/config-autoload/ts/object/index.css"
);
expect(loadedConfig.config.to).toEqual(
"./test/fixtures/config-autoload/ts/object/expect/index.css"
);
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(4);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "ts/array", "postcss.config.ts")
);
});

it('should load empty ".postcssrc"', async () => {
const loadedConfig = await loadConfig(
loaderContext,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/config-autoload/ts/array/imports/section.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import {
color: goldenrod;
}
5 changes: 5 additions & 0 deletions test/fixtures/config-autoload/ts/array/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import 'imports/section.css';

.test {
color: cyan;
}
3 changes: 3 additions & 0 deletions test/fixtures/config-autoload/ts/array/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import style from './index.css'

export default style
23 changes: 23 additions & 0 deletions test/fixtures/config-autoload/ts/array/postcss.config.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Config as PostCSSConfig } from 'postcss-load-config';
import { Configuration as WebpackConfig } from 'webpack';

module.exports = function (api: WebpackConfig): PostCSSConfig {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-akait I believe this is the way to solve the issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it should be https://github.com/webpack-contrib/postcss-loader/blob/master/src/utils.js#L152, it is mix of types, so I think we just need to add this to documents

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to update types in docs and we can merge + small example, thank you

return {
parser: 'sugarss',
syntax: 'sugarss',
map: api.mode === 'development' ? 'inline' : false,
from: './test/fixtures/config-autoload/js/object/index.css',
to: './test/fixtures/config-autoload/js/object/expect/index.css',
plugins: [
'postcss-import',
[
'postcss-nested',
{
// Options
}
],
require('postcss-nested'),
require('postcss-nested')({ /* Options */ }),
]
}
};
3 changes: 3 additions & 0 deletions test/fixtures/config-autoload/ts/object/imports/section.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import {
color: red;
}
5 changes: 5 additions & 0 deletions test/fixtures/config-autoload/ts/object/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import "imports/section.css";

.test {
color: blue;
}
3 changes: 3 additions & 0 deletions test/fixtures/config-autoload/ts/object/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import style from './index.css'

export default style
18 changes: 18 additions & 0 deletions test/fixtures/config-autoload/ts/object/postcss.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Config as PostCSSConfig } from 'postcss-load-config';
import { Configuration as WebpackConfig } from 'webpack';

const config = function (api: WebpackConfig): PostCSSConfig {
return {
parser: 'sugarss',
syntax: 'sugarss',
map: api.mode === 'development' ? 'inline' : false,
from: './test/fixtures/config-autoload/js/object/index.css',
to: './test/fixtures/config-autoload/js/object/expect/index.css',
plugins: {
'postcss-import': {},
'postcss-nested': {},
}
}
};

export default config;