Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(loadConfig): added fix when loading postcss esm configs #684

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
}

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

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

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

Check warning on line 138 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L138

Added line #L138 was not covered by tests
}

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 */ }),
]
}
};
Loading