2021-10-02 16:01:17 +00:00
|
|
|
import { createCommonJS } from 'mlly'
|
|
|
|
import { defaults, merge, cloneDeep } from 'lodash-es'
|
2022-02-25 19:11:01 +00:00
|
|
|
import { requireModule } from '@nuxt/kit'
|
2021-11-21 16:14:46 +00:00
|
|
|
import type { Nuxt } from '@nuxt/schema'
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2022-02-25 19:11:01 +00:00
|
|
|
const isPureObject = (obj: unknown): obj is Object => obj !== null && !Array.isArray(obj) && typeof obj === 'object'
|
2020-07-02 13:02:35 +00:00
|
|
|
|
|
|
|
export const orderPresets = {
|
2022-02-25 19:11:01 +00:00
|
|
|
cssnanoLast (names: string[]) {
|
2020-07-02 13:02:35 +00:00
|
|
|
const nanoIndex = names.indexOf('cssnano')
|
|
|
|
if (nanoIndex !== names.length - 1) {
|
|
|
|
names.push(names.splice(nanoIndex, 1)[0])
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
},
|
2022-02-25 19:11:01 +00:00
|
|
|
autoprefixerLast (names: string[]) {
|
2021-09-17 16:20:05 +00:00
|
|
|
const nanoIndex = names.indexOf('autoprefixer')
|
2020-07-02 13:02:35 +00:00
|
|
|
if (nanoIndex !== names.length - 1) {
|
|
|
|
names.push(names.splice(nanoIndex, 1)[0])
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
},
|
2022-02-25 19:11:01 +00:00
|
|
|
autoprefixerAndCssnanoLast (names: string[]) {
|
2021-09-17 16:20:05 +00:00
|
|
|
return orderPresets.cssnanoLast(orderPresets.autoprefixerLast(names))
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:11:01 +00:00
|
|
|
export const getPostcssConfig = (nuxt: Nuxt) => {
|
|
|
|
function defaultConfig () {
|
2020-07-02 13:02:35 +00:00
|
|
|
return {
|
2022-02-25 19:11:01 +00:00
|
|
|
sourceMap: nuxt.options.webpack.cssSourceMap,
|
|
|
|
plugins: nuxt.options.postcss.plugins,
|
2020-07-02 13:02:35 +00:00
|
|
|
// Array, String or Function
|
2021-09-17 16:20:05 +00:00
|
|
|
order: 'autoprefixerAndCssnanoLast'
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:11:01 +00:00
|
|
|
function sortPlugins ({ plugins, order }) {
|
2020-07-02 13:02:35 +00:00
|
|
|
const names = Object.keys(plugins)
|
|
|
|
if (typeof order === 'string') {
|
|
|
|
order = orderPresets[order]
|
|
|
|
}
|
|
|
|
return typeof order === 'function' ? order(names, orderPresets) : (order || names)
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:11:01 +00:00
|
|
|
function loadPlugins (config) {
|
2021-09-21 15:19:54 +00:00
|
|
|
if (!isPureObject(config.plugins)) { return }
|
2022-02-25 19:11:01 +00:00
|
|
|
|
2021-09-21 15:19:54 +00:00
|
|
|
// Map postcss plugins into instances on object mode once
|
2021-10-02 16:01:17 +00:00
|
|
|
const cjs = createCommonJS(import.meta.url)
|
2022-02-25 19:11:01 +00:00
|
|
|
config.plugins = sortPlugins(config).map((pluginName) => {
|
2021-10-02 16:01:17 +00:00
|
|
|
const pluginFn = requireModule(pluginName, { paths: [cjs.__dirname] })
|
2021-09-21 15:19:54 +00:00
|
|
|
const pluginOptions = config.plugins[pluginName]
|
|
|
|
if (!pluginOptions || typeof pluginFn !== 'function') { return null }
|
|
|
|
return pluginFn(pluginOptions)
|
|
|
|
}).filter(Boolean)
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:11:01 +00:00
|
|
|
if (!nuxt.options.webpack.postcss || !nuxt.options.postcss) {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2022-02-25 19:11:01 +00:00
|
|
|
const configFile = nuxt.options.postcss?.config
|
|
|
|
if (configFile) {
|
|
|
|
return {
|
|
|
|
postcssOptions: {
|
|
|
|
config: configFile
|
|
|
|
},
|
|
|
|
sourceMap: nuxt.options.webpack.cssSourceMap
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
2022-02-25 19:11:01 +00:00
|
|
|
}
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2022-02-25 19:11:01 +00:00
|
|
|
let postcssOptions = cloneDeep(nuxt.options.postcss)
|
|
|
|
// Apply default plugins
|
|
|
|
if (isPureObject(postcssOptions)) {
|
|
|
|
if (Array.isArray(postcssOptions.plugins)) {
|
|
|
|
defaults(postcssOptions, defaultConfig())
|
|
|
|
} else {
|
|
|
|
// Keep the order of default plugins
|
|
|
|
postcssOptions = merge({}, defaultConfig(), postcssOptions)
|
|
|
|
loadPlugins(postcssOptions)
|
|
|
|
}
|
2021-09-17 16:20:05 +00:00
|
|
|
|
2022-02-25 19:11:01 +00:00
|
|
|
delete nuxt.options.webpack.postcss.order
|
2021-09-17 16:20:05 +00:00
|
|
|
|
2022-02-25 19:11:01 +00:00
|
|
|
return {
|
|
|
|
sourceMap: nuxt.options.webpack.cssSourceMap,
|
|
|
|
...nuxt.options.webpack.postcss,
|
|
|
|
postcssOptions
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|