From 38ebe08aaf96ed270b554587b68fd6f80320979a Mon Sep 17 00:00:00 2001 From: Alexander Krasnoyarov <evilebottnawi@users.noreply.github.com> Date: Mon, 7 Sep 2020 13:50:08 +0300 Subject: [PATCH] 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` --- README.md | 10 ++++---- src/index.js | 4 ++-- src/options.json | 2 +- src/utils.js | 9 +------ test/__snapshots__/exec.test.js.snap | 21 ---------------- test/__snapshots__/execute.test.js.snap | 21 ++++++++++++++++ .../validate-options.test.js.snap | 24 +++++++++---------- test/{exec.test.js => execute.test.js} | 9 +++---- test/fixtures/jss/{ => postcss-js}/index.js | 0 test/fixtures/jss/{ => postcss-js}/style.js | 0 test/validate-options.test.js | 2 +- 11 files changed, 48 insertions(+), 54 deletions(-) delete mode 100644 test/__snapshots__/exec.test.js.snap create mode 100644 test/__snapshots__/execute.test.js.snap rename test/{exec.test.js => execute.test.js} (90%) rename test/fixtures/jss/{ => postcss-js}/index.js (100%) rename test/fixtures/jss/{ => postcss-js}/style.js (100%) diff --git a/README.md b/README.md index 8b2ae681..64ca370f 100644 --- a/README.md +++ b/README.md @@ -124,16 +124,16 @@ And run `webpack` via your preferred method. | Name | Type | Default | Description | | :---------------------------------: | :------------------: | :-----------------------------------: | :------------------------------------------- | -| [`exec`](#exec) | `{Boolean}` | `undefined` | Enable PostCSS Parser support in `CSS-in-JS` | +| [`execute`](#execute) | `{Boolean}` | `undefined` | Enable PostCSS Parser support in `CSS-in-JS` | | [`postcssOptions`](#postcssOptions) | `{Object\/Function}` | `defaults values for Postcss.process` | Set `postcss` options and plugins | | [`sourceMap`](#sourcemap) | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps | -### `exec` +### `execute` Type: `Boolean` Default: `undefined` -If you use JS styles without the [`postcss-js`][postcss-js] parser, add the `exec` option. +If you use JS styles the [`postcss-js`](https://github.com/postcss/postcss-js) parser, add the `execute` option. **webpack.config.js** @@ -147,13 +147,12 @@ module.exports = { 'style-loader', { loader: 'css-loader', - options: { importLoaders: 1 }, }, { loader: 'postcss-loader', options: { postcssOptions: { - parser: 'sugarss', + parser: 'postcss-js', }, exec: true, }, @@ -867,6 +866,7 @@ module.exports = { postcssOptions: { parser: 'postcss-js', }, + execute: true, }, }, 'babel-loader', diff --git a/src/index.js b/src/index.js index 4bbd2cd1..09e26fec 100644 --- a/src/index.js +++ b/src/index.js @@ -59,13 +59,13 @@ export default async function loader(content, sourceMap) { ? options.sourceMap : this.sourceMap; - const { plugins, processOptions, needExecute } = getPostcssOptions( + const { plugins, processOptions } = getPostcssOptions( this, loadedConfig, options.postcssOptions ); - if (options.exec || needExecute) { + if (options.execute) { // eslint-disable-next-line no-param-reassign content = exec(content, this); } diff --git a/src/options.json b/src/options.json index f86c5b96..2a8a44c0 100644 --- a/src/options.json +++ b/src/options.json @@ -71,7 +71,7 @@ } ] }, - "exec": { + "execute": { "description": "Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)", "type": "boolean" }, diff --git a/src/utils.js b/src/utils.js index 56be7608..61b1af1c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -200,14 +200,7 @@ function getPostcssOptions(loaderContext, config, postcssOptions = {}) { ...processOptionsFromOptions, }; - let needExecute = false; - if (typeof processOptions.parser === 'string') { - // TODO respect the `syntax` option too or remove this options - if (processOptions.parser === 'postcss-js') { - needExecute = true; - } - try { // eslint-disable-next-line import/no-dynamic-require, global-require processOptions.parser = require(processOptions.parser); @@ -246,7 +239,7 @@ function getPostcssOptions(loaderContext, config, postcssOptions = {}) { } } - return { plugins, processOptions, needExecute }; + return { plugins, processOptions }; } const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i; diff --git a/test/__snapshots__/exec.test.js.snap b/test/__snapshots__/exec.test.js.snap deleted file mode 100644 index 7767a494..00000000 --- a/test/__snapshots__/exec.test.js.snap +++ /dev/null @@ -1,21 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`"exec" option should work with "Boolean" value: css 1`] = ` -"a { - color: green -}" -`; - -exports[`"exec" option should work with "Boolean" value: errors 1`] = `Array []`; - -exports[`"exec" option should work with "Boolean" value: warnings 1`] = `Array []`; - -exports[`"exec" option should work with "JSS" parser: css 1`] = ` -"a { - color: yellow -}" -`; - -exports[`"exec" option should work with "JSS" parser: errors 1`] = `Array []`; - -exports[`"exec" option should work with "JSS" parser: warnings 1`] = `Array []`; diff --git a/test/__snapshots__/execute.test.js.snap b/test/__snapshots__/execute.test.js.snap new file mode 100644 index 00000000..e0a368bb --- /dev/null +++ b/test/__snapshots__/execute.test.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`"execute" option should work with "Boolean" value: css 1`] = ` +"a { + color: green +}" +`; + +exports[`"execute" option should work with "Boolean" value: errors 1`] = `Array []`; + +exports[`"execute" option should work with "Boolean" value: warnings 1`] = `Array []`; + +exports[`"execute" option should work with "postcss-js" parser: css 1`] = ` +"a { + color: yellow +}" +`; + +exports[`"execute" option should work with "postcss-js" parser: errors 1`] = `Array []`; + +exports[`"execute" option should work with "postcss-js" parser: warnings 1`] = `Array []`; diff --git a/test/__snapshots__/validate-options.test.js.snap b/test/__snapshots__/validate-options.test.js.snap index 09bd4998..44681d0d 100644 --- a/test/__snapshots__/validate-options.test.js.snap +++ b/test/__snapshots__/validate-options.test.js.snap @@ -1,38 +1,38 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`validate options should throw an error on the "exec" option with "/test/" value 1`] = ` +exports[`validate options should throw an error on the "execute" option with "/test/" value 1`] = ` "Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema. - - options.exec should be a boolean. + - options.execute should be a boolean. -> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)" `; -exports[`validate options should throw an error on the "exec" option with "[]" value 1`] = ` +exports[`validate options should throw an error on the "execute" option with "[]" value 1`] = ` "Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema. - - options.exec should be a boolean. + - options.execute should be a boolean. -> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)" `; -exports[`validate options should throw an error on the "exec" option with "{"foo":"bar"}" value 1`] = ` +exports[`validate options should throw an error on the "execute" option with "{"foo":"bar"}" value 1`] = ` "Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema. - - options.exec should be a boolean. + - options.execute should be a boolean. -> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)" `; -exports[`validate options should throw an error on the "exec" option with "{}" value 1`] = ` +exports[`validate options should throw an error on the "execute" option with "{}" value 1`] = ` "Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema. - - options.exec should be a boolean. + - options.execute should be a boolean. -> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)" `; -exports[`validate options should throw an error on the "exec" option with "1" value 1`] = ` +exports[`validate options should throw an error on the "execute" option with "1" value 1`] = ` "Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema. - - options.exec should be a boolean. + - options.execute should be a boolean. -> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)" `; -exports[`validate options should throw an error on the "exec" option with "test" value 1`] = ` +exports[`validate options should throw an error on the "execute" option with "test" value 1`] = ` "Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema. - - options.exec should be a boolean. + - options.execute should be a boolean. -> Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)" `; diff --git a/test/exec.test.js b/test/execute.test.js similarity index 90% rename from test/exec.test.js rename to test/execute.test.js index 7100f0b5..cd59079e 100644 --- a/test/exec.test.js +++ b/test/execute.test.js @@ -8,7 +8,7 @@ import { getWarnings, } from './helpers'; -describe('"exec" option', () => { +describe('"execute" option', () => { it('should work with "Boolean" value', async () => { const compiler = getCompiler( './jss/exec/index.js', @@ -26,7 +26,7 @@ describe('"exec" option', () => { { loader: path.resolve(__dirname, '../src'), options: { - exec: true, + execute: true, }, }, ], @@ -44,9 +44,9 @@ describe('"exec" option', () => { expect(getErrors(stats)).toMatchSnapshot('errors'); }); - it('should work with "JSS" parser', async () => { + it('should work with "postcss-js" parser', async () => { const compiler = getCompiler( - './jss/index.js', + './jss/postcss-js/index.js', {}, { module: { @@ -64,6 +64,7 @@ describe('"exec" option', () => { postcssOptions: { parser: 'postcss-js', }, + execute: true, }, }, ], diff --git a/test/fixtures/jss/index.js b/test/fixtures/jss/postcss-js/index.js similarity index 100% rename from test/fixtures/jss/index.js rename to test/fixtures/jss/postcss-js/index.js diff --git a/test/fixtures/jss/style.js b/test/fixtures/jss/postcss-js/style.js similarity index 100% rename from test/fixtures/jss/style.js rename to test/fixtures/jss/postcss-js/style.js diff --git a/test/validate-options.test.js b/test/validate-options.test.js index bebe9358..95e63b23 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -5,7 +5,7 @@ import { getCompiler, compile } from './helpers/index'; describe('validate options', () => { const tests = { - exec: { + execute: { success: [false], failure: [1, 'test', /test/, [], {}, { foo: 'bar' }], },