Skip to content

Commit 3d32c35

Browse files
fix: default export for plugins (#465)
1 parent 38ebe08 commit 3d32c35

File tree

4 files changed

+175
-7
lines changed

4 files changed

+175
-7
lines changed

src/utils.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,29 @@ async function loadConfig(loaderContext, config) {
8484
}
8585

8686
function loadPlugin(plugin, options, file) {
87-
// TODO defaults
8887
try {
8988
if (!options || Object.keys(options).length === 0) {
90-
// eslint-disable-next-line global-require,import/no-dynamic-require
91-
return require(plugin);
89+
// eslint-disable-next-line global-require, import/no-dynamic-require
90+
const loadedPlugin = require(plugin);
91+
92+
if (loadedPlugin.default) {
93+
return loadedPlugin.default;
94+
}
95+
96+
return loadedPlugin;
97+
}
98+
99+
// eslint-disable-next-line global-require, import/no-dynamic-require
100+
const loadedPlugin = require(plugin);
101+
102+
if (loadedPlugin.default) {
103+
return loadedPlugin.default(options);
92104
}
93105

94-
// eslint-disable-next-line global-require,import/no-dynamic-require
95-
return require(plugin)(options);
106+
return loadedPlugin(options);
96107
} catch (error) {
97108
throw new Error(
98-
`Loading PostCSS Plugin failed: ${error.message}\n\n(@${file})`
109+
`Loading PostCSS "${plugin}" plugin failed: ${error.message}\n\n(@${file})`
99110
);
100111
}
101112
}

test/__snapshots__/plugins.test.js.snap

+103-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`"plugins" option should throw an error on the unresolved plugin: errors 1`] = `
44
Array [
55
"ModuleError: Module Error (from \`replaced original path\`):
6-
Loading PostCSS Plugin failed: Cannot find module 'postcss-unresolved' from 'src/utils.js'",
6+
Loading PostCSS \\"postcss-unresolved\\" plugin failed: Cannot find module 'postcss-unresolved' from 'src/utils.js'",
77
]
88
`;
99

@@ -587,3 +587,105 @@ a {
587587
exports[`"plugins" option should work with empty "Object" value: errors 1`] = `Array []`;
588588

589589
exports[`"plugins" option should work with empty "Object" value: warnings 1`] = `Array []`;
590+
591+
exports[`"plugins" option should work with the "default" property with options: css 1`] = `
592+
"a {
593+
color: black;
594+
}
595+
596+
a {
597+
color: rgba(0, 0, 255, 0.5);
598+
}
599+
600+
a {
601+
color: green;
602+
}
603+
604+
a {
605+
color: blue;
606+
}
607+
608+
.class {
609+
-x-border-color: blue blue *;
610+
-x-color: * #fafafa;
611+
}
612+
613+
.class-foo {
614+
-z-border-color: blue blue *;
615+
-z-color: * #fafafa;
616+
}
617+
618+
.phone {
619+
&_title {
620+
width: 500px;
621+
622+
@media (max-width: 500px) {
623+
width: auto;
624+
}
625+
626+
body.is_dark & {
627+
color: white;
628+
}
629+
}
630+
631+
img {
632+
display: block;
633+
}
634+
}
635+
"
636+
`;
637+
638+
exports[`"plugins" option should work with the "default" property with options: errors 1`] = `Array []`;
639+
640+
exports[`"plugins" option should work with the "default" property with options: warnings 1`] = `Array []`;
641+
642+
exports[`"plugins" option should work with the "default" property without options: css 1`] = `
643+
"a {
644+
color: black;
645+
}
646+
647+
a {
648+
color: red;
649+
}
650+
651+
a {
652+
color: green;
653+
}
654+
655+
a {
656+
color: rgba(0, 0, 255, 1.0);
657+
}
658+
659+
.class {
660+
-x-border-color: blue blue *;
661+
-x-color: * #fafafa;
662+
}
663+
664+
.class-foo {
665+
-z-border-color: blue blue *;
666+
-z-color: * #fafafa;
667+
}
668+
669+
.phone {
670+
&_title {
671+
width: 500px;
672+
673+
@media (max-width: 500px) {
674+
width: auto;
675+
}
676+
677+
body.is_dark & {
678+
color: white;
679+
}
680+
}
681+
682+
img {
683+
display: block;
684+
}
685+
}
686+
"
687+
`;
688+
689+
exports[`"plugins" option should work with the "default" property without options: errors 1`] = `Array []`;
690+
691+
exports[`"plugins" option should work with the "default" property without options: warnings 1`] = `Array []`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const postcss = require('postcss');
4+
5+
module.exports = {
6+
default: postcss.plugin('my-plugin', (options) => {
7+
options = { alpha: '1.0', color: 'blue', ...options };
8+
9+
return (root, result) => {
10+
root.walkDecls((decl) => {
11+
if (decl.value === options.color) {
12+
decl.value = 'rgba(0, 0, 255, ' + options.alpha + ')'
13+
}
14+
})
15+
}
16+
}),
17+
};

test/plugins.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,42 @@ describe('"plugins" option', () => {
260260
expect(getWarnings(stats)).toMatchSnapshot('warnings');
261261
expect(getErrors(stats, true)).toMatchSnapshot('errors');
262262
});
263+
264+
it('should work with the "default" property without options', async () => {
265+
const compiler = getCompiler('./css/index.js', {
266+
postcssOptions: {
267+
plugins: [
268+
path.resolve(__dirname, './fixtures/plugin/default-other-plugin.js'),
269+
],
270+
},
271+
});
272+
const stats = await compile(compiler);
273+
const codeFromBundle = getCodeFromBundle('style.css', stats);
274+
275+
expect(codeFromBundle.css).toMatchSnapshot('css');
276+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
277+
expect(getErrors(stats)).toMatchSnapshot('errors');
278+
});
279+
280+
it('should work with the "default" property with options', async () => {
281+
const compiler = getCompiler('./css/index.js', {
282+
postcssOptions: {
283+
plugins: [
284+
[
285+
path.resolve(
286+
__dirname,
287+
'./fixtures/plugin/default-other-plugin.js'
288+
),
289+
{ alpha: 0.5, color: 'red' },
290+
],
291+
],
292+
},
293+
});
294+
const stats = await compile(compiler);
295+
const codeFromBundle = getCodeFromBundle('style.css', stats);
296+
297+
expect(codeFromBundle.css).toMatchSnapshot('css');
298+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
299+
expect(getErrors(stats)).toMatchSnapshot('errors');
300+
});
263301
});

0 commit comments

Comments
 (0)