Skip to content

Commit

Permalink
support postcss cjs format
Browse files Browse the repository at this point in the history
  • Loading branch information
beaussan committed Oct 18, 2023
1 parent 773d341 commit 00495dc
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions fixtures/plugins/postcss-cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@fixtures/postcss-cjs",
"scripts": {
"postcss": "postcss"
},
"devDependencies": {
"postcss": "*"
},
"postcss": {
"plugins": [
"autoprefixer"
]
}
}
3 changes: 3 additions & 0 deletions fixtures/plugins/postcss-cjs/postcss.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: [require('autoprefixer')],
};
2 changes: 1 addition & 1 deletion src/plugins/postcss/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ or `devDependencies`:
```json
{
"postcss": {
"config": ["postcss.config.js", "postcss.config.json", "package.json"]
"config": ["postcss.config.{cjs,js}", "postcss.config.json", "package.json"]
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/postcss/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const ENABLERS = ['postcss', 'next'];

export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

export const CONFIG_FILE_PATTERNS = ['postcss.config.js', 'postcss.config.json', 'package.json'];
export const CONFIG_FILE_PATTERNS = ['postcss.config.{cjs,js}', 'postcss.config.json', 'package.json'];

const findPostCSSDependencies: GenericPluginCallback = async (configFilePath, options) => {
const { manifest, isProduction } = options;
Expand Down
40 changes: 40 additions & 0 deletions test/plugins/postcss-cjs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { main } from '../../src/index.js';
import * as postcss from '../../src/plugins/postcss/index.js';
import { resolve, join } from '../../src/util/path.js';
import baseArguments from '../helpers/baseArguments.js';
import baseCounters from '../helpers/baseCounters.js';
import { getManifest } from '../helpers/index.js';

const cwd = resolve('fixtures/plugins/postcss-cjs');
const manifestFilePath = join(cwd, 'package.json');
const manifest = getManifest(cwd);

test('Find dependencies in PostCSS configuration (package.json)', async () => {
const dependencies = await postcss.findDependencies(manifestFilePath, { manifest });
assert.deepEqual(dependencies, ['autoprefixer']);
});

test('Find dependencies in PostCSS configuration (postcss.config.cjs)', async () => {
const configFilePath = join(cwd, 'postcss.config.cjs');
const dependencies = await postcss.findDependencies(configFilePath, { manifest });
assert.deepEqual(dependencies, []);
});

test('Find dependencies in PostCSS configuration (postcss.config.cjs function)', async () => {
const { issues, counters } = await main({
...baseArguments,
cwd,
});

assert(issues.unlisted['package.json']['autoprefixer']);
assert(issues.unlisted['postcss.config.cjs']['autoprefixer']);

assert.deepEqual(counters, {
...baseCounters,
unlisted: 2,
processed: 1,
total: 1,
});
});

0 comments on commit 00495dc

Please sign in to comment.