Skip to content

Commit

Permalink
fix: uno存储问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mater1996 committed Dec 10, 2024
1 parent cc0cbf8 commit 13fd97d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 71 deletions.
27 changes: 14 additions & 13 deletions packages/unocss-plugin/lib/rn-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ const reLetters = /[a-z]+/gi
function WebpackPlugin (configOrPath, defaults) {
return {
apply (compiler) {
const ctx = createContext(configOrPath, defaults)
const { filter, transformCache } = ctx
compiler.__unoCtx = ctx
// transform 提取tokens
compiler.options.module.rules.unshift({
enforce: 'pre',
use: (data) => {
if (data.resource == null) { return [] }

const id = normalizeAbsolutePath(data.resource + (data.resourceQuery || ''))
if (filter('', id) && !id.match(/\.html$/) && !RESOLVED_ID_RE.test(id)) {
if (compiler.__unoCtx.filter('', id) && !id.match(/\.html$/) && !RESOLVED_ID_RE.test(id)) {
return [{
loader: nodePath.resolve(__dirname, '../web-plugin/transform-loader')
}]
Expand All @@ -36,15 +33,14 @@ function WebpackPlugin (configOrPath, defaults) {
}
})



compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const mpx = compilation.__mpx__
const { mode, srcMode } = mpx
compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, async () => {
await ctx.ready
const mpx = compilation.__mpx__
const { mode, srcMode } = mpx
const ctx = compiler.__unoCtx
const uno = ctx.uno
// 清空transformCache避免watch修改不生效
transformCache.clear()
ctx.transformCache.clear()
const tokens = new Set()
for (const module of compilation.modules) {
const assetsInfo = module.buildInfo.assetsInfo || new Map()
Expand All @@ -56,7 +52,6 @@ function WebpackPlugin (configOrPath, defaults) {
}
}
}
const { uno } = ctx
const result = await uno.generate(tokens, { minify: true })
if (uno._mpx2rnUnsuportedRules && uno._mpx2rnUnsuportedRules.length) {
compilation.errors.push(`[Mpx Unocss]: all those '${uno._mpx2rnUnsuportedRules.join(', ')}' class utilities is not supported in react native mode`)
Expand Down Expand Up @@ -98,9 +93,15 @@ function WebpackPlugin (configOrPath, defaults) {
})
})

compiler.hooks.make.tapPromise(PLUGIN_NAME, (compilation) => {
compiler.hooks.thisCompilation.tap('MpxWebpackPlugin', (compilation) => {
const mpx = compilation.__mpx__
return ctx.ready.then(() => mpx.unoCtx = ctx.uno)
mpx.unoCtx = compiler.__unoCtx.uno
})

compiler.hooks.beforeCompile.tapPromise(PLUGIN_NAME, async (compilation) => {
const ctx = await createContext(configOrPath, defaults)
compiler.__unoCtx = ctx
return ctx
})
}
}
Expand Down
23 changes: 17 additions & 6 deletions packages/unocss-plugin/lib/web-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const VIRTUAL_MODULE_PREFIX = nodePath.resolve(process.cwd(), '_virtual_')
function WebpackPlugin (configOrPath, defaults) {
return {
apply (compiler) {
const ctx = createContext(configOrPath, defaults)
const { uno, filter, transformCache } = ctx
const entries = new Set()
const __vfsModules = new Set()
let __vfs = null
Expand All @@ -39,7 +37,7 @@ function WebpackPlugin (configOrPath, defaults) {
__vfs = new VirtualModulesPlugin()
compiler.options.plugins.push(__vfs)
}
compiler.__unoCtx = ctx

// 添加解析虚拟模块插件 import 'uno.css' 并且注入layer代码
const resolverPlugin = {
apply (resolver) {
Expand Down Expand Up @@ -91,7 +89,7 @@ function WebpackPlugin (configOrPath, defaults) {
if (data.resource == null) { return [] }

const id = normalizeAbsolutePath(data.resource + (data.resourceQuery || ''))
if (filter('', id) && !id.match(/\.html$/) && !RESOLVED_ID_RE.test(id)) {
if (compiler.__unoCtx.filter('', id) && !id.match(/\.html$/) && !RESOLVED_ID_RE.test(id)) {
return [{
loader: nodePath.resolve(__dirname, './transform-loader')
}]
Expand All @@ -103,9 +101,10 @@ function WebpackPlugin (configOrPath, defaults) {

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, async () => {
await ctx.ready
const ctx = compiler.__unoCtx
const uno = ctx.uno
// 清空transformCache避免watch修改不生效
transformCache.clear()
ctx.transformCache.clear()
const tokens = new Set()
for (const module of compilation.modules) {
const assetsInfo = module.buildInfo.assetsInfo || new Map()
Expand Down Expand Up @@ -135,9 +134,21 @@ function WebpackPlugin (configOrPath, defaults) {
}
})
})

compiler.hooks.thisCompilation.tap('MpxWebpackPlugin', (compilation) => {
const mpx = compilation.__mpx__
mpx.unoCtx = compiler.__unoCtx.uno
})

compiler.hooks.make.tapPromise(PLUGIN_NAME, async (compilation) => {
const ctx = await createContext(configOrPath, defaults)
compiler.__unoCtx = ctx
return ctx
})
}
}
}

function getLayer (id) {
let layer = resolveLayer(getPath(id))
if (!layer) {
Expand Down
1 change: 0 additions & 1 deletion packages/unocss-plugin/lib/web-plugin/transform-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ async function transform (code, map) {
const ctx = this._compiler.__unoCtx
const mpx = this.getMpx()
if (!ctx || !mpx) return callback(null, code, map)
await ctx.ready
// 使用resourcePath而不是resource作为id,规避query的影响
const id = this.resourcePath
const { extract, transformCache } = ctx
Expand Down
93 changes: 42 additions & 51 deletions packages/unocss-plugin/lib/web-plugin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,53 @@ const sfcIdRE = /\.(vue|mpx)($|\?)/
const templateIdRE = /\.(wxml|axml|swan|qml|ttml|qxml|jxml|ddml|html)($|\?)/
const cssIdRE = /\.(wxss|acss|css|qss|ttss|jxss|ddss)($|\?)/

function createContext (configOrPath, defaults = {}, extraConfigSources = []) {
async function createContext (configOrPath, defaults = {}, extraConfigSources = []) {
const root = process.cwd()
let rawConfig = {}
const ctx = {
get ready () {
return ready
},
filter,
uno: undefined,
extract,
transformCache: new Map()
}
const unoPromise = core.createGenerator(rawConfig, defaults)

const uno = await core.createGenerator(rawConfig, defaults)
let rollupFilter = pluginutils.createFilter(defaultInclude, defaultExclude)
const idFilter = pluginutils.createFilter([sfcIdRE, templateIdRE, cssIdRE, core.cssIdRE])
const ready = reloadConfig()

async function reloadConfig () {
const uno = ctx.uno = await unoPromise
const result = await config.loadConfig(root, configOrPath, extraConfigSources, defaults)
rawConfig = result.config
uno.setConfig(rawConfig)
rollupFilter = pluginutils.createFilter(
rawConfig.include || defaultInclude,
rawConfig.exclude || defaultExclude
)
const presets = /* @__PURE__ */ new Set()
uno.config.presets.forEach((i) => {
if (!i.name) {
return
}
if (presets.has(i.name)) {
console.warn(`[unocss] duplication of preset ${i.name} found, there might be something wrong with your config.`)
} else {
presets.add(i.name)
}
})

const transformers = uno.config.transformers
if (transformers) {
const pre = []
const normal = []
const post = []
transformers.forEach(i => {
if (i.enforce === 'pre') pre.push(i)
else if (i.enforce === 'post') post.push(i)
else normal.push(i)
})
uno.config.transformers = [
...pre,
...normal,
...post
]
const result = await config.loadConfig(root, configOrPath, extraConfigSources, defaults)
rawConfig = result.config
uno.setConfig(rawConfig)
rollupFilter = pluginutils.createFilter(
rawConfig.include || defaultInclude,
rawConfig.exclude || defaultExclude
)
const presets = /* @__PURE__ */ new Set()
uno.config.presets.forEach((i) => {
if (!i.name) {
return
}
if (presets.has(i.name)) {
console.warn(`[unocss] duplication of preset ${i.name} found, there might be something wrong with your config.`)
} else {
presets.add(i.name)
}
})

return result
const transformers = uno.config.transformers
if (transformers) {
const pre = []
const normal = []
const post = []
transformers.forEach(i => {
if (i.enforce === 'pre') pre.push(i)
else if (i.enforce === 'post') post.push(i)
else normal.push(i)
})
uno.config.transformers = [
...pre,
...normal,
...post
]
}

async function extract (code, id) {
const tokens = new Set()
await ctx.uno.applyExtractors(code, id, tokens)
await uno.applyExtractors(code, id, tokens)
if (tokens.size > 0) {
this.emitFile(id, '', undefined, {
skipEmit: true,
Expand All @@ -94,7 +80,12 @@ function createContext (configOrPath, defaults = {}, extraConfigSources = []) {
return code.includes(INCLUDE_COMMENT) || code.includes(CSS_PLACEHOLDER) || rollupFilter(id.replace(/\?v=\w+$/, ''))
}

return ctx
return {
filter,
uno,
extract,
transformCache: new Map()
}
}

async function applyTransformers (ctx, original, id) {
Expand Down

0 comments on commit 13fd97d

Please sign in to comment.