From 70f067a14b9a6e02bb7a4e6889ccf19fc674e1f4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 22 Mar 2022 10:40:36 +0000 Subject: [PATCH] fix(vite): sort postcss plugins to add autoprefixer & cssnano last (#3827) --- packages/vite/src/css.ts | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/vite/src/css.ts b/packages/vite/src/css.ts index 5daf34d005..71b05746d4 100644 --- a/packages/vite/src/css.ts +++ b/packages/vite/src/css.ts @@ -4,28 +4,25 @@ import type { ViteOptions } from './vite' import { distDir } from './dirs' export function resolveCSSOptions (nuxt: Nuxt): ViteOptions['css'] { - const css: ViteOptions['css'] = { + const css: ViteOptions['css'] & { postcss: Exclude } = { postcss: { plugins: [] } } - const plugins = nuxt.options.postcss.plugins - - for (const name in plugins) { - const opts = plugins[name] - if (!opts) { - continue - } - const plugin = requireModule(name, { - paths: [ - ...nuxt.options.modulesDir, - distDir - ] + const lastPlugins = ['autoprefixer', 'cssnano'] + css.postcss.plugins = Object.entries(nuxt.options.postcss.plugins) + .sort((a, b) => lastPlugins.indexOf(a[0]) - lastPlugins.indexOf(b[0])) + .filter(([, opts]) => opts) + .map(([name, opts]) => { + const plugin = requireModule(name, { + paths: [ + ...nuxt.options.modulesDir, + distDir + ] + }) + return plugin(opts) }) - // @ts-ignore - css.postcss.plugins.push(plugin(opts)) - } return css }