Skip to content

Commit

Permalink
Feat: Allow dynamic config in @snowpack/plugin-postcss
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed May 20, 2021
1 parent eae9121 commit 5a6c924
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
17 changes: 10 additions & 7 deletions plugins/plugin-postcss/plugin.js
@@ -1,15 +1,18 @@
'use strict';

const {resolve, relative, isAbsolute} = require('path');
const path = require('path');
const workerpool = require('workerpool');

module.exports = function postcssPlugin(snowpackConfig, options) {
// options validation
if (options) {
if (typeof options !== 'object' || Array.isArray(options))
throw new Error('options isn’t an object. Please see README.');
if (options.config && typeof options.config !== 'string')
throw new Error('options.config must be a path to a PostCSS config file.');
if (
(options.config && typeof options !== 'string' && typeof options !== 'object') ||
Array.isArray(options)
)
throw new Error('options.config must be a config object or a path to a PostCSS config file.');
}

let worker, pool;
Expand All @@ -23,8 +26,8 @@ module.exports = function postcssPlugin(snowpackConfig, options) {

if (!input.includes(fileExt) || !contents) return;

if (config) {
config = resolve(config);
if (config && typeof config === 'string') {
config = path.resolve(config);
}

pool = pool || workerpool.pool(require.resolve('./worker.js'));
Expand Down Expand Up @@ -72,9 +75,9 @@ module.exports = function postcssPlugin(snowpackConfig, options) {
}
for (const dir of dirs) {
// https://stackoverflow.com/a/45242825
const relativePath = relative(dir, filePath);
const relativePath = path.relative(dir, filePath);
const dirContainsFilePath =
relativePath && !relativePath.startsWith('..') && !isAbsolute(relativePath);
relativePath && !relativePath.startsWith('..') && !path.isAbsolute(relativePath);

if (dirContainsFilePath) {
this.markChanged(id);
Expand Down
16 changes: 16 additions & 0 deletions plugins/plugin-postcss/test/plugin.test.js
Expand Up @@ -70,4 +70,20 @@ describe('@snowpack/plugin-postcss', () => {
expect(transformCSSResults).toBeFalsy();
await pluginInstance.cleanup();
});

test('allows dynamic config', async () => {
const pluginInstance = plugin(
{root: path.resolve(__dirname, 'stubs')},
{config: {plugins: {cssnano: {}}}},
);
const transformCSSResults = await pluginInstance.transform({
id: cssPath,
fileExt: path.extname(cssPath),
contents: cssContent,
});
expect(transformCSSResults.code).toBe(minCssContent); // TODO: remove this?
expect(transformCSSResults.contents).toBe(minCssContent);
expect(transformCSSResults.map).toBe(undefined);
await pluginInstance.cleanup();
});
});
7 changes: 5 additions & 2 deletions plugins/plugin-postcss/worker.js
Expand Up @@ -9,9 +9,12 @@ let process = null;
async function transformAsync(css, {filepath, config, cwd, map}) {
// Initialize processor. `config`, `cwd` won't change until Snowpack is restarted
if (!process) {
const {plugins, rcOptions} = await postcssrc({}, config || cwd);
const {plugins, options} =
typeof config === 'string'
? await postcssrc({}, config || cwd)
: await postcssrc(config, cwd);
const processor = postcss(plugins);
process = (css) => processor.process(css, {...rcOptions, from: filepath, map});
process = (css) => processor.process(css, {...options, from: filepath, map});
}

const result = await process(css);
Expand Down

0 comments on commit 5a6c924

Please sign in to comment.