Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept semver compatible postcss AST #1049

Merged
merged 2 commits into from Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 5 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -54,7 +54,8 @@
"postcss-modules-scope": "^2.1.1",
"postcss-modules-values": "^3.0.0",
"postcss-value-parser": "^4.0.2",
"schema-utils": "^2.6.0"
"schema-utils": "^2.6.0",
"semver": "^6.3.0"
},
"devDependencies": {
"@babel/cli": "^7.7.7",
Expand Down
24 changes: 14 additions & 10 deletions src/index.js
Expand Up @@ -2,24 +2,24 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import validateOptions from 'schema-utils';
import { getOptions, isUrlRequest } from 'loader-utils';
import postcss from 'postcss';
import postcssPkg from 'postcss/package.json';
import validateOptions from 'schema-utils';
import { satisfies } from 'semver';

import { getOptions, isUrlRequest } from 'loader-utils';

import CssSyntaxError from './CssSyntaxError';
import Warning from './Warning';
import schema from './options.json';
import { importParser, icssParser, urlParser } from './plugins';
import { icssParser, importParser, urlParser } from './plugins';
import {
normalizeSourceMap,
getModulesPlugins,
getExportCode,
getFilter,
getImportCode,
getModuleCode,
getExportCode,
getModulesPlugins,
normalizeSourceMap,
} from './utils';
import Warning from './Warning';
import CssSyntaxError from './CssSyntaxError';

export default function loader(content, map, meta) {
const options = getOptions(this) || {};
Expand Down Expand Up @@ -63,7 +63,11 @@ export default function loader(content, map, meta) {
if (meta) {
const { ast } = meta;

if (ast && ast.type === 'postcss' && ast.version === postcssPkg.version) {
if (
ast &&
ast.type === 'postcss' &&
satisfies(ast.version, `^${postcssPkg.version}`)
) {
// eslint-disable-next-line no-param-reassign
content = ast.root;
}
Expand Down
27 changes: 27 additions & 0 deletions test/helpers/ast-loader.js
@@ -0,0 +1,27 @@
import postcss from 'postcss';
import postcssPresetEnv from 'postcss-preset-env';
import postcssPkg from 'postcss/package.json';
import semver from 'semver';

const incomingVersion = semver.inc(postcssPkg.version, 'minor');

export default function astLoader(content) {
const callback = this.async();

const { spy = jest.fn() } = this.query;

postcss([postcssPresetEnv({ stage: 0 })])
.process(content)
.then(({ css, map, root, messages }) => {
const ast = {
type: 'postcss',
version: incomingVersion,
};

Object.defineProperty(ast, 'root', {
get: spy.mockReturnValue(root),
});

callback(null, css, map, { ast, messages });
});
}
9 changes: 5 additions & 4 deletions test/loader.test.js
@@ -1,7 +1,5 @@
import path from 'path';

import postcssPresetEnv from 'postcss-preset-env';

import { version } from 'webpack';

import {
Expand Down Expand Up @@ -129,6 +127,7 @@ describe('loader', () => {
});

it('should reuse `ast` from "postcss-loader"', async () => {
const spy = jest.fn();
const compiler = getCompiler(
'./postcss-present-env/source.js',
{},
Expand All @@ -143,8 +142,8 @@ describe('loader', () => {
options: { importLoaders: 1 },
},
{
loader: 'postcss-loader',
options: { plugins: () => [postcssPresetEnv({ stage: 0 })] },
loader: require.resolve('./helpers/ast-loader'),
options: { spy },
},
],
},
Expand All @@ -159,6 +158,8 @@ describe('loader', () => {
);
const stats = await compile(compiler);

expect(spy).toHaveBeenCalledTimes(1);

expect(
getModuleSource('./postcss-present-env/source.css', stats)
).toMatchSnapshot('module');
Expand Down