Skip to content

Commit

Permalink
fix: respect default when loading postcss esm configs
Browse files Browse the repository at this point in the history
  • Loading branch information
lideen authored Feb 28, 2024
1 parent fdd5448 commit 52d8050
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ async function loadConfig(loaderContext, config, postcssOptions) {
}
}

if (result.default) {
return result.default;
}

return result;
},
".cjs": defaultLoadersSync[".cjs"],
Expand All @@ -130,6 +134,10 @@ async function loadConfig(loaderContext, config, postcssOptions) {
throw new Error("ESM is not supported");
}

if (result.default) {
return result.default;
}

return result;
},
};
Expand Down
39 changes: 39 additions & 0 deletions test/config-autoload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,45 @@ describe("autoload config", () => {
);
});

it('should load ESM version of "postcss.config.js" with "Array" syntax of plugins', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "js/array-esm-js"),
);

expect(loadedConfig.config.map).toEqual(false);
expect(loadedConfig.config.from).toEqual(
"./test/fixtures/config-autoload/js/object/index.css",
);
expect(loadedConfig.config.to).toEqual(
"./test/fixtures/config-autoload/js/object/expect/index.css",
);
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(4);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "js/array-esm-js", "postcss.config.js"),
);
});

// TODO Test manually with NODE_OPTIONS=--experimental-vm-modules to enable ESM support in jest
it.skip('should load "postcss.config.mjs" with "Array" syntax of plugins', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "js/array-mjs"),
);

expect(loadedConfig.config.map).toEqual(false);
expect(loadedConfig.config.from).toEqual(
"./test/fixtures/config-autoload/js/object/index.css",
);
expect(loadedConfig.config.to).toEqual(
"./test/fixtures/config-autoload/js/object/expect/index.css",
);
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(4);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "js/array-mjs", "postcss.config.mjs"),
);
});

it('should load "postcss.config.ts" with "Array" syntax of plugins', async () => {
const loadedConfig = await loadConfig(
loaderContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import {
color: goldenrod;
}
5 changes: 5 additions & 0 deletions test/fixtures/config-autoload/js/array-esm-js/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import 'imports/section.css';

.test {
color: cyan;
}
3 changes: 3 additions & 0 deletions test/fixtures/config-autoload/js/array-esm-js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import style from './index.css'

export default style
21 changes: 21 additions & 0 deletions test/fixtures/config-autoload/js/array-esm-js/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import postcssNested from 'postcss-nested';
export default function (api) {
return {
parser: 'sugarss',
syntax: 'sugarss',
map: api.mode === 'development' ? 'inline' : false,
from: './test/fixtures/config-autoload/js/object/index.css',
to: './test/fixtures/config-autoload/js/object/expect/index.css',
plugins: [
'postcss-import',
[
'postcss-nested',
{
// Options
}
],
postcssNested,
postcssNested({ /* Options */ }),
]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import {
color: goldenrod;
}
5 changes: 5 additions & 0 deletions test/fixtures/config-autoload/js/array-mjs/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import 'imports/section.css';

.test {
color: cyan;
}
3 changes: 3 additions & 0 deletions test/fixtures/config-autoload/js/array-mjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import style from './index.css'

export default style
21 changes: 21 additions & 0 deletions test/fixtures/config-autoload/js/array-mjs/postcss.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import postcssNested from 'postcss-nested';
export default function (api) {
return {
parser: 'sugarss',
syntax: 'sugarss',
map: api.mode === 'development' ? 'inline' : false,
from: './test/fixtures/config-autoload/js/object/index.css',
to: './test/fixtures/config-autoload/js/object/expect/index.css',
plugins: [
'postcss-import',
[
'postcss-nested',
{
// Options
}
],
postcssNested,
postcssNested({ /* Options */ }),
]
}
};

0 comments on commit 52d8050

Please sign in to comment.