Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unocss 支持 postcss #1851

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

XiaoXice
Copy link
Contributor

@XiaoXice XiaoXice commented Feb 17, 2025

fix: #1850

@pagnkelly
Copy link
Collaborator

用unocss preset貌似可以解决你的问题 参考 https://github.com/unocss/unocss/blob/main/packages-presets/preset-rem-to-px/src/index.ts

@XiaoXice
Copy link
Contributor Author

这个功能确实是可以通过 unocss preset 实现,但是 postcss 还可以添加其他的插件,例如 autoprefixer
unocss 配合 vite 框架是可以正常使用postcss处理uno生成的css的。
我的实现方式可能不太优雅,希望mpx可以支持这个功能。

@@ -110,14 +138,27 @@ function WebpackPlugin (configOrPath, defaults) {
if (file === '*') { return }
let code = compilation.assets[file].source().toString()
let replaced = false
code = code.replace(LAYER_PLACEHOLDER_RE, (_, quote, layer) => {
for(const [source, quote, layer] of code.matchAll(LAYER_PLACEHOLDER_RE)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接生成完code之后整体再进行一次postcss就可以

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在 Web 生成的时候,code 里的代码不是CSS而是JS,所以不能进行整体处理。
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -89,8 +92,33 @@ function WebpackPlugin (configOrPath, defaults) {
}
})

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compiler.hooks.thisCompilation.tap({ name: PLUGIN_NAME, stage: 1000 }, (compilation) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥需要改钩子注册逻辑

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为下面需要从 compilation 中读取 __mpx__,需要等待下面这个钩子执行完毕。

compiler.hooks.thisCompilation.tap('MpxWebpackPlugin', (compilation, { normalModuleFactory }) => {
compilation.warnings.push(...warnings)
compilation.errors.push(...errors)
const moduleGraph = compilation.moduleGraph
if (!compilation.__mpx__) {
// init mpx
mpx = compilation.__mpx__ = {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

defs: mpx.defs,
inlineConfigFile: node_path.join(mpx.projectRoot, 'vue.config.js')
})
const configBox = loadPostcssConfig({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果是需要对unocss产物进行包含框架内置插件的postcss处理,建议在style-compiler中抽一个接收参数进行postcss处理的公共方法,对应逻辑统一维护在一个地方

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

输出小程序和输出RN中也需要拉齐执行postcss

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看了一下 你可能提到的是下面这段代码里的 transSpecialscopeId,我认为不应该添加这两个插件,甚至 pluginCondStrip 都不需要。
我认为 unocss 貌似不太适用这些插件的场景,unocss产出的代码不需要scope和host,也不会有mpx相关的标志。
可能是我的理解还不到位,如果有理解错误还请指教。

// ali平台下处理scoped和host选择器
if (mode === 'ali' || mode === 'web') {
if (queryObj.scoped || mpxStyleOptions.scoped) {
plugins.push(scopeId({ id }))
}
plugins.push(transSpecial({ id }))
}
if (isReact(mode)) {
plugins.push(transSpecial({ id }))
}
plugins.push(pluginCondStrip({
defs
}))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于unocss的产物来说可能确实不需要一些内置postcss功能
对于用户自定义的postcss配置,在输出小程序时拉齐进行postcss处理吧,对于输出rn,本身该能力还在review状态,可以先不用关注,我们后续进行添加

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我在最新的代码中完全移除了相关的plugin,请关注。

compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, async () => {
const [plugins, options] = await Promise.all([finalPluginsBox, configBox.then(config => Object.assign(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以把loadPostcssConfig的逻辑都统一放在这里,直接await loadPostcssConfig获取{options, plugins, prePlugins}清晰一些

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

经过测试,读取配置并不会消耗太多时间,全构建耗时不超过100ms,我觉得你说的有道理。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request]unocss 产出的 css 也经过 postcss 处理
3 participants