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 1 commit
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
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": "^7.1.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try and find one. Ya'll know that node 8 is End of Life right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still support node@8 https://github.com/webpack-contrib/css-loader/blob/master/package.json#L16, In the near future there will be a release that will throw out support node@8, but right now it is breaking change, so we should avoid it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, just wanted to make sure ya'll knew.

},
"devDependencies": {
"@babel/cli": "^7.7.7",
Expand Down
26 changes: 15 additions & 11 deletions src/index.js
@@ -1,25 +1,25 @@
import { getOptions, isUrlRequest } from 'loader-utils';
import postcss from 'postcss';
import postcssPkg from 'postcss/package.json';
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import validateOptions from 'schema-utils';
import postcss from 'postcss';
import postcssPkg from 'postcss/package.json';

import { getOptions, isUrlRequest } from 'loader-utils';
import satisfies from 'semver/functions/satisfies';
jquense marked this conversation as resolved.
Show resolved Hide resolved

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 semverInc from 'semver/functions/inc';

const incomingVersion = semverInc(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