Skip to content

Commit

Permalink
Remove webpack workarounds from PostCSS config
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Mar 15, 2023
1 parent 0d6307e commit 67b3fe2
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 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 @@ -17,29 +15,32 @@ import { isDev } from './tasks/helpers/task-arguments.mjs'
/**
* PostCSS config
*
* @param {import('postcss-load-config').ConfigContext} ctx - PostCSS context
* @returns {import('postcss-load-config').Config} PostCSS config
* @type {import('postcss-load-config').ConfigFn}
*/
export default function postcssConfig (ctx) {
const plugins = []
const syntax = ctx.to?.endsWith('.scss') ? scss : postcss
export default ({ from = '', to = '', env = 'production' }) => {
/** @type {import('postcss-load-config').Config} */
const config = {
plugins: [],
syntax: postcss
}

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

// IE8 stylesheets
const isIE8 = name?.endsWith('-ie8') || name?.endsWith('-ie8.min')
// Sass syntax support
if (to.endsWith('.scss')) {
config.syntax = scss
}

// Add vendor prefixes
plugins.push(autoprefixer({
env: isIE8 ? 'oldie' : ctx.env
}))
config.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')) {
plugins.push(pseudoclasses({
if (minimatch(from, '**/app/stylesheets/*')) {
config.plugins.push(pseudoclasses({
allCombinations: true,
restrictTo: [
':link',
Expand All @@ -52,8 +53,8 @@ export default function postcssConfig (ctx) {
}

// Transpile CSS for Internet Explorer
if (isIE8) {
plugins.push(
if (env === 'oldie') {
config.plugins.push(
unmq(),
unopacity({ browsers: 'ie 8' }),
unrgba({ filter: true })
Expand All @@ -62,7 +63,7 @@ export default function postcssConfig (ctx) {

// Add GOV.UK Frontend release version
if (!isDev) {
plugins.push({
config.plugins.push({
postcssPlugin: 'govuk-frontend-version',
Declaration: {
// Find CSS declaration for version, update value
Expand All @@ -76,8 +77,8 @@ export default function postcssConfig (ctx) {
}

// Always minify CSS
if (syntax !== scss) {
plugins.push(cssnano({
if (config.syntax !== scss) {
config.plugins.push(cssnano({
preset: [cssnanoPresetDefault, {
// Sorted CSS is smaller when gzipped, but we sort using Stylelint
// https://cssnano.co/docs/optimisations/cssdeclarationsorter/
Expand All @@ -86,8 +87,5 @@ export default function postcssConfig (ctx) {
}))
}

return {
syntax,
plugins
}
return config
}

0 comments on commit 67b3fe2

Please sign in to comment.