Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
test: code
  • Loading branch information
cap-Bernardito committed Aug 14, 2020
commit 1e4cd69183b38931fc16fffc626246000b00ad4d
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,21 @@ export default async function loader(content, sourceMap, meta = {}) {
}
}

let plugins;

try {
plugins = [
...getArrayPlugins(loadedConfig.plugins, file),
...getArrayPlugins(options.plugins, file),
];
} catch (error) {
this.emitError(error);
}

const mergedOptions = {
...loadedConfig,
...options,
plugins: [
...getArrayPlugins(loadedConfig.plugins, file),
...getArrayPlugins(options.plugins, file),
],
plugins,
};

const resultPlugins = mergedOptions.plugins;
Expand Down
27 changes: 8 additions & 19 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,20 @@ const load = (plugin, options, file) => {
};

function loadPlugins(pluginEntry, file) {
let plugins = [];
const plugins = Object.entries(pluginEntry).filter((i) => {
const [, options] = i;

if (Array.isArray(pluginEntry)) {
plugins = pluginEntry.filter(Boolean);
} else {
plugins = Object.entries(pluginEntry).filter((i) => {
const [, options] = i;

return options !== false ? pluginEntry : '';
});
}
return options !== false ? pluginEntry : '';
});

plugins = plugins.map((plugin) => {
const loadedPlugins = plugins.map((plugin) => {
const [pluginName, pluginOptions] = plugin;

return load(pluginName, pluginOptions, file);
});

if (plugins.length && plugins.length > 0) {
plugins.forEach((plugin, i) => {
if (plugin.postcss) {
// eslint-disable-next-line no-param-reassign
plugin = plugin.postcss;
}

if (loadedPlugins.length && loadedPlugins.length > 0) {
loadedPlugins.forEach((plugin, i) => {
if (plugin.default) {
// eslint-disable-next-line no-param-reassign
plugin = plugin.default;
Expand All @@ -97,7 +86,7 @@ function loadPlugins(pluginEntry, file) {
});
}

return plugins;
return loadedPlugins;
}

function exec(code, loaderContext) {
Expand Down
11 changes: 11 additions & 0 deletions test/options/__snapshots__/plugins.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options Plugins should emit error on load plugin: errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
TypeError: Cannot read property 'postcss' of undefined",
"ModuleError: Module Error (from \`replaced original path\`):
Loading PostCSS Plugin failed: Cannot find module 'postcss-unresolved' from 'src/utils.js'",
]
`;

exports[`Options Plugins should emit error on load plugin: warnings 1`] = `Array []`;

exports[`Options Plugins should work Plugins - {Array<Object>} + options: css 1`] = `
"a {
border-top-color: blue;
Expand Down
12 changes: 12 additions & 0 deletions test/options/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,16 @@ describe('Options Plugins', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should emit error on load plugin', async () => {
const compiler = getCompiler('./css/index2.js', {
plugins: {
'postcss-unresolved': {},
},
});
const stats = await compile(compiler);

expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats, true)).toMatchSnapshot('errors');
});
});