-
Notifications
You must be signed in to change notification settings - Fork 390
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
XiaoXice
wants to merge
1
commit into
didi:master
Choose a base branch
from
XiaoXice:unocss-postcss
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: unocss 支持 postcss #1851
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_') | ||
|
@@ -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) => { | ||
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() | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 嗯?这里不是说不能对js模块进行处理么 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 还是你看的细,我rebase代码的时候貌似选错分支了 |
||
if (replaced) { compilation.assets[file] = new WebpackSources.RawSource(code) } | ||
} | ||
}) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
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