-
Notifications
You must be signed in to change notification settings - Fork 385
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
base: master
Are you sure you want to change the base?
feat: unocss 支持 postcss #1851
Conversation
用unocss preset貌似可以解决你的问题 参考 https://github.com/unocss/unocss/blob/main/packages-presets/preset-rem-to-px/src/index.ts |
这个功能确实是可以通过 unocss preset 实现,但是 postcss 还可以添加其他的插件,例如 |
@@ -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)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里直接生成完code之后整体再进行一次postcss就可以
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥需要改钩子注册逻辑
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为下面需要从 compilation 中读取 __mpx__
,需要等待下面这个钩子执行完毕。
mpx/packages/webpack-plugin/lib/index.js
Lines 646 to 653 in b822464
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__ = { |
There was a problem hiding this comment.
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({ |
There was a problem hiding this comment.
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处理的公共方法,对应逻辑统一维护在一个地方
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
输出小程序和输出RN中也需要拉齐执行postcss
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看了一下 你可能提到的是下面这段代码里的 transSpecial
和 scopeId
,我认为不应该添加这两个插件,甚至 pluginCondStrip
都不需要。
我认为 unocss 貌似不太适用这些插件的场景,unocss产出的代码不需要scope和host,也不会有mpx相关的标志。
可能是我的理解还不到位,如果有理解错误还请指教。
mpx/packages/webpack-plugin/lib/style-compiler/index.js
Lines 49 to 63 in b822464
// 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 | |
})) |
There was a problem hiding this comment.
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状态,可以先不用关注,我们后续进行添加
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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}清晰一些
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
经过测试,读取配置并不会消耗太多时间,全构建耗时不超过100ms,我觉得你说的有道理。
e9e4ea1
to
1362cab
Compare
fix: #1850