1+ import { createRequire } from 'node:module' ;
12import path from 'node:path' ;
23import { fileURLToPath } from 'node:url' ;
34import 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
1213const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
14+ const require = createRequire ( import . meta. url ) ;
1315const FIXTURES_DIR = path . join ( __dirname , '../../../test/fixtures' ) ;
1416
1517describe ( '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