Skip to content

Commit

Permalink
fix unocss web
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki committed Jan 10, 2024
1 parent 7dbc62d commit 1caf8a7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/unocss-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function normalizeOptions (options) {
exclude: scan.exclude || [],
transformers: [
...transformGroups ? [transformerVariantGroup()] : [],
// todo 由于enforce不为pre以及idFilter的存在,输出web时transformerDirectives暂时无法对.mpx中的样式文件生效,待优化改进
...transformCSS ? [transformerDirectives()] : []
],
...webOptions
Expand Down
8 changes: 4 additions & 4 deletions packages/unocss-plugin/lib/web-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function WebpackPlugin (configOrPath, defaults) {
return {
apply (compiler) {
const ctx = createContext(configOrPath, defaults)
const { uno, filter } = ctx
const { uno, filter, transformCache } = ctx
const entries = new Set()
const __vfsModules = new Set()
const __vfs = new VirtualModulesPlugin()
Expand Down Expand Up @@ -82,7 +82,8 @@ function WebpackPlugin (configOrPath, defaults) {

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, async () => {
// 可以收集到cache中的tokens,可解决存在cache,二次serve中无法获取tokens的问题
// 清空transformCache避免watch修改不生效
transformCache.clear()
const tokens = new Set()
for (const module of compilation.modules) {
const assetsInfo = module.buildInfo.assetsInfo || new Map()
Expand All @@ -94,9 +95,8 @@ function WebpackPlugin (configOrPath, defaults) {
}
}
}
const files = Object.keys(compilation.assets)
const result = await uno.generate(tokens, { minify: true })

const files = Object.keys(compilation.assets)
for (const file of files) {
if (file === '*') { return }
let code = compilation.assets[file].source().toString()
Expand Down
2 changes: 2 additions & 0 deletions packages/unocss-plugin/lib/web-plugin/transform-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ async function transform (code, map) {
const ctx = this._compiler.__unoCtx
if (!ctx) return callback(null, code, map)
await ctx.ready
// 使用resourcePath而不是resource作为id,规避query的影响
const id = this.resourcePath
const { extract, transformCache } = ctx
let res
// 通过transformCache减少不必要的重复的transform/extract行为,如对于.mpx/.vue文件及其block request(template/style/script)进行重复transform/extract
if (transformCache.has(id)) {
res = transformCache.get(id)
} else {
Expand Down
6 changes: 6 additions & 0 deletions packages/unocss-plugin/lib/web-plugin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ function createContext (configOrPath, defaults = {}, extraConfigSources = []) {
presets.add(i.name)
}
})
const nonPreTransformers = uno.config.transformers?.filter((i) => i.enforce !== 'pre')
if (nonPreTransformers?.length) {
console.warn(
'[unocss] webpack integration only supports "pre" enforce transformers currently.the following transformers will be ignored\n' + nonPreTransformers.map((i) => ` - ${i.name}`).join('\n')
)
}
return result
}

Expand Down

0 comments on commit 1caf8a7

Please sign in to comment.