Skip to content
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
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Default configuration:
{
postcss: {
// Default plugins
plugins: ['autoprefixer'],
plugins: ['autoprefixer', ['another-postcss-plugin',{ foo: 'bar'}]],
// Stops the build if an error is found
failOnError: true
}
Expand Down
85 changes: 2 additions & 83 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions utils/getPlugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = function getPlugins(plugins = []) {
return plugins
.filter(Boolean)
.map(function (entry) {
if (Array.isArray(entry)) {
const [pluginName, pluginOptions] = entry;
const pluginModule = require(pluginName);
const pluginFn = pluginModule.default || pluginModule;
return pluginFn(pluginOptions);
} else if (typeof entry === 'string') {
const pluginModule = require(entry);
return pluginModule.default || pluginModule;
} else {
throw new Error(`Invalid plugin entry: ${JSON.stringify(entry)}`);
}
});
};
52 changes: 52 additions & 0 deletions utils/getPlugins.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
process.argv.push('--quiet');

describe('Test getPlugins.js', () => {
beforeEach(() => {
jest.resetModules();
});

it('Should load a plugin from string', () => {
jest.mock('postcss-fe-build-test-a', () => () => 'mocked-plugin-a', { virtual: true });

const getPlugins = require('./getPlugins');
const plugins = ['postcss-fe-build-test-a'];
const result = getPlugins(plugins);

expect(result).toHaveLength(1);
expect(result[0]()).toBe('mocked-plugin-a');
});

it('Should load a plugin from [plugin, options] format', () => {
jest.mock('postcss-fe-build-test-b', () => ({
default: (opts) => `mocked-plugin-b:${JSON.stringify(opts)}`
}), { virtual: true });

const getPlugins = require('./getPlugins');
const plugins = [['postcss-fe-build-test-b', { extractAll: false }]];
const result = getPlugins(plugins);

expect(result).toHaveLength(1);
expect(result[0]).toBe('mocked-plugin-b:{"extractAll":false}');
});


it('Should ignore falsy values in plugin list', () => {
jest.mock('postcss-fe-build-test-c', () => () => 'mocked-plugin-a', { virtual: true });

const getPlugins = require('./getPlugins');
const plugins = [null, undefined, false, 'postcss-fe-build-test-c'];
const result = getPlugins(plugins);

expect(result).toHaveLength(1);
expect(result[0]()).toBe('mocked-plugin-a');
});

it('Should throw on invalid plugin entry', () => {
const getPlugins = require('./getPlugins');

const invalidPlugins = [{ foo: 'bar' }, 123, true];
invalidPlugins.forEach(plugin => {
expect(() => getPlugins([plugin])).toThrow('Invalid plugin entry');
});
});
});
13 changes: 8 additions & 5 deletions utils/renderPostcss.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const postcss = require('postcss');
const { log } = require('./log');
const getPlugins = require('./getPlugins');

module.exports = function renderPostcss(input, outFile, config, cb) {
const { plugins, failOnError } = config.postcss;

try {
// try to dynamic load of postcss plugins
/* eslint-disable */
const runPlugins = plugins.map(plugin => require(plugin));
const map = config.general.isProduction ? false : { inline: true, prev: input.sourceMap } ;
const runPlugins = getPlugins(plugins);
const map = config.general.isProduction ? false : { inline: true, prev: input.sourceMap };

/* eslint-enable */
// run postcss plugins at sass output
postcss(runPlugins).process(input.css.toString(), { from: outFile, map })
.then((result) => {
Expand All @@ -29,7 +28,11 @@ module.exports = function renderPostcss(input, outFile, config, cb) {
}

// success and return
log(__filename, `Postcss applied ${plugins.join(',')} - `, input.destFile, 'success');
log(__filename,
`PostCSS applied: ${plugins.map(ent => (Array.isArray(ent) ? ent[0] : ent)).join(', ')}`,
input.destFile,
'success');

return cb(result);
});
} catch (error) {
Expand Down