Skip to content

Commit

Permalink
Correctly emit warnings for unnecessary PostCSS plugins in package.js…
Browse files Browse the repository at this point in the history
…on (#8024)
  • Loading branch information
mischnic committed May 1, 2022
1 parent 27034ec commit 0c9d93f
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 2 deletions.
@@ -0,0 +1,3 @@
.foo {
color: #fff;
}
@@ -0,0 +1,5 @@
const map = require('./foo.css');

module.exports = {
foo: map.foo,
};
@@ -0,0 +1,3 @@
body {
background: blue;
}
@@ -0,0 +1,6 @@
require('./index.css');
var foo = require('./foo');

module.exports = function () {
return foo.foo;
};
@@ -0,0 +1,10 @@
{
"postcss": {
"modules": true,
"plugins": {
"autoprefixer": {
"grid": true
}
}
}
}
Empty file.
31 changes: 31 additions & 0 deletions packages/core/integration-tests/test/postcss.js
Expand Up @@ -46,6 +46,37 @@ describe('postcss', () => {
assert(css.includes(`.${cssClass}`));
});

it('should build successfully with only postcss-modules config in package.json', async () => {
let b = await bundle(
path.join(
__dirname,
'/integration/postcss-modules-config-package/index.js',
),
);

assertBundles(b, [
{
name: 'index.js',
assets: ['foo.css', 'foo.js', 'index.css', 'index.js'],
},
{
name: 'index.css',
assets: ['foo.css', 'index.css'],
},
]);

let output = await run(b);
assert.equal(typeof output, 'function');

let value = output();
assert(/foo_[0-9a-z]/.test(value));

let cssClass = value.match(/(foo_[0-9a-z])/)[1];

let css = await outputFS.readFile(path.join(distDir, 'index.css'), 'utf8');
assert(css.includes(`.${cssClass}`));
});

it('should support transforming with postcss twice with the same result', async () => {
let b = await bundle(
path.join(__dirname, '/integration/postcss-plugins/index.js'),
Expand Down
3 changes: 3 additions & 0 deletions packages/transformers/postcss/src/PostCSSTransformer.js
Expand Up @@ -101,6 +101,9 @@ export default (new Transformer({
configKey = '/plugins/postcss-modules';
hint = md`Remove the "postcss-modules" plugin from __${filename}__`;
}
if (filename === 'package.json') {
configKey = `/postcss${configKey}`;
}

let hints = [
'Enable the "cssModules" option for "@parcel/transformer-css" in your package.json',
Expand Down
6 changes: 4 additions & 2 deletions packages/transformers/postcss/src/loadConfig.js
Expand Up @@ -79,9 +79,10 @@ async function configHydrator(
);
if (redundantPlugins.length > 0) {
let filename = path.basename(resolveFrom);
let isPackageJson = filename === 'package.json';
let message;
let hints = [];
if (redundantPlugins.length === pluginArray.length) {
if (!isPackageJson && redundantPlugins.length === pluginArray.length) {
message = md`Parcel includes CSS transpilation and vendor prefixing by default. PostCSS config __${filename}__ contains only redundant plugins. Deleting it may significantly improve build performance.`;
hints.push(md`Delete __${filename}__`);
} else {
Expand All @@ -96,6 +97,7 @@ async function configHydrator(
let codeFrames;
if (path.extname(filename) !== '.js') {
let contents = await options.inputFS.readFile(resolveFrom, 'utf8');
let prefix = isPackageJson ? '/postcss' : '';
codeFrames = [
{
language: 'json',
Expand All @@ -104,7 +106,7 @@ async function configHydrator(
codeHighlights: generateJSONCodeHighlights(
contents,
redundantPlugins.map(plugin => ({
key: `/plugins/${plugin}`,
key: `${prefix}/plugins/${plugin}`,
type: 'key',
})),
),
Expand Down

0 comments on commit 0c9d93f

Please sign in to comment.