Skip to content

Commit 84a9c46

Browse files
feat: partial compatibility with postcss-cli (#498)
1 parent 0e7be43 commit 84a9c46

File tree

6 files changed

+107
-2
lines changed

6 files changed

+107
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ module.exports = (api) => {
400400
// `api.file` - path to the file
401401
// `api.mode` - `mode` value of webpack, please read https://webpack.js.org/configuration/mode/
402402
// `api.webpackLoaderContext` - loader context for complex use cases
403+
// `api.env` - alias `api.mode` for compatibility with `postcss-cli`
404+
// `api.options` - the `postcssOptions` options
403405

404406
if (/\.sss$/.test(api.file)) {
405407
return {

src/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ export default async function loader(content, sourceMap, meta) {
5050

5151
if (configOption) {
5252
try {
53-
loadedConfig = await loadConfig(this, configOption);
53+
loadedConfig = await loadConfig(
54+
this,
55+
configOption,
56+
options.postcssOptions
57+
);
5458
} catch (error) {
5559
callback(error);
5660

src/utils.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function exec(code, loaderContext) {
3131
return module.exports;
3232
}
3333

34-
async function loadConfig(loaderContext, config) {
34+
async function loadConfig(loaderContext, config, postcssOptions) {
3535
const searchPath =
3636
typeof config === 'string'
3737
? path.resolve(config)
@@ -75,6 +75,9 @@ async function loadConfig(loaderContext, config) {
7575
file: loaderContext.resourcePath,
7676
// For complex use
7777
webpackLoaderContext: loaderContext,
78+
// Partial compatibility with `postcss-cli`
79+
env: loaderContext.mode,
80+
options: postcssOptions || {},
7881
};
7982

8083
result.config = result.config(api);

test/__snapshots__/postcssOptins.test.js.snap

+53
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,59 @@ exports[`"postcssOptions" option should work "Function" value: errors 1`] = `Arr
213213

214214
exports[`"postcssOptions" option should work "Function" value: warnings 1`] = `Array []`;
215215

216+
exports[`"postcssOptions" option should work and provide API for the configuration: css 1`] = `
217+
"a {
218+
color: black;
219+
}
220+
221+
a {
222+
color: red;
223+
}
224+
225+
a {
226+
color: green;
227+
}
228+
229+
a {
230+
color: blue;
231+
}
232+
233+
.class {
234+
border-top-color: blue;
235+
border-right-color: blue;
236+
border-left-color: blue;
237+
background-color: #fafafa;
238+
}
239+
240+
.class-foo {
241+
-z-border-color: blue blue *;
242+
-z-color: * #fafafa;
243+
}
244+
245+
.phone {
246+
&_title {
247+
width: 500px;
248+
249+
@media (max-width: 500px) {
250+
width: auto;
251+
}
252+
253+
body.is_dark & {
254+
color: white;
255+
}
256+
}
257+
258+
img {
259+
display: block;
260+
}
261+
}
262+
"
263+
`;
264+
265+
exports[`"postcssOptions" option should work and provide API for the configuration: errors 1`] = `Array []`;
266+
267+
exports[`"postcssOptions" option should work and provide API for the configuration: warnings 1`] = `Array []`;
268+
216269
exports[`"postcssOptions" option should work with "from", "to" and "map" options (absolute paths): css 1`] = `
217270
"a {
218271
color: black;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = function (api) {
2+
if (!api.mode) {
3+
throw new Error(`Failed, no ${api.mode} API`);
4+
}
5+
6+
if (!api.file) {
7+
throw new Error(`Failed, no ${api.file} API`);
8+
}
9+
10+
if (!api.webpackLoaderContext) {
11+
throw new Error(`Failed, no ${api.webpackLoaderContext} API`);
12+
}
13+
14+
if (!api.env) {
15+
throw new Error(`Failed, no ${api.env} API`);
16+
}
17+
18+
if (!api.options) {
19+
throw new Error(`Failed, no ${api.options} API`);
20+
}
21+
22+
return {
23+
plugins: [['postcss-short', { prefix: 'x' }]],
24+
}
25+
};

test/postcssOptins.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -776,4 +776,22 @@ describe('"postcssOptions" option', () => {
776776
expect(getWarnings(stats)).toMatchSnapshot('warnings');
777777
expect(getErrors(stats)).toMatchSnapshot('errors');
778778
});
779+
780+
it('should work and provide API for the configuration', async () => {
781+
const compiler = getCompiler('./css/index.js', {
782+
postcssOptions: {
783+
config: path.resolve(
784+
__dirname,
785+
'./fixtures/config-scope/api/postcss.config.js'
786+
),
787+
},
788+
});
789+
const stats = await compile(compiler);
790+
791+
const codeFromBundle = getCodeFromBundle('style.css', stats);
792+
793+
expect(codeFromBundle.css).toMatchSnapshot('css');
794+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
795+
expect(getErrors(stats)).toMatchSnapshot('errors');
796+
});
779797
});

0 commit comments

Comments
 (0)