Skip to content

Commit

Permalink
build(style): 增加前置postcss插件能力支持
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherso1a committed Dec 20, 2023
1 parent ae26ece commit fdb5134
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
31 changes: 31 additions & 0 deletions docs-vuepress/api/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,37 @@ module.exports = defineConfig({
})
```

**注意:**默认添加的 postcss 插件均会在`mpx的内置插件`(例如如rpx插件等)之后处理。如需使配置的插件优先于内置插件,可以在插件 options 中设置 `mpxPrePlugin` 选项为 `true`

```js
// vue.config.js
module.exports = defineConfig({
pluginOptions: {
mpx: {
plugin: {
postcssInlineConfig: {
plugins: [
require('postcss-import'),
require('postcss-preset-env'),
require('cssnano')({ mpxPrePlugin: true }),
require('autoprefixer')({ remove: true, mpxPrePlugin: true })
]
// 以下写法同理
// plugins: {
// 'postcss-import': {},
// 'postcss-preset-env': {},
// 'cssnano': { mpxPrePlugin: true },
// 'autoprefixer': { remove: true, mpxPrePlugin: true }
// }
}
}
}
}
})
```

在上面这个例子当中,postcss 插件处理的最终顺序为:`cssnano` -> `autoprefixer` -> `mpx内置插件` -> `postcss-import` -> `postcss-preset-env`

### decodeHTMLText

`boolean = false`
Expand Down
12 changes: 10 additions & 2 deletions packages/webpack-plugin/lib/style-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,17 @@ module.exports = function (css, map) {
}
}

plugins.push(...config.plugins) // push user config plugins
const prePlugins = []

return postcss(plugins)
config.plugins.forEach(e => {
if (e.options && e.options.mpxPrePlugin) {
prePlugins.push(e)
} else {
plugins.push(e)
}
})

return postcss(prePlugins.concat(plugins))
.process(css, options)
.then(result => {
// ali环境添加全局样式抹平root差异
Expand Down

0 comments on commit fdb5134

Please sign in to comment.