Skip to content

feat: unocss 支持 postcss #1851

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/unocss-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class MpxUnocssPlugin {
await Promise.all(Object.entries(assets).map(([file, source]) => {
if (file.endsWith(styleExt) || file.endsWith(templateExt)) {
const assetModules = assetsModulesMap.get(file)
if (has(assetModules, (module) => {
if (assetModules && has(assetModules, (module) => {
if (module.resource) {
const resourcePath = toPosix(parseRequest(module.resource).resourcePath)
return filterFile(resourcePath, this.options.scan)
Expand Down
17 changes: 16 additions & 1 deletion packages/unocss-plugin/lib/web-plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const postcss = require('postcss')
const WebpackSources = require('webpack-sources')
const VirtualModulesPlugin = require('webpack-virtual-modules')
const node_path = require('node:path')
const process = require('process')
const fs = require('fs')
const { createContext, getPath, normalizeAbsolutePath } = require('./utils')
const { LAYER_MARK_ALL, LAYER_PLACEHOLDER_RE, RESOLVED_ID_RE, getLayerPlaceholder, resolveId, resolveLayer } = require('./consts')
const loadPostcssConfig = require('@mpxjs/webpack-plugin/lib/style-compiler/load-postcss-config')

const PLUGIN_NAME = 'unocss:webpack'
const VIRTUAL_MODULE_PREFIX = node_path.resolve(process.cwd(), '_virtual_')
Expand Down Expand Up @@ -89,8 +91,20 @@ 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

const { __mpx__: mpx } = compilation
const inlineConfig = Object.assign({}, mpx.postcssInlineConfig, {
defs: mpx.defs,
inlineConfigFile: node_path.join(mpx.projectRoot, 'vue.config.js')
})
compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, async () => {
const config = await loadPostcssConfig({
emitWarning (warning) {
compilation.warnings.push(warning)
}
}, inlineConfig)
const plugins = config.prePlugins.concat(config.plugins)
const options = Object.assign({ to: '__uno.css', from: '__uno.css', map: false }, config.options)
// 清空transformCache避免watch修改不生效
transformCache.clear()
const tokens = new Set()
Expand Down Expand Up @@ -118,6 +132,7 @@ function WebpackPlugin (configOrPath, defaults) {
if (quote === '\\"') { escaped = JSON.stringify(escaped).slice(1, -1) }
return quote + escaped
})
code = (await postcss(plugins).process(code, options)).content
Copy link
Collaborator

Choose a reason for hiding this comment

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

嗯?这里不是说不能对js模块进行处理么

Copy link
Contributor Author

Choose a reason for hiding this comment

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

还是你看的细,我rebase代码的时候貌似选错分支了

if (replaced) { compilation.assets[file] = new WebpackSources.RawSource(code) }
}
})
Expand Down
Loading