Skip to content

Commit

Permalink
Breaking: Run purgecss and minify as a part of defaultPlugins()
Browse files Browse the repository at this point in the history
Take a bundle: bool flag and purge + minify is true. Defaults
to false.
  • Loading branch information
brookback committed Mar 2, 2023
1 parent 6290dc6 commit cfba8f6
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 44 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ exports.colors = colors;
exports.tailwindConfig = tailwindConfig;

/** PostCSS config for external use. */
exports.postCssConfig = ({ pathToTailwindConf } = {}) => () => ({
exports.postCssConfig = ({ pathToTailwindConf, bundle } = {}) => () => ({
map: 'inline', // Sourcemaps
plugins: defaultPlugins({ pathToTailwindConf }),
plugins: defaultPlugins({ pathToTailwindConf, bundle }),
});

exports.defaultPostCssPlugins = defaultPlugins;
27 changes: 27 additions & 0 deletions lib/postcss-plugins.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
const path = require('path');
const purgecss = require('@fullhuman/postcss-purgecss');
const csso = require('postcss-csso');

const LOOKBOOK_TAILWIND_CONF = path.resolve(
path.join(__dirname, '..', 'tailwind.config.js')
);

// Looking through all React files for selectors
// that are unused in the CSS, and removes the selectors from the final CSS
const purgeCssOpts = {
content: [
// Source tree, with the app view code
'./src/**/*.{tsx,ts}',
// Often imported components from our (built) lib in node_modules
'./node_modules/@lookback/component/build/**/*.js',
],
safelist: [/flex-\d/, /flex-grow/, /flex-shrink/, /LoadingDots/],
extractors: [
{
extractor(content) {
// Matches Tailwind class names
return content.match(/[A-Za-z0-9-_:/]+/g) || [];
},

extensions: ['tsx', 'ts'],
},
],
};

exports.defaultPlugins = ({
pathToTailwindConf = LOOKBOOK_TAILWIND_CONF,
/** If true, we'll minify and purge unused CSS classes. */
bundle = false,
} = {}) => [
require('postcss-import'),
require('tailwindcss')(pathToTailwindConf),
require('postcss-nested'),
require('postcss-color-function'),
require('postcss-hexrgba'),
require('autoprefixer'),
...(bundle ? [purgecss(purgeCssOpts), csso] : []),
];
Loading

0 comments on commit cfba8f6

Please sign in to comment.