Skip to content

Commit 4964068

Browse files
refactor: add config options
1 parent 520a5f5 commit 4964068

File tree

10 files changed

+244
-79
lines changed

10 files changed

+244
-79
lines changed

README.md

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ module.exports = {
111111

112112
<h2 align="center">Options</h2>
113113

114-
| Name | Type | Default | Description |
115-
| :------------------------: | :-----------------: | :---------: | :------------------------------------------- |
116-
| [`exec`](#exec) | `{Boolean}` | `undefined` | Enable PostCSS Parser support in `CSS-in-JS` |
117-
| [`parser`](#syntaxes) | `{String\|Object}` | `undefined` | Set PostCSS Parser |
118-
| [`syntax`](#syntaxes) | `{String\|Object}` | `undefined` | Set PostCSS Syntax |
119-
| [`stringifier`](#syntaxes) | `{String\|Object}` | `undefined` | Set PostCSS Stringifier |
120-
| [`config`](#config) | `{Object}` | `undefined` | Set `postcss.config.js` config path && `ctx` |
121-
| [`plugins`](#plugins) | `{Array\|Function}` | `[]` | Set PostCSS Plugins |
122-
| [`sourceMap`](#sourcemap) | `{String\|Boolean}` | `false` | Enable Source Maps |
114+
| Name | Type | Default | Description |
115+
| :------------------------: | :-------------------------: | :---------: | :------------------------------------------- |
116+
| [`exec`](#exec) | `{Boolean}` | `undefined` | Enable PostCSS Parser support in `CSS-in-JS` |
117+
| [`parser`](#syntaxes) | `{String\|Object}` | `undefined` | Set PostCSS Parser |
118+
| [`syntax`](#syntaxes) | `{String\|Object}` | `undefined` | Set PostCSS Syntax |
119+
| [`stringifier`](#syntaxes) | `{String\|Object}` | `undefined` | Set PostCSS Stringifier |
120+
| [`config`](#config) | `{String\|Object\|Boolean}` | `undefined` | Set `postcss.config.js` config path && `ctx` |
121+
| [`plugins`](#plugins) | `{Array\|Function}` | `[]` | Set PostCSS Plugins |
122+
| [`sourceMap`](#sourcemap) | `{String\|Boolean}` | `false` | Enable Source Maps |
123123

124124
### `Exec`
125125

@@ -140,12 +140,61 @@ If you use JS styles without the [`postcss-js`][postcss-js] parser, add the `exe
140140

141141
### `Config`
142142

143+
Type: `Boolean|String|Object`
144+
Default: `undefined`
145+
146+
#### Boolean
147+
148+
Enables/Disables autoloading config.
149+
150+
**webpack.config.js**
151+
152+
```js
153+
module.exports = {
154+
module: {
155+
rules: [
156+
{
157+
test: /\.css$/i,
158+
loader: 'postcss-loader',
159+
options: {
160+
config: false,
161+
},
162+
},
163+
],
164+
},
165+
};
166+
```
167+
168+
#### String
169+
170+
Allows to specify the absolute path to the config file.
171+
172+
**webpack.config.js**
173+
174+
```js
175+
module.exports = {
176+
module: {
177+
rules: [
178+
{
179+
test: /\.css$/i,
180+
loader: 'postcss-loader',
181+
options: {
182+
config: path.resolve(__dirname, 'custom.config.js'),
183+
},
184+
},
185+
],
186+
},
187+
};
188+
```
189+
190+
#### Object
191+
143192
| Name | Type | Default | Description |
144193
| :-----------------------: | :--------: | :---------: | :----------------------- |
145194
| [`path`](#path) | `{String}` | `undefined` | PostCSS Config Directory |
146195
| [`context`](#context-ctx) | `{Object}` | `undefined` | PostCSS Config Context |
147196

148-
#### `Path`
197+
##### `Path`
149198

150199
You can manually specify the path to search for your config (`postcss.config.js`) with the `config.path` option. This is needed if you store your config in a separate e.g `./config || ./.config` folder.
151200

@@ -169,7 +218,7 @@ You can manually specify the path to search for your config (`postcss.config.js`
169218

170219
[supported config formats]: https://github.com/michael-ciniawsky/postcss-load-config#usage
171220

172-
#### `Context (ctx)`
221+
##### `Context (ctx)`
173222

174223
| Name | Type | Default | Description |
175224
| :-------: | :--------: | :-------------------: | :------------------------------- |

src/index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default async function loader(content, sourceMap, meta = {}) {
8282
rc.ctx.webpack = this;
8383

8484
try {
85-
config = await loadConfig(rc.ctx, rc.path);
85+
config = await loadConfig(options.config, rc.ctx, rc.path);
8686
} catch (error) {
8787
callback(error);
8888

@@ -98,15 +98,14 @@ export default async function loader(content, sourceMap, meta = {}) {
9898
this.addDependency(config.file);
9999
}
100100

101-
// Disable override `to` option from `postcss.config.js`
102-
if (config.options.to) {
103-
// eslint-disable-next-line no-param-reassign
104-
delete config.options.to;
105-
}
106-
// Disable override `from` option from `postcss.config.js`
107-
if (config.options.from) {
108-
// eslint-disable-next-line no-param-reassign
109-
delete config.options.from;
101+
if (typeof config.options !== 'undefined') {
102+
if (typeof config.options.to !== 'undefined') {
103+
delete config.options.to;
104+
}
105+
106+
if (typeof config.options.from !== 'undefined') {
107+
delete config.options.from;
108+
}
110109
}
111110

112111
const plugins = config.plugins || [];

src/options.json

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@
33
"properties": {
44
"config": {
55
"description": "should be {Object} (https://github.com/postcss/postcss-loader#config)",
6-
"type": "object",
7-
"properties": {
8-
"path": {
9-
"description": "should be {String} (https://github.com/postcss/postcss-loader#path)",
6+
"anyOf": [
7+
{
8+
"type": "object",
9+
"properties": {
10+
"path": {
11+
"description": "should be {String} (https://github.com/postcss/postcss-loader#path)",
12+
"type": "string"
13+
},
14+
"ctx": {
15+
"description": "should be {Object} (https://github.com/postcss/postcss-loader#context-ctx)",
16+
"type": "object"
17+
}
18+
},
19+
"additionalProperties": false
20+
},
21+
{
22+
"description": "Allows to specify the path to the configuration file",
1023
"type": "string"
1124
},
12-
"ctx": {
13-
"description": "should be {Object} (https://github.com/postcss/postcss-loader#context-ctx)",
14-
"type": "object"
25+
{
26+
"description": "Enables/Disables autoloading config",
27+
"type": "boolean"
1528
}
16-
},
17-
"additionalProperties": false
29+
]
1830
},
1931
"exec": {
2032
"description": "should be {Boolean} (https://github.com/postcss/postcss-loader#exec)",

src/utils.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { cosmiconfig } from 'cosmiconfig';
55
import importCwd from 'import-cwd';
66

77
const parentModule = module;
8+
const moduleName = 'postcss';
89

910
const createContext = (context) => {
1011
const result = {
@@ -159,16 +160,39 @@ function exec(code, loaderContext) {
159160
return module.exports;
160161
}
161162

162-
function loadConfig(context, configPath) {
163-
const configPathResolved = configPath
164-
? path.resolve(configPath)
165-
: process.cwd();
163+
function loadConfig(config, context, configPath) {
164+
if (config === false) {
165+
return {};
166+
}
167+
168+
const searchPlaces = [
169+
`.${moduleName}rc.js`,
170+
`.${moduleName}rc.yaml`,
171+
`.${moduleName}rc.yml`,
172+
`.${moduleName}rc.json`,
173+
`.${moduleName}rc`,
174+
`${moduleName}.config.js`,
175+
`package.json`,
176+
];
177+
const cosmiconfigOptions = { searchPlaces };
178+
let configFilename;
179+
let configDir;
180+
181+
if (typeof config === 'string') {
182+
const parsedPath = path.parse(config);
183+
configFilename = parsedPath.base;
184+
configDir = parsedPath.dir;
185+
186+
cosmiconfigOptions.searchPlaces.unshift(configFilename);
187+
}
188+
189+
configDir = configDir || configPath || process.cwd();
166190

167-
return cosmiconfig('postcss')
168-
.search(configPathResolved)
191+
return cosmiconfig(moduleName, cosmiconfigOptions)
192+
.search(configDir)
169193
.then((result) => {
170194
if (!result) {
171-
throw new Error(`No PostCSS Config found in: ${configPathResolved}`);
195+
throw new Error(`No PostCSS Config found in: ${configDir}`);
172196
}
173197

174198
return processResult(createContext(context), result);

test/__snapshots__/validate-options.test.js.snap

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,22 @@
22

33
exports[`validate options should throw an error on the "config" option with "[]" value 1`] = `
44
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
5-
- options.config should be an object:
6-
object { path?, ctx? }
7-
-> should be {Object} (https://github.com/postcss/postcss-loader#config)"
5+
- options.config should be one of these:
6+
object { path?, ctx? } | string | boolean
7+
-> should be {Object} (https://github.com/postcss/postcss-loader#config)
8+
Details:
9+
* options.config should be an object:
10+
object { path?, ctx? }
11+
* options.config should be a string.
12+
-> Allows to specify the path to the configuration file
13+
* options.config should be a boolean.
14+
-> Enables/Disables autoloading config"
815
`;
916
1017
exports[`validate options should throw an error on the "config" option with "{"foo":"bar"}" value 1`] = `
1118
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
1219
- options.config has an unknown property 'foo'. These properties are valid:
13-
object { path?, ctx? }
14-
-> should be {Object} (https://github.com/postcss/postcss-loader#config)"
15-
`;
16-
17-
exports[`validate options should throw an error on the "config" option with "false" value 1`] = `
18-
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
19-
- options.config should be an object:
20-
object { path?, ctx? }
21-
-> should be {Object} (https://github.com/postcss/postcss-loader#config)"
22-
`;
23-
24-
exports[`validate options should throw an error on the "config" option with "test" value 1`] = `
25-
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
26-
- options.config should be an object:
27-
object { path?, ctx? }
28-
-> should be {Object} (https://github.com/postcss/postcss-loader#config)"
29-
`;
30-
31-
exports[`validate options should throw an error on the "config" option with "true" value 1`] = `
32-
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
33-
- options.config should be an object:
34-
object { path?, ctx? }
35-
-> should be {Object} (https://github.com/postcss/postcss-loader#config)"
36-
`;
37-
38-
exports[`validate options should throw an error on the "config" option with "true" value 2`] = `
39-
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
40-
- options.config should be an object:
41-
object { path?, ctx? }
42-
-> should be {Object} (https://github.com/postcss/postcss-loader#config)"
20+
object { path?, ctx? }"
4321
`;
4422
4523
exports[`validate options should throw an error on the "exec" option with "/test/" value 1`] = `

0 commit comments

Comments
 (0)