Skip to content

Commit 14c4faa

Browse files
authored
feat: accept semver compatible postcss AST (#1049)
1 parent 229d36a commit 14c4faa

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

package-lock.json

+5-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"postcss-modules-scope": "^2.1.1",
5555
"postcss-modules-values": "^3.0.0",
5656
"postcss-value-parser": "^4.0.2",
57-
"schema-utils": "^2.6.0"
57+
"schema-utils": "^2.6.0",
58+
"semver": "^6.3.0"
5859
},
5960
"devDependencies": {
6061
"@babel/cli": "^7.7.7",

src/index.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
import validateOptions from 'schema-utils';
5+
import { getOptions, isUrlRequest } from 'loader-utils';
66
import postcss from 'postcss';
77
import postcssPkg from 'postcss/package.json';
8+
import validateOptions from 'schema-utils';
9+
import { satisfies } from 'semver';
810

9-
import { getOptions, isUrlRequest } from 'loader-utils';
10-
11+
import CssSyntaxError from './CssSyntaxError';
12+
import Warning from './Warning';
1113
import schema from './options.json';
12-
import { importParser, icssParser, urlParser } from './plugins';
14+
import { icssParser, importParser, urlParser } from './plugins';
1315
import {
14-
normalizeSourceMap,
15-
getModulesPlugins,
16+
getExportCode,
1617
getFilter,
1718
getImportCode,
1819
getModuleCode,
19-
getExportCode,
20+
getModulesPlugins,
21+
normalizeSourceMap,
2022
} from './utils';
21-
import Warning from './Warning';
22-
import CssSyntaxError from './CssSyntaxError';
2323

2424
export default function loader(content, map, meta) {
2525
const options = getOptions(this) || {};
@@ -63,7 +63,11 @@ export default function loader(content, map, meta) {
6363
if (meta) {
6464
const { ast } = meta;
6565

66-
if (ast && ast.type === 'postcss' && ast.version === postcssPkg.version) {
66+
if (
67+
ast &&
68+
ast.type === 'postcss' &&
69+
satisfies(ast.version, `^${postcssPkg.version}`)
70+
) {
6771
// eslint-disable-next-line no-param-reassign
6872
content = ast.root;
6973
}

test/helpers/ast-loader.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import postcss from 'postcss';
2+
import postcssPresetEnv from 'postcss-preset-env';
3+
import postcssPkg from 'postcss/package.json';
4+
import semver from 'semver';
5+
6+
const incomingVersion = semver.inc(postcssPkg.version, 'minor');
7+
8+
export default function astLoader(content) {
9+
const callback = this.async();
10+
11+
const { spy = jest.fn() } = this.query;
12+
13+
postcss([postcssPresetEnv({ stage: 0 })])
14+
.process(content)
15+
.then(({ css, map, root, messages }) => {
16+
const ast = {
17+
type: 'postcss',
18+
version: incomingVersion,
19+
};
20+
21+
Object.defineProperty(ast, 'root', {
22+
get: spy.mockReturnValue(root),
23+
});
24+
25+
callback(null, css, map, { ast, messages });
26+
});
27+
}

test/loader.test.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import path from 'path';
22

3-
import postcssPresetEnv from 'postcss-preset-env';
4-
53
import { version } from 'webpack';
64

75
import {
@@ -129,6 +127,7 @@ describe('loader', () => {
129127
});
130128

131129
it('should reuse `ast` from "postcss-loader"', async () => {
130+
const spy = jest.fn();
132131
const compiler = getCompiler(
133132
'./postcss-present-env/source.js',
134133
{},
@@ -143,8 +142,8 @@ describe('loader', () => {
143142
options: { importLoaders: 1 },
144143
},
145144
{
146-
loader: 'postcss-loader',
147-
options: { plugins: () => [postcssPresetEnv({ stage: 0 })] },
145+
loader: require.resolve('./helpers/ast-loader'),
146+
options: { spy },
148147
},
149148
],
150149
},
@@ -159,6 +158,8 @@ describe('loader', () => {
159158
);
160159
const stats = await compile(compiler);
161160

161+
expect(spy).toHaveBeenCalledTimes(1);
162+
162163
expect(
163164
getModuleSource('./postcss-present-env/source.css', stats)
164165
).toMatchSnapshot('module');

0 commit comments

Comments
 (0)