Skip to content

Commit 335197a

Browse files
committed
feat: dom2-mp下样式隔离时加入uvue.wxss的处理
1 parent 6d7a277 commit 335197a

File tree

3 files changed

+96
-27
lines changed

3 files changed

+96
-27
lines changed

packages/uni-cli-shared/src/mp/externalClasses.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isStringLiteral,
88
} from '@babel/types'
99
import { walk } from 'estree-walker'
10+
import { normalizePath } from '../utils'
1011

1112
const externalClassesCache = new Map<
1213
string,
@@ -25,19 +26,27 @@ export interface PageExternalClassesInfo {
2526
hasAppAndPageStyle?: boolean
2627
}
2728

28-
const pageStyleIsolationCache = new Map<string, boolean>() // 子组件是否存在 styleIsolation 为app
29+
export type StyleIsolation = 'isolated' | 'app' | 'app-and-page'
30+
31+
const pageStyleIsolationCache = new Map<
32+
string,
33+
{
34+
styleIsolation: StyleIsolation
35+
isPage: boolean
36+
}
37+
>()
2938

3039
const pageExternalClassesCache = new Map<string, PageExternalClassesInfo>()
3140

3241
export function findPageExternalClasses(filename: string) {
33-
return pageExternalClassesCache.get(filename)
42+
return pageExternalClassesCache.get(normalizePath(filename))
3443
}
3544

3645
export function updatePageExternalClasses(
3746
filename: string,
3847
info: PageExternalClassesInfo
3948
) {
40-
pageExternalClassesCache.set(filename, info)
49+
pageExternalClassesCache.set(normalizePath(filename), info)
4150
}
4251

