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

Correctly emit warnings for unnecessary PostCSS plugins in package.json #8024

Merged
merged 3 commits into from May 1, 2022
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
@@ -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
}
}
}
}
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