Skip to content

Commit a458c49

Browse files
authored
refactor: support CommonJS require in postcss.config.cjs (#185)
1 parent 79b62cd commit a458c49

File tree

7 files changed

+37
-13
lines changed

7 files changed

+37
-13
lines changed

.changeset/lemon-tips-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"postcss-if-function": patch
3+
---
4+
5+
fix: support CommonJS require in postcss.config.cjs

packages/postcss-if-function/README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ npx postcss input.css --output output.css --config postcss.config.js
5656
### Basic Programmatic Usage
5757

5858
```js
59-
// Named export (recommended)
60-
import postcss from "postcss";
61-
import { postcssIfFunction } from "postcss-if-function";
62-
63-
// Or default export (for compatibility)
6459
import postcss from "postcss";
6560
import postcssIfFunction from "postcss-if-function";
6661

@@ -115,7 +110,7 @@ const result = await postcss([
115110

116111
```js
117112
// postcss.config.js
118-
import { postcssIfFunction } from "postcss-if-function";
113+
import postcssIfFunction from "postcss-if-function";
119114

120115
export default {
121116
plugins: [
@@ -126,14 +121,21 @@ export default {
126121
};
127122
```
128123

124+
```js
125+
// postcss.config.cjs (CommonJS)
126+
module.exports = {
127+
plugins: [require("postcss-if-function")]
128+
};
129+
```
130+
129131
### With Popular PostCSS Tools
130132

131133
#### Vite
132134

133135
```js
134136
// vite.config.js
135137
import { defineConfig } from "vite";
136-
import { postcssIfFunction } from "postcss-if-function";
138+
import postcssIfFunction from "postcss-if-function";
137139

138140
export default defineConfig({
139141
css: {

packages/postcss-if-function/demo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import postcss from 'postcss';
2-
import { postcssIfFunction } from './src/index.js';
2+
import postcssIfFunction from './src/index.js';
33

44
const css = `
55
.example {

packages/postcss-if-function/microbundle.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const config = {
22
output: {
3-
exports: 'named'
3+
exports: 'default'
44
}
55
};
66

packages/postcss-if-function/src/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export type PluginOptions = {
2727
* @param {PluginOptions} [options={}] - Plugin configuration options
2828
* @returns {Object} PostCSS plugin
2929
*/
30-
export function postcssIfFunction(
30+
declare function postcssIfFunction(
3131
options?: PluginOptions
3232
): Record<string, unknown>;
33-
export namespace postcssIfFunction {
33+
declare namespace postcssIfFunction {
3434
const postcss: boolean;
3535
}
3636

packages/postcss-if-function/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,4 @@ function postcssIfFunction(options = {}) {
119119

120120
postcssIfFunction.postcss = true;
121121

122-
export { postcssIfFunction };
123122
export default postcssIfFunction;

packages/postcss-if-function/test/plugin.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createRequire } from 'node:module';
12
import path from 'node:path';
23
import { fileURLToPath } from 'node:url';
34
import postcss from 'postcss';
@@ -7,9 +8,10 @@ import {
78
normalizeCSS,
89
postcssFixtureTests
910
} from '../../../test/scripts/fixture-utils.js';
10-
import { postcssIfFunction } from '../src/index.js';
11+
import postcssIfFunction from '../src/index.js';
1112

1213
const __dirname = path.dirname(fileURLToPath(import.meta.url));
14+
const require = createRequire(import.meta.url);
1315
const FIXTURES_DIR = path.join(__dirname, '../../../test/fixtures');
1416

1517
describe('postcss-if-function plugin', () => {
@@ -81,4 +83,20 @@ describe('postcss-if-function plugin', () => {
8183
// Verify the from option was properly set in the result
8284
expect(result.opts.from).toBe(inputPath);
8385
});
86+
87+
it('should support CommonJS require in postcss.config.cjs', async () => {
88+
const plugin = require('../dist/index.cjs');
89+
const { input, expected } = loadFixture('basic-media');
90+
91+
// Verify the CJS export is a callable function with the postcss flag
92+
expect(typeof plugin).toBe('function');
93+
expect(plugin.postcss).toBe(true);
94+
95+
const result = await postcss([plugin]).process(input, {
96+
from: undefined
97+
});
98+
99+
expect(normalizeCSS(result.css)).toBe(normalizeCSS(expected));
100+
expect(result.warnings()).toHaveLength(0);
101+
});
84102
});

0 commit comments

Comments
 (0)