Skip to content

Commit e9e4ea1

Browse files
committed
feat: unocss 支持 postcss
1 parent 016ec61 commit e9e4ea1

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

packages/unocss-plugin/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ class MpxUnocssPlugin {
385385
await Promise.all(Object.entries(assets).map(([file, source]) => {
386386
if (file.endsWith(styleExt) || file.endsWith(templateExt)) {
387387
const assetModules = assetsModulesMap.get(file)
388-
if (has(assetModules, (module) => {
388+
if (assetModules && has(assetModules, (module) => {
389389
if (module.resource) {
390390
const resourcePath = toPosix(parseRequest(module.resource).resourcePath)
391391
return filterFile(resourcePath, this.options.scan)

packages/unocss-plugin/lib/web-plugin/index.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
const postcss = require('postcss')
2+
const pluginCondStrip = require('@mpxjs/webpack-plugin/lib/style-compiler/plugins/conditional-strip')
13
const WebpackSources = require('webpack-sources')
24
const VirtualModulesPlugin = require('webpack-virtual-modules')
35
const node_path = require('node:path')
46
const process = require('process')
57
const fs = require('fs')
68
const { createContext, getPath, normalizeAbsolutePath } = require('./utils')
79
const { LAYER_MARK_ALL, LAYER_PLACEHOLDER_RE, RESOLVED_ID_RE, getLayerPlaceholder, resolveId, resolveLayer } = require('./consts')
10+
const loadPostcssConfig = require('@mpxjs/webpack-plugin/lib/style-compiler/load-postcss-config')
811

912
const PLUGIN_NAME = 'unocss:webpack'
1013
const VIRTUAL_MODULE_PREFIX = node_path.resolve(process.cwd(), '_virtual_')
@@ -89,8 +92,33 @@ function WebpackPlugin (configOrPath, defaults) {
8992
}
9093
})
9194

92-
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
95+
compiler.hooks.thisCompilation.tap({ name: PLUGIN_NAME, stage: 1000 }, (compilation) => {
96+
const { __mpx__: mpx } = compilation
97+
const inlineConfig = Object.assign({}, mpx.postcssInlineConfig, {
98+
defs: mpx.defs,
99+
inlineConfigFile: node_path.join(mpx.projectRoot, 'vue.config.js')
100+
})
101+
const configBox = loadPostcssConfig({
102+
emitWarning(warning) {
103+
compilation.warnings.push(warning)
104+
}
105+
}, inlineConfig)
106+
const plugins = [] // init with trim plugin
107+
plugins.push(
108+
pluginCondStrip({
109+
defs: mpx.defs
110+
})
111+
)
112+
const finalPluginsBox = configBox.then(config => config.prePlugins.concat(plugins, config.plugins))
93113
compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, async () => {
114+
const [plugins, options] = await Promise.all([finalPluginsBox, configBox.then(config => Object.assign(
115+
{
116+
to: '__uno.css',
117+
from: '__uno.css',
118+
map: false
119+
},
120+
config.options
121+
))])
94122
// 清空transformCache避免watch修改不生效
95123
transformCache.clear()
96124
const tokens = new Set()
@@ -110,14 +138,27 @@ function WebpackPlugin (configOrPath, defaults) {
110138
if (file === '*') { return }
111139
let code = compilation.assets[file].source().toString()
112140
let replaced = false
113-
code = code.replace(LAYER_PLACEHOLDER_RE, (_, quote, layer) => {
141+
for(const [source, quote, layer] of code.matchAll(LAYER_PLACEHOLDER_RE)) {
114142
replaced = true
115-
const css = layer === LAYER_MARK_ALL ? result.getLayers(undefined, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || ''
116-
if (!quote) { return css }
143+
const _css = layer === LAYER_MARK_ALL? result.getLayers(undefined, Array.from(entries).map((i) => resolveLayer(i)).filter((i) =>!!i)) : result.getLayer(layer) || ''
144+
const css = (await postcss(plugins).process(_css, options)).content
145+
if (!quote) {
146+
code = code.replace(source, css)
147+
continue
148+
}
117149
let escaped = JSON.stringify(css).slice(1, -1)
118150
if (quote === '\\"') { escaped = JSON.stringify(escaped).slice(1, -1) }
119-
return quote + escaped
120-
})
151+
code = code.replace(source, quote + escaped)
152+
}
153+
// code = code.replace(LAYER_PLACEHOLDER_RE, (_, quote, layer) => {
154+
// replaced = true
155+
// const _css = layer === LAYER_MARK_ALL ? result.getLayers(undefined, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || ''
156+
// const css = await postcss(plugins).process(_css, options)
157+
// if (!quote) { return css }
158+
// let escaped = JSON.stringify(css).slice(1, -1)
159+
// if (quote === '\\"') { escaped = JSON.stringify(escaped).slice(1, -1) }
160+
// return quote + escaped
161+
// })
121162
if (replaced) { compilation.assets[file] = new WebpackSources.RawSource(code) }
122163
}
123164
})

0 commit comments

Comments
 (0)