Skip to content

Commit 52d8050

Browse files
authored
fix: respect default when loading postcss esm configs
1 parent fdd5448 commit 52d8050

File tree

10 files changed

+111
-0
lines changed

10 files changed

+111
-0
lines changed

src/utils.js

+8
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ async function loadConfig(loaderContext, config, postcssOptions) {
108108
}
109109
}
110110

111+
if (result.default) {
112+
return result.default;
113+
}
114+
111115
return result;
112116
},
113117
".cjs": defaultLoadersSync[".cjs"],
@@ -130,6 +134,10 @@ async function loadConfig(loaderContext, config, postcssOptions) {
130134
throw new Error("ESM is not supported");
131135
}
132136

137+
if (result.default) {
138+
return result.default;
139+
}
140+
133141
return result;
134142
},
135143
};

test/config-autoload.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,45 @@ describe("autoload config", () => {
116116
);
117117
});
118118

119+
it('should load ESM version of "postcss.config.js" with "Array" syntax of plugins', async () => {
120+
const loadedConfig = await loadConfig(
121+
loaderContext,
122+
path.resolve(testDirectory, "js/array-esm-js"),
123+
);
124+
125+
expect(loadedConfig.config.map).toEqual(false);
126+
expect(loadedConfig.config.from).toEqual(
127+
"./test/fixtures/config-autoload/js/object/index.css",
128+
);
129+
expect(loadedConfig.config.to).toEqual(
130+
"./test/fixtures/config-autoload/js/object/expect/index.css",
131+
);
132+
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(4);
133+
expect(loadedConfig.filepath).toEqual(
134+
path.resolve(testDirectory, "js/array-esm-js", "postcss.config.js"),
135+
);
136+
});
137+
138+
// TODO Test manually with NODE_OPTIONS=--experimental-vm-modules to enable ESM support in jest
139+
it.skip('should load "postcss.config.mjs" with "Array" syntax of plugins', async () => {
140+
const loadedConfig = await loadConfig(
141+
loaderContext,
142+
path.resolve(testDirectory, "js/array-mjs"),
143+
);
144+
145+
expect(loadedConfig.config.map).toEqual(false);
146+
expect(loadedConfig.config.from).toEqual(
147+
"./test/fixtures/config-autoload/js/object/index.css",
148+
);
149+
expect(loadedConfig.config.to).toEqual(
150+
"./test/fixtures/config-autoload/js/object/expect/index.css",
151+
);
152+
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(4);
153+
expect(loadedConfig.filepath).toEqual(
154+
path.resolve(testDirectory, "js/array-mjs", "postcss.config.mjs"),
155+
);
156+
});
157+
119158
it('should load "postcss.config.ts" with "Array" syntax of plugins', async () => {
120159
const loadedConfig = await loadConfig(
121160
loaderContext,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.import {
2+
color: goldenrod;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import 'imports/section.css';
2+
3+
.test {
4+
color: cyan;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import style from './index.css'
2+
3+
export default style
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import postcssNested from 'postcss-nested';
2+
export default function (api) {
3+
return {
4+
parser: 'sugarss',
5+
syntax: 'sugarss',
6+
map: api.mode === 'development' ? 'inline' : false,
7+
from: './test/fixtures/config-autoload/js/object/index.css',
8+
to: './test/fixtures/config-autoload/js/object/expect/index.css',
9+
plugins: [
10+
'postcss-import',
11+
[
12+
'postcss-nested',
13+
{
14+
// Options
15+
}
16+
],
17+
postcssNested,
18+
postcssNested({ /* Options */ }),
19+
]
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.import {
2+
color: goldenrod;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import 'imports/section.css';
2+
3+
.test {
4+
color: cyan;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import style from './index.css'
2+
3+
export default style
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import postcssNested from 'postcss-nested';
2+
export default function (api) {
3+
return {
4+
parser: 'sugarss',
5+
syntax: 'sugarss',
6+
map: api.mode === 'development' ? 'inline' : false,
7+
from: './test/fixtures/config-autoload/js/object/index.css',
8+
to: './test/fixtures/config-autoload/js/object/expect/index.css',
9+
plugins: [
10+
'postcss-import',
11+
[
12+
'postcss-nested',
13+
{
14+
// Options
15+
}
16+
],
17+
postcssNested,
18+
postcssNested({ /* Options */ }),
19+
]
20+
}
21+
};

0 commit comments

Comments
 (0)