Skip to content

Commit

Permalink
Simplify PostCSS config
Browse files Browse the repository at this point in the history
Removes workarounds previously needed for the webpack example
  • Loading branch information
colinrotherham committed Mar 7, 2023
1 parent cf270b7 commit 5864542
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions postcss.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { parse } from 'path'

import autoprefixer from 'autoprefixer'
import cssnano from 'cssnano'
import cssnanoPresetDefault from 'cssnano-preset-default'
Expand All @@ -20,25 +18,20 @@ import { isDev } from './tasks/task-arguments.mjs'
* @param {import('postcss-load-config').ConfigContext} ctx - PostCSS context
* @returns {import('postcss-load-config').Config} PostCSS config
*/
export default function postcssConfig (ctx) {
export default ({ from = '', to = '', env = 'production' }) => {
const plugins = []
const syntax = ctx.to?.endsWith('.scss') ? scss : postcss

// PostCSS 'from' (or webpack 'file') source path
// https://github.com/postcss/postcss-load-config#options
const { dir, name } = parse(ctx.from || ctx.file || '')

// IE8 stylesheets
const isIE8 = name?.endsWith('-ie8') || name?.endsWith('-ie8.min')
// Browserslist IE8 environment
if (from.includes('-ie8')) {
env = 'oldie'
}

// Add vendor prefixes
plugins.push(autoprefixer({
env: isIE8 ? 'oldie' : ctx.env
}))
plugins.push(autoprefixer({ env }))

// Add review app auto-generated 'companion' classes for each pseudo-class
// For example ':hover' and ':focus' classes to simulate form label states
if (minimatch(dir, '**/app/stylesheets')) {
if (minimatch(from, '**/app/stylesheets/*')) {
plugins.push(pseudoclasses({
allCombinations: true,
restrictTo: [
Expand All @@ -52,7 +45,7 @@ export default function postcssConfig (ctx) {
}

// Transpile CSS for Internet Explorer
if (isIE8) {
if (env === 'oldie') {
plugins.push(
unmq(),
unopacity({ browsers: 'ie 8' }),
Expand All @@ -72,6 +65,9 @@ export default function postcssConfig (ctx) {
})
}

// Sass syntax support
const syntax = to.endsWith('.scss') ? scss : postcss

// Always minify CSS
if (syntax !== scss) {
plugins.push(cssnano({
Expand Down

0 comments on commit 5864542

Please sign in to comment.