Skip to content

Commit 677c2fe

Browse files
refactor: removed inline value for the sourceMap option (#454)
BREAKING CHANGE: the `inline` value was removed for the `sourceMap` option, please use `{ map: { inline: true, annotation: false } }` to achieve this
1 parent d8d84f7 commit 677c2fe

9 files changed

+62
-116
lines changed

README.md

+8-47
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ module.exports = {
118118
| [`exec`](#exec) | `{Boolean}` | `undefined` | Enable PostCSS Parser support in `CSS-in-JS` |
119119
| [`config`](#config) | `{String\|Object\|Boolean}` | `undefined` | Set `postcss.config.js` config path && `ctx` |
120120
| [`postcssOptions`](#postcssOptions) | `{Object}` | `defaults values for Postcss.process` | Set Postcss.process options and postcss plugins |
121-
| [`sourceMap`](#sourcemap) | `{String\|Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
121+
| [`sourceMap`](#sourcemap) | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
122122

123123
### `Exec`
124124

@@ -649,70 +649,31 @@ module.exports = {
649649

650650
### `SourceMap`
651651

652-
Type: `Boolean|String`
653-
Default: `compiler.devtool`
654-
655-
By default generation of source maps depends on the devtool option.
656-
All values enable source map generation except eval and false value.
657-
In most cases this option should be discouraged.
658-
If need `postcss-loader` to generate an inline map, use the `inline` value.
659-
`postcss-loader` will use the previous source map given by other loaders and update it accordingly, if no previous loader is applied before `postcss-loader`, the loader will generate a source map for you.
660-
661-
**webpack.config.js**
662-
663-
```js
664-
module.exports = {
665-
devtool: 'source-map',
666-
module: {
667-
rules: [
668-
{
669-
test: /\.css$/i,
670-
use: [
671-
{ loader: 'style-loader', options: { sourceMap: true } },
672-
{ loader: 'css-loader' },
673-
{ loader: 'postcss-loader' },
674-
{ loader: 'sass-loader' },
675-
],
676-
},
677-
],
678-
},
679-
};
680-
```
681-
682-
#### `'inline'`
652+
Type: `Boolean`
653+
Default: depends on the `compiler.devtool` value
683654

684-
You can set the `sourceMap: 'inline'` option to inline the source map
685-
within the CSS directly as an annotation comment.
655+
By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option. All values enable source map generation except `eval` and `false` value.
686656

687657
**webpack.config.js**
688658

689659
```js
690660
module.exports = {
691-
devtool: 'source-map',
692661
module: {
693662
rules: [
694663
{
695664
test: /\.css$/i,
696665
use: [
697-
{ loader: 'style-loader', options: { sourceMap: true } },
698-
{ loader: 'css-loader' },
699-
{ loader: 'postcss-loader', options: { sourceMap: 'inline' } },
700-
{ loader: 'sass-loader' },
666+
{ loader: 'style-loader' },
667+
{ loader: 'css-loader', options: { sourceMap: true } },
668+
{ loader: 'postcss-loader', options: { sourceMap: true } },
669+
{ loader: 'sass-loader', options: { sourceMap: true } },
701670
],
702671
},
703672
],
704673
},
705674
};
706675
```
707676

708-
```css
709-
.class {
710-
color: red;
711-
}
712-
713-
/*# sourceMappingURL=data:application/json;base64, ... */
714-
```
715-
716677
<h2 align="center">Examples</h2>
717678

718679
### `Stylelint`

src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ export default async function loader(content, sourceMap, meta = {}) {
103103
}
104104

105105
if (useSourceMap) {
106-
processOptions.map =
107-
options.sourceMap === 'inline'
108-
? { inline: true, annotation: false }
109-
: { inline: false, annotation: false };
106+
processOptions.map = { inline: false, annotation: false };
107+
// options.sourceMap === 'inline'
108+
// ? { inline: true, annotation: false }
109+
// : { inline: false, annotation: false };
110110

111111
if (sourceMap) {
112112
const sourceMapNormalized = normalizeSourceMap(sourceMap);

src/options.json

+1-8
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,7 @@
9595
},
9696
"sourceMap": {
9797
"description": "Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)",
98-
"anyOf": [
99-
{
100-
"type": "string"
101-
},
102-
{
103-
"type": "boolean"
104-
}
105-
]
98+
"type": "boolean"
10699
}
107100
},
108101
"additionalProperties": true

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

+14-24
Original file line numberDiff line numberDiff line change
@@ -248,40 +248,30 @@ exports[`validate options should throw an error on the "postcssOptions" option w
248248
249249
exports[`validate options should throw an error on the "sourceMap" option with "/test/" value 1`] = `
250250
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
251-
- options.sourceMap should be one of these:
252-
string | boolean
253-
-> Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)
254-
Details:
255-
* options.sourceMap should be a string.
256-
* options.sourceMap should be a boolean."
251+
- options.sourceMap should be a boolean.
252+
-> Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)"
257253
`;
258254
259255
exports[`validate options should throw an error on the "sourceMap" option with "[]" value 1`] = `
260256
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
261-
- options.sourceMap should be one of these:
262-
string | boolean
263-
-> Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)
264-
Details:
265-
* options.sourceMap should be a string.
266-
* options.sourceMap should be a boolean."
257+
- options.sourceMap should be a boolean.
258+
-> Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)"
267259
`;
268260
269261
exports[`validate options should throw an error on the "sourceMap" option with "{}" value 1`] = `
270262
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
271-
- options.sourceMap should be one of these:
272-
string | boolean
273-
-> Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)
274-
Details:
275-
* options.sourceMap should be a string.
276-
* options.sourceMap should be a boolean."
263+
- options.sourceMap should be a boolean.
264+
-> Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)"
277265
`;
278266
279267
exports[`validate options should throw an error on the "sourceMap" option with "1" value 1`] = `
280268
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
281-
- options.sourceMap should be one of these:
282-
string | boolean
283-
-> Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)
284-
Details:
285-
* options.sourceMap should be a string.
286-
* options.sourceMap should be a boolean."
269+
- options.sourceMap should be a boolean.
270+
-> Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)"
271+
`;
272+
273+
exports[`validate options should throw an error on the "sourceMap" option with "something" value 1`] = `
274+
"Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
275+
- options.sourceMap should be a boolean.
276+
-> Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)"
287277
`;

test/options/__snapshots__/postcssOptins.test.js.snap

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Postcss options should work "from", "to" and "map" postcssOptions: css 1`] = `
3+
exports[`"postcssOptions" option should work the the "map" option and generate inlined source maps: css 1`] = `
4+
"a { color: black }
5+
6+
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFJLGFBQWEiLCJmaWxlIjoic3R5bGUuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYSB7IGNvbG9yOiBibGFjayB9XG4iXX0= */"
7+
`;
8+
9+
exports[`"postcssOptions" option should work the the "map" option and generate inlined source maps: errors 1`] = `Array []`;
10+
11+
exports[`"postcssOptions" option should work the the "map" option and generate inlined source maps: map 1`] = `undefined`;
12+
13+
exports[`"postcssOptions" option should work the the "map" option and generate inlined source maps: warnings 1`] = `Array []`;
14+
15+
exports[`"postcssOptions" option should work with "from", "to" and "map" options: css 1`] = `
416
"a { color: black }
517
"
618
`;
719

8-
exports[`Postcss options should work "from", "to" and "map" postcssOptions: errors 1`] = `Array []`;
20+
exports[`"postcssOptions" option should work with "from", "to" and "map" options: errors 1`] = `Array []`;
921

10-
exports[`Postcss options should work "from", "to" and "map" postcssOptions: map 1`] = `
22+
exports[`"postcssOptions" option should work with "from", "to" and "map" options: map 1`] = `
1123
Object {
1224
"file": "x",
1325
"mappings": "AAAA,IAAI,aAAa",
@@ -23,4 +35,4 @@ Object {
2335
}
2436
`;
2537