4352
export function addPageExternalClasses(
@@ -46,10 +55,11 @@ export function addPageExternalClasses(
4655
hasDynamic: boolean,
4756
hasAppAndPageStyle?: boolean
4857
) {
49-
let info = pageExternalClassesCache.get(filename)
58+
const normalizedFilename = normalizePath(filename)
59+
let info = pageExternalClassesCache.get(normalizedFilename)
5060
if (!info) {
5161
info = { staticClasses: new Set(), hasDynamic: false }
52-
pageExternalClassesCache.set(filename, info)
62+
pageExternalClassesCache.set(normalizedFilename, info)
5363
}
5464
staticClasses.forEach((cls) => info!.staticClasses.add(cls))
5565
if (hasDynamic) {
@@ -61,22 +71,22 @@ export function addPageExternalClasses(
6171
}
6272

6373
export function clearPageExternalClasses(filename: string) {
64-
pageExternalClassesCache.delete(filename)
74+
pageExternalClassesCache.delete(normalizePath(filename))
6575
}
6676

6777
export function hasExternalClasses(code: string) {
6878
return code.includes('externalClasses')
6979
}
7080

7181
export function findMiniProgramComponentExternalClasses(filename: string) {
72-
return externalClassesCache.get(filename)
82+
return externalClassesCache.get(normalizePath(filename))
7383
}
7484

7585
export function updateMiniProgramComponentExternalClasses(
7686
filename: string,
7787
value: { mtime: number; classes: string[] }
7888
) {
79-
externalClassesCache.set(filename, value)
89+
externalClassesCache.set(normalizePath(filename), value)
8090
}
8191

8292
export function parseExternalClasses(ast: Program) {
@@ -103,8 +113,8 @@ export function parseExternalClasses(ast: Program) {
103113
return classes
104114
}
105115

106-
export function parseStyleIsolation(ast: Program) {
107-
let styleIsolationValue = ''
116+
export function parseStyleIsolation(ast: Program): StyleIsolation | '' {
117+
let styleIsolationValue: StyleIsolation | '' = ''
108118
;(walk as any)(ast, {
109119
enter(child: Node, parent: Node) {
110120
if (!isIdentifier(child) || child.name !== 'styleIsolation') {
@@ -116,22 +126,36 @@ export function parseStyleIsolation(ast: Program) {
116126
if (!isStringLiteral(parent.value)) {
117127
return
118128
}
119-
if (parent.value.value === 'app') {
120-
styleIsolationValue = 'app'
129+
if (
130+
parent.value.value === 'app' ||
131+
parent.value.value === 'app-and-page' ||
132+
parent.value.value === 'isolated'
133+
) {
134+
styleIsolationValue = parent.value.value
121135
}
122136
return parent.value.value
123137
},
124138
})
125139
return styleIsolationValue
126140
}
127141

142+
/**
143+
* 目前只有小程序平台才会走这个逻辑
144+
* @param pagePahth
145+
* @param value
146+
* @param isPage
147+
*/
128148
export function updateMiniProgramComponentStyleIsolation(
129149
pagePahth: string,
130-
value: boolean
150+
value: StyleIsolation,
151+
isPage = false
131152
) {
132-
pageStyleIsolationCache.set(pagePahth, value)
153+
pageStyleIsolationCache.set(normalizePath(pagePahth), {
154+
styleIsolation: value,
155+
isPage,
156+
})
133157
}
134158

135159
export function findMiniProgramComponentStyleIsolation(pagePahth: string) {
136-
return pageStyleIsolationCache.get(pagePahth)
160+
return pageStyleIsolationCache.get(normalizePath(pagePahth))
137161
}

packages/uni-mp-vite/src/plugin/configResolved.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
createEncryptCssUrlReplacer,
77
createShadowImageUrl,
88
cssPostPlugin,
9+
findMiniProgramComponentStyleIsolation,
910
getUniModulesEncryptType,
1011
injectAssetPlugin,
1112
injectCssPlugin,
@@ -30,6 +31,7 @@ import {
3031
parseVirtualComponentPath,
3132
parseVirtualPagePath,
3233
} from '../plugins/entry'
34+
import path from 'path'
3335

3436
const debugNVueCss = debug('uni:nvue-css')
3537
const cssVars = `page{--status-bar-height:25px;--top-window-height:0px;--window-top:0px;--window-bottom:0px;--window-left:0px;--window-right:0px;--window-magin:0px}`
@@ -185,11 +187,35 @@ export function createConfigResolved({
185187
(page.style as any).renderer === 'skyline')
186188

187189
if (!shouldNotResetStyle) {
188-
/**
189-
* 兼容发布为小程序分包模式
190-
*/
191-
const uvueCssPath = relativeFile(filename, `uvue${extname}`)
192-
cssCode = `@import "${uvueCssPath}";\n` + cssCode
190+
let addUvueCss = false
191+
if (
192+
process.env.UNI_APP_STYLE_ISOLATION_VERSION === '2' &&
193+
process.env.UNI_APP_X === 'true'
194+
) {
195+
const basePath = path.join(
196+
process.env.UNI_INPUT_DIR!,
197+
removeExt(filename)
198+
)
199+
const styleIsolation =
200+
findMiniProgramComponentStyleIsolation(basePath + '.uvue') ||
201+
findMiniProgramComponentStyleIsolation(basePath + '.vue')
202+
if (
203+
styleIsolation &&
204+
styleIsolation.isPage &&
205+
styleIsolation.styleIsolation === 'isolated'
206+
) {
207+
addUvueCss = true
208+
}
209+
} else {
210+
addUvueCss = true
211+
}
212+
if (addUvueCss) {
213+
/**
214+
* 兼容发布为小程序分包模式
215+
*/
216+
const uvueCssPath = relativeFile(filename, `uvue${extname}`)
217+
cssCode = `@import "${uvueCssPath}";\n` + cssCode
218+
}
193219
}
194220
return cssCode
195221
}

packages/uni-mp-vite/src/plugin/polyfill.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { extend } from '@vue/shared'
22
import { once } from '@dcloudio/uni-shared'
33
import {
44
findMiniProgramComponentStyleIsolation,
5+
isAppVue,
56
isMiniProgramPageFile,
67
parseStyleIsolation,
78
resolveBuiltIn,
@@ -36,8 +37,21 @@ function rewriteCompileScript() {
3637
compiler.compileStyleAsync = (
3738
options: SFCAsyncStyleCompileOptions
3839
): Promise<SFCStyleCompileResults> => {
39-
if (findMiniProgramComponentStyleIsolation(options.filename)) {
40-
options.source = `@import "/app.wxss";\n` + options.source
40+
if (
41+
process.env.UNI_APP_STYLE_ISOLATION_VERSION === '2' &&
42+
process.env.UNI_APP_X === 'true' &&
43+
isAppVue(options.filename)
44+
) {
45+
options.source = `@import "/uvue.wxss";\n` + options.source
46+
}
47+
const { styleIsolation, isPage } =
48+
findMiniProgramComponentStyleIsolation(options.filename) || {}
49+
if (!isPage) {
50+
if (styleIsolation === 'app') {
51+
options.source = `@import "/app.wxss";\n` + options.source
52+
} else if (styleIsolation === 'isolated') {
53+
options.source = `@import "/uvue.wxss";\n` + options.source
54+
}
4155
}
4256
// https://github.com/dcloudio/uni-app/issues/4076
4357
options.isProd = true
@@ -56,6 +70,7 @@ function rewriteCompileScript() {
5670
// https://github.com/dcloudio/uni-app/issues/4076
5771
// dev模式下,会生成:{ "83a5a03c-style.color": style.color}
5872
options.isProd = true
73+
let res = compileScript(sfc, options)
5974
if (
6075
process.env.UNI_APP_STYLE_ISOLATION_VERSION === '2' &&
6176
process.env.UNI_APP_X === 'true' &&
@@ -66,11 +81,15 @@ function rewriteCompileScript() {
6681
sfc.filename,
6782
process.env.UNI_INPUT_DIR
6883
)
69-
}
70-
let res = compileScript(sfc, options)
71-
// @ts-expect-error
72-
if (!options.__isPage && parseStyleIsolation(res)) {
73-
updateMiniProgramComponentStyleIsolation(sfc.filename, true)
84+
const styleIsolation = parseStyleIsolation(res)
85+
if (styleIsolation) {
86+
// @ts-expect-error
87+
updateMiniProgramComponentStyleIsolation(
88+
sfc.filename,
89+
styleIsolation,
90+
options.__isPage
91+
)
92+
}
7493
}
7594
return res
7695
}

0 commit comments

Comments
 (0)