Skip to content

Commit 38ebe08

Browse files
refactor: execute option (#464)
BREKING CHANGE: the `exec` option was renamed to the `execute` option, always set the option to `true` if you use `postcss-js`
1 parent d0ea725 commit 38ebe08

11 files changed

+48
-54
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ And run `webpack` via your preferred method.
124124

125125
| Name | Type | Default | Description |
126126
| :---------------------------------: | :------------------: | :-----------------------------------: | :------------------------------------------- |
127-
| [`exec`](#exec) | `{Boolean}` | `undefined` | Enable PostCSS Parser support in `CSS-in-JS` |
127+
| [`execute`](#execute) | `{Boolean}` | `undefined` | Enable PostCSS Parser support in `CSS-in-JS` |
128128
| [`postcssOptions`](#postcssOptions) | `{Object\/Function}` | `defaults values for Postcss.process` | Set `postcss` options and plugins |
129129
| [`sourceMap`](#sourcemap) | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
130130

131-
### `exec`
131+
### `execute`
132132

133133
Type: `Boolean`
134134
Default: `undefined`
135135

136-
If you use JS styles without the [`postcss-js`][postcss-js] parser, add the `exec` option.
136+
If you use JS styles the [`postcss-js`](https://github.com/postcss/postcss-js) parser, add the `execute` option.
137137

138138
**webpack.config.js**
139139

@@ -147,13 +147,12 @@ module.exports = {
147147
'style-loader',
148148
{
149149
loader: 'css-loader',
150-
options: { importLoaders: 1 },
151150
},
152151
{
153152
loader: 'postcss-loader',
154153
options: {
155154
postcssOptions: {
156-
parser: 'sugarss',
155+
parser: 'postcss-js',
157156
},
158157
exec: true,
159158
},
@@ -867,6 +866,7 @@ module.exports = {
867866
postcssOptions: {
868867
parser: 'postcss-js',
869868
},
869+
execute: true,
870870
},
871871
},
872872
'babel-loader',

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ export default async function loader(content, sourceMap) {
5959
? options.sourceMap
6060
: this.sourceMap;
6161

62-
const { plugins, processOptions, needExecute } = getPostcssOptions(
62+
const { plugins, processOptions } = getPostcssOptions(
6363
this,
6464
loadedConfig,
6565
options.postcssOptions
6666
);
6767

68-
if (options.exec || needExecute) {
68+
if (options.execute) {
6969
// eslint-disable-next-line no-param-reassign
7070
content = exec(content, this);
7171
}

src/options.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
}
7272
]
7373
},
74-
"exec": {
74+
"execute": {
7575
"description": "Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)",
7676
"type": "boolean"
7777
},

src/utils.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,7 @@ function getPostcssOptions(loaderContext, config, postcssOptions = {}) {
200200
...processOptionsFromOptions,
201201
};
202202

203-
let needExecute = false;
204-
205203
if (typeof processOptions.parser === 'string') {
206-
// TODO respect the `syntax` option too or remove this options
207-
if (processOptions.parser === 'postcss-js') {
208-
needExecute = true;
209-
}
210-
211204
try {
212205
// eslint-disable-next-line import/no-dynamic-require, global-require
213206
processOptions.parser = require(processOptions.parser);
@@ -246,7 +239,7 @@ function getPostcssOptions(loaderContext, config, postcssOptions = {}) {
246239
}
247240
}
248241

249-
return { plugins, processOptions, needExecute };
242+
return { plugins, processOptions };
250243
}
251244

252245
const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i;

test/__snapshots__/exec.test.js.snap

-21
This file was deleted.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`"execute" option should work with "Boolean" value: css 1`] = `
4+
"a {
5+
color: green
6+
}"
7+
`;
8+
9+
exports[`"execute" option should work with "Boolean" value: errors 1`] = `Array []`;
10+
11+
exports[`"execute" option should work with "Boolean" value: warnings 1`] = `Array []`;
12+
13+
exports[`"execute" option should work with "postcss-js" parser: css 1`] = `
14+
"a {
15+
color: yellow
16+
}"
17+
`;
18+
19+
exports[`"execute" option should work with "postcss-js" parser: errors 1`] = `Array []`;
20+
21+
exports[`"execute" option should work with "postcss-js" parser: warnings 1`] = `Array []`;

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`validate options should throw an error on the "exec" option with "/test/" value 1`] = `
3+
exports[`validate options should throw an error on the "execute" option with "/test/" value 1`] = `
44
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
5-
- options.exec should be a boolean.
5+
- options.execute should be a boolean.
66
-> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)"
77
`;
88

9-
exports[`validate options should throw an error on the "exec" option with "[]" value 1`] = `
9+
exports[`validate options should throw an error on the "execute" option with "[]" value 1`] = `
1010
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
11-
- options.exec should be a boolean.
11+
- options.execute should be a boolean.
1212
-> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)"
1313
`;
1414

15-
exports[`validate options should throw an error on the "exec" option with "{"foo":"bar"}" value 1`] = `
15+
exports[`validate options should throw an error on the "execute" option with "{"foo":"bar"}" value 1`] = `
1616
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
17-
- options.exec should be a boolean.
17+
- options.execute should be a boolean.
1818
-> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)"
1919
`;
2020

21-
exports[`validate options should throw an error on the "exec" option with "{}" value 1`] = `
21+
exports[`validate options should throw an error on the "execute" option with "{}" value 1`] = `
2222
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
23-
- options.exec should be a boolean.
23+
- options.execute should be a boolean.
2424
-> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)"
2525
`;
2626

27-
exports[`validate options should throw an error on the "exec" option with "1" value 1`] = `
27+
exports[`validate options should throw an error on the "execute" option with "1" value 1`] = `
2828
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
29-
- options.exec should be a boolean.
29+
- options.execute should be a boolean.
3030
-> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)"
3131
`;
3232

33-
exports[`validate options should throw an error on the "exec" option with "test" value 1`] = `
33+
exports[`validate options should throw an error on the "execute" option with "test" value 1`] = `
3434
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
35-
- options.exec should be a boolean.
35+
- options.execute should be a boolean.
3636
-> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)"
3737
`;
3838

test/exec.test.js renamed to test/execute.test.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
getWarnings,
99
} from './helpers';
1010

11-
describe('"exec" option', () => {
11+
describe('"execute" option', () => {
1212
it('should work with "Boolean" value', async () => {
1313
const compiler = getCompiler(
1414
'./jss/exec/index.js',
@@ -26,7 +26,7 @@ describe('"exec" option', () => {
2626
{
2727
loader: path.resolve(__dirname, '../src'),
2828
options: {
29-
exec: true,
29+
execute: true,
3030
},
3131
},
3232
],
@@ -44,9 +44,9 @@ describe('"exec" option', () => {
4444
expect(getErrors(stats)).toMatchSnapshot('errors');
4545
});
4646

47-
it('should work with "JSS" parser', async () => {
47+
it('should work with "postcss-js" parser', async () => {
4848
const compiler = getCompiler(
49-
'./jss/index.js',
49+
'./jss/postcss-js/index.js',
5050
{},
5151
{
5252
module: {
@@ -64,6 +64,7 @@ describe('"exec" option', () => {
6464
postcssOptions: {
6565
parser: 'postcss-js',
6666
},
67+
execute: true,
6768
},
6869
},
6970
],
File renamed without changes.
File renamed without changes.

test/validate-options.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getCompiler, compile } from './helpers/index';
55

66
describe('validate options', () => {
77
const tests = {
8-
exec: {
8+
execute: {
99
success: [false],
1010
failure: [1, 'test', /test/, [], {}, { foo: 'bar' }],
1111
},

0 commit comments

Comments
 (0)