26-
exports[`Postcss options should work "from", "to" and "map" postcssOptions: warnings 1`] = `Array []`;
38+
exports[`"postcssOptions" option should work with "from", "to" and "map" options: warnings 1`] = `Array []`;

test/options/__snapshots__/sourceMap.test.js.snap

-10
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ Object {
4848

4949
exports[`Options Sourcemap should work Sourcemap - {Boolean}: warnings 1`] = `Array []`;
5050

51-
exports[`Options Sourcemap should work Sourcemap - {String}: css 1`] = `
52-
"a { color: black }
53-
54-
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFJLGFBQWEiLCJmaWxlIjoic3R5bGUuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYSB7IGNvbG9yOiBibGFjayB9XG4iXX0= */"
55-
`;
56-
57-
exports[`Options Sourcemap should work Sourcemap - {String}: errors 1`] = `Array []`;
58-
59-
exports[`Options Sourcemap should work Sourcemap - {String}: warnings 1`] = `Array []`;
60-
6151
exports[`Options Sourcemap should work disable Sourcemap - {Boolean}: css 1`] = `
6252
"a { color: black }
6353
"

test/options/postcssOptins.test.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
getWarnings,
77
} from '../helpers/index';
88

9-
describe('Postcss options', () => {
10-
it.only('should work "from", "to" and "map" postcssOptions', async () => {
9+
describe('"postcssOptions" option', () => {
10+
it('should work with "from", "to" and "map" options', async () => {
1111
const compiler = getCompiler('./css/index.js', {
1212
postcssOptions: {
1313
from: '/test/from.css',
@@ -38,4 +38,19 @@ describe('Postcss options', () => {
3838
expect(getWarnings(stats)).toMatchSnapshot('warnings');
3939
expect(getErrors(stats)).toMatchSnapshot('errors');
4040
});
41+
42+
it('should work the the "map" option and generate inlined source maps', async () => {
43+
const compiler = getCompiler('./css/index.js', {
44+
postcssOptions: {
45+
map: { inline: true, annotation: false },
46+
},
47+
});
48+
const stats = await compile(compiler);
49+
const codeFromBundle = getCodeFromBundle('style.css', stats);
50+
51+
expect(codeFromBundle.css).toMatchSnapshot('css');
52+
expect(codeFromBundle.map).toMatchSnapshot('map');
53+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
54+
expect(getErrors(stats)).toMatchSnapshot('errors');
55+
});
4156
});

test/options/sourceMap.test.js

-15
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,6 @@ describe('Options Sourcemap', () => {
4545
expect(getErrors(stats)).toMatchSnapshot('errors');
4646
});
4747

48-
it('should work Sourcemap - {String}', async () => {
49-
const compiler = getCompiler('./css/index.js', { sourceMap: 'inline' });
50-
const stats = await compile(compiler);
51-
52-
const codeFromBundle = getCodeFromBundle('style.css', stats);
53-
54-
expect(codeFromBundle.map).toBeUndefined();
55-
expect(codeFromBundle.css).toMatchSnapshot('css');
56-
expect(getWarnings(stats)).toMatchSnapshot('warnings');
57-
expect(getErrors(stats)).toMatchSnapshot('errors');
58-
});
59-
6048
it('should work with prev sourceMap (sass-loader)', async () => {
6149
const compiler = getCompiler(
6250
'./scss/index.js',
@@ -118,9 +106,6 @@ describe('Options Sourcemap', () => {
118106
},
119107
{
120108
loader: path.resolve(__dirname, '../../src'),
121-
options: {
122-
config: false,
123-
},
124109
},
125110
{
126111
loader: 'less-loader',

test/validate-options.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ describe('validate options', () => {
6161
],
6262
},
6363
sourceMap: {
64-
success: ['source-map', true],
65-
failure: [1, /test/, [], {}],
64+
success: [true, false],
65+
failure: [1, /test/, [], {}, 'something'],
6666
},
6767
};
6868

0 commit comments

Comments
 (0)