1
+ const postcss = require ( 'postcss' )
2
+ const pluginCondStrip = require ( '@mpxjs/webpack-plugin/lib/style-compiler/plugins/conditional-strip' )
1
3
const WebpackSources = require ( 'webpack-sources' )
2
4
const VirtualModulesPlugin = require ( 'webpack-virtual-modules' )
3
5
const node_path = require ( 'node:path' )
4
6
const process = require ( 'process' )
5
7
const fs = require ( 'fs' )
6
8
const { createContext, getPath, normalizeAbsolutePath } = require ( './utils' )
7
9
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' )
8
11
9
12
const PLUGIN_NAME = 'unocss:webpack'
10
13
const VIRTUAL_MODULE_PREFIX = node_path . resolve ( process . cwd ( ) , '_virtual_' )
@@ -89,8 +92,33 @@ function WebpackPlugin (configOrPath, defaults) {
89
92
}
90
93
} )
91
94
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 ) )
93
113
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
+ ) ) ] )
94
122
// 清空transformCache避免watch修改不生效
95
123
transformCache . clear ( )
96
124
const tokens = new Set ( )
@@ -110,14 +138,27 @@ function WebpackPlugin (configOrPath, defaults) {
110
138
if ( file === '*' ) { return }
111
139
let code = compilation . assets [ file ] . source ( ) . toString ( )
112
140
let replaced = false
113
- code = code . replace ( LAYER_PLACEHOLDER_RE , ( _ , quote , layer ) => {
141
+ for ( const [ source , quote , layer ] of code . matchAll ( LAYER_PLACEHOLDER_RE ) ) {
114
142
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
+ }
117
149
let escaped = JSON . stringify ( css ) . slice ( 1 , - 1 )
118
150
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
+ // })
121
162
if ( replaced ) { compilation . assets [ file ] = new WebpackSources . RawSource ( code ) }
122
163
}
123
164
} )
0 commit comments