Skip to content

Commit

Permalink
feat: partial compatibility with postcss-cli (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Nov 19, 2020
1 parent 0e7be43 commit 84a9c46
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ module.exports = (api) => {
// `api.file` - path to the file
// `api.mode` - `mode` value of webpack, please read https://webpack.js.org/configuration/mode/
// `api.webpackLoaderContext` - loader context for complex use cases
// `api.env` - alias `api.mode` for compatibility with `postcss-cli`
// `api.options` - the `postcssOptions` options

if (/\.sss$/.test(api.file)) {
return {
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export default async function loader(content, sourceMap, meta) {

if (configOption) {
try {
loadedConfig = await loadConfig(this, configOption);
loadedConfig = await loadConfig(
this,
configOption,
options.postcssOptions
);
} catch (error) {
callback(error);

Expand Down
5 changes: 4 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function exec(code, loaderContext) {
return module.exports;
}

async function loadConfig(loaderContext, config) {
async function loadConfig(loaderContext, config, postcssOptions) {
const searchPath =
typeof config === 'string'
? path.resolve(config)
Expand Down Expand Up @@ -75,6 +75,9 @@ async function loadConfig(loaderContext, config) {
file: loaderContext.resourcePath,
// For complex use
webpackLoaderContext: loaderContext,
// Partial compatibility with `postcss-cli`
env: loaderContext.mode,
options: postcssOptions || {},
};

result.config = result.config(api);
Expand Down
53 changes: 53 additions & 0 deletions test/__snapshots__/postcssOptins.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,59 @@ exports[`"postcssOptions" option should work "Function" value: errors 1`] = `Arr

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

exports[`"postcssOptions" option should work and provide API for the configuration: css 1`] = `
"a {
color: black;
}
a {
color: red;
}
a {
color: green;
}
a {
color: blue;
}
.class {
border-top-color: blue;
border-right-color: blue;
border-left-color: blue;
background-color: #fafafa;
}
.class-foo {
-z-border-color: blue blue *;
-z-color: * #fafafa;
}
.phone {
&_title {
width: 500px;
@media (max-width: 500px) {
width: auto;
}
body.is_dark & {
color: white;
}
}
img {
display: block;
}
}
"
`;

exports[`"postcssOptions" option should work and provide API for the configuration: errors 1`] = `Array []`;

exports[`"postcssOptions" option should work and provide API for the configuration: warnings 1`] = `Array []`;

exports[`"postcssOptions" option should work with "from", "to" and "map" options (absolute paths): css 1`] = `
"a {
color: black;
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/config-scope/api/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = function (api) {
if (!api.mode) {
throw new Error(`Failed, no ${api.mode} API`);
}

if (!api.file) {
throw new Error(`Failed, no ${api.file} API`);
}

if (!api.webpackLoaderContext) {
throw new Error(`Failed, no ${api.webpackLoaderContext} API`);
}

if (!api.env) {
throw new Error(`Failed, no ${api.env} API`);
}

if (!api.options) {
throw new Error(`Failed, no ${api.options} API`);
}

return {
plugins: [['postcss-short', { prefix: 'x' }]],
}
};
18 changes: 18 additions & 0 deletions test/postcssOptins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,4 +776,22 @@ describe('"postcssOptions" option', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should work and provide API for the configuration', async () => {
const compiler = getCompiler('./css/index.js', {
postcssOptions: {
config: path.resolve(
__dirname,
'./fixtures/config-scope/api/postcss.config.js'
),
},
});
const stats = await compile(compiler);

const codeFromBundle = getCodeFromBundle('style.css', stats);

expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});

0 comments on commit 84a9c46

Please sign in to comment.