From 1caf8a7be15f4b658e25499207b8426fbc506000 Mon Sep 17 00:00:00 2001 From: hiyuki <674883329@qq.com> Date: Wed, 10 Jan 2024 13:35:11 +0800 Subject: [PATCH] fix unocss web --- packages/unocss-plugin/lib/index.js | 1 + packages/unocss-plugin/lib/web-plugin/index.js | 8 ++++---- packages/unocss-plugin/lib/web-plugin/transform-loader.js | 2 ++ packages/unocss-plugin/lib/web-plugin/utils.js | 6 ++++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/unocss-plugin/lib/index.js b/packages/unocss-plugin/lib/index.js index 7068217e93..591f913dc2 100644 --- a/packages/unocss-plugin/lib/index.js +++ b/packages/unocss-plugin/lib/index.js @@ -96,6 +96,7 @@ function normalizeOptions (options) { exclude: scan.exclude || [], transformers: [ ...transformGroups ? [transformerVariantGroup()] : [], + // todo 由于enforce不为pre以及idFilter的存在,输出web时transformerDirectives暂时无法对.mpx中的样式文件生效,待优化改进 ...transformCSS ? [transformerDirectives()] : [] ], ...webOptions diff --git a/packages/unocss-plugin/lib/web-plugin/index.js b/packages/unocss-plugin/lib/web-plugin/index.js index e24ec60fc1..1725f3849e 100644 --- a/packages/unocss-plugin/lib/web-plugin/index.js +++ b/packages/unocss-plugin/lib/web-plugin/index.js @@ -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() @@ -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() @@ -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() diff --git a/packages/unocss-plugin/lib/web-plugin/transform-loader.js b/packages/unocss-plugin/lib/web-plugin/transform-loader.js index 0f00878059..091d42ec62 100644 --- a/packages/unocss-plugin/lib/web-plugin/transform-loader.js +++ b/packages/unocss-plugin/lib/web-plugin/transform-loader.js @@ -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 { diff --git a/packages/unocss-plugin/lib/web-plugin/utils.js b/packages/unocss-plugin/lib/web-plugin/utils.js index 3aaf50379f..06581bf37b 100644 --- a/packages/unocss-plugin/lib/web-plugin/utils.js +++ b/packages/unocss-plugin/lib/web-plugin/utils.js @@ -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 }