2024-07-02 18:28:48 +00:00
|
|
|
import type { Nuxt, NuxtOptions } from '@nuxt/schema'
|
2023-03-29 10:59:57 +00:00
|
|
|
import type { InlineConfig as ViteConfig } from 'vite'
|
2024-07-02 18:28:48 +00:00
|
|
|
import type { Plugin } from 'postcss'
|
2024-07-03 20:12:33 +00:00
|
|
|
import { createJiti } from 'jiti'
|
2021-10-13 20:08:26 +00:00
|
|
|
|
2024-07-02 18:28:48 +00:00
|
|
|
function sortPlugins ({ plugins, order }: NuxtOptions['postcss']): string[] {
|
|
|
|
const names = Object.keys(plugins)
|
|
|
|
return typeof order === 'function' ? order(names) : (order || names)
|
|
|
|
}
|
2024-06-13 22:35:00 +00:00
|
|
|
|
2024-07-02 18:28:48 +00:00
|
|
|
export async function resolveCSSOptions (nuxt: Nuxt): Promise<ViteConfig['css']> {
|
2023-03-29 10:59:57 +00:00
|
|
|
const css: ViteConfig['css'] & { postcss: NonNullable<Exclude<NonNullable<ViteConfig['css']>['postcss'], string>> } = {
|
2021-10-13 20:08:26 +00:00
|
|
|
postcss: {
|
2024-04-05 18:08:32 +00:00
|
|
|
plugins: [],
|
|
|
|
},
|
2021-10-13 20:08:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-13 22:35:00 +00:00
|
|
|
css.postcss.plugins = []
|
2024-07-02 18:28:48 +00:00
|
|
|
const postcssOptions = nuxt.options.postcss
|
|
|
|
|
2024-07-03 20:12:33 +00:00
|
|
|
const jiti = createJiti(nuxt.options.rootDir, {
|
|
|
|
interopDefault: true,
|
|
|
|
alias: nuxt.options.alias,
|
|
|
|
})
|
|
|
|
|
2024-07-02 18:28:48 +00:00
|
|
|
for (const pluginName of sortPlugins(postcssOptions)) {
|
|
|
|
const pluginOptions = postcssOptions.plugins[pluginName]
|
|
|
|
if (!pluginOptions) { continue }
|
|
|
|
|
2024-09-20 17:39:17 +00:00
|
|
|
let pluginFn: ((opts: Record<string, any>) => Plugin) | undefined
|
|
|
|
for (const parentURL of nuxt.options.modulesDir) {
|
2024-09-21 21:04:59 +00:00
|
|
|
pluginFn = await jiti.import(pluginName, { parentURL: parentURL.replace(/\/node_modules\/?$/, ''), try: true }) as (opts: Record<string, any>) => Plugin
|
2024-09-20 17:39:17 +00:00
|
|
|
if (typeof pluginFn === 'function') {
|
|
|
|
css.postcss.plugins.push(pluginFn(pluginOptions))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof pluginFn !== 'function') {
|
2024-07-03 20:12:33 +00:00
|
|
|
console.warn(`[nuxt] could not import postcss plugin \`${pluginName}\`. Please report this as a bug.`)
|
2024-06-13 22:35:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-13 20:08:26 +00:00
|
|
|
|
|
|
|
return css
|
|
|
|
}
|