Skip to content

Commit

Permalink
feat(uni-app-x mp): 增加全局virtualHost、ucss样式重置开关
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangyaqi committed Nov 29, 2024
1 parent 210ad6a commit 417231e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
22 changes: 12 additions & 10 deletions packages/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ declare var __PLUS__: boolean
declare var __VUE_OPTIONS_API__: boolean
declare var __VUE_PROD_DEVTOOLS__: boolean

declare var __UNI_FEATURE_VIRTUAL_HOST__: boolean
declare var __UNI_FEATURE_UCSS_RESET__: boolean
declare var __UNI_FEATURE_WX__: boolean
declare var __UNI_FEATURE_WXS__: boolean
declare var __UNI_FEATURE_RPX__: boolean
Expand Down Expand Up @@ -136,16 +138,16 @@ interface Uni {
openDialogPage: (options: {
url: string
animationType?:
| 'auto'
| 'none'
| 'slide-in-right'
| 'slide-in-left'
| 'slide-in-top'
| 'slide-in-bottom'
| 'fade-in'
| 'zoom-out'
| 'zoom-fade-out'
| null
| 'auto'
| 'none'
| 'slide-in-right'
| 'slide-in-left'
| 'slide-in-top'
| 'slide-in-bottom'
| 'fade-in'
| 'zoom-out'
| 'zoom-fade-out'
| null
animationDuration?: number | null
disableEscBack?: boolean | null
parentPage?: UniPage | null
Expand Down
20 changes: 20 additions & 0 deletions packages/uni-cli-shared/src/env/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,28 @@ export function initDefine(stringifyBoolean: boolean = false) {
const platformManifestJson = getPlatformManifestJsonOnce()
const isRunByHBuilderX = runByHBuilderX()
const isDebug = !!manifestJson.debug
const isX = process.env.UNI_APP_X === 'true'
const isMP = process.env.UNI_PLATFORM.startsWith('mp')

process.env['UNI_APP_ID'] = manifestJson.appid

let mpXDefine =
isX && isMP
? {
__UNI_FEATURE_UCSS_RESET__: true,
__UNI_FEATURE_VIRTUAL_HOST__: true,
}
: {
__UNI_FEATURE_UCSS_RESET__: false,
__UNI_FEATURE_VIRTUAL_HOST__: false,
}
if (isX && isMP) {
mpXDefine.__UNI_FEATURE_UCSS_RESET__ =
platformManifestJson.enableUcssReset !== false
mpXDefine.__UNI_FEATURE_VIRTUAL_HOST__ =
platformManifestJson.enableVirtualHost !== false
}

return {
...initCustomDefine(),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
Expand Down Expand Up @@ -58,6 +77,7 @@ export function initDefine(stringifyBoolean: boolean = false) {
'process.env.VUE_APP_DARK_MODE': JSON.stringify(
platformManifestJson.darkmode || false
),
...mpXDefine,
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/uni-cli-shared/src/json/mp/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const NON_APP_JSON_KEYS = [
'uniStatistics',
'mergeVirtualHostAttributes',
'styleIsolation',
'enableVirtualHost',
'enableUcssReset',
]

export function mergeMiniProgramAppJson(
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-mp-core/src/runtime/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function parseComponent(
pureDataPattern: /^uP$/,
}

if (__X__ && __PLATFORM__ === 'mp-weixin' && !isPageInProject) {
if (__X__ && __UNI_FEATURE_VIRTUAL_HOST__ && !isPageInProject) {
options.virtualHost = true
}

Expand Down
4 changes: 4 additions & 0 deletions packages/uni-mp-vite/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ export function uniMiniProgramPlugin(
}

export function genUVueCssCode(manifestJson: Record<string, any>) {
const platformConfig = manifestJson[process.env.UNI_PLATFORM] || {}
if (platformConfig.enableUcssReset === false) {
return ''
}
let cssCode = fs.readFileSync(
path.resolve(__dirname, '../../lib/uvue.css'),
'utf8'
Expand Down
10 changes: 3 additions & 7 deletions packages/uni-mp-vite/src/plugin/uni/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ export function uniOptions({
}): UniVitePlugin['uni'] {
const manifest = parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
const platformOptions = manifest[process.env.UNI_PLATFORM] || {}

const isX = process.env.UNI_APP_X === 'true'
const mergeVirtualHostAttributes =
platformOptions.mergeVirtualHostAttributes != null
? platformOptions.mergeVirtualHostAttributes
: process.env.UNI_PLATFORM === 'mp-weixin' &&
process.env.UNI_APP_X === 'true'
: isX

return {
copyOptions,
Expand All @@ -46,10 +45,7 @@ export function uniOptions({
mergeVirtualHostAttributes,
}),
}),
isNativeTag:
process.env.UNI_APP_X === 'true'
? isMiniProgramUVueNativeTag
: isMiniProgramNativeTag,
isNativeTag: isX ? isMiniProgramUVueNativeTag : isMiniProgramNativeTag,
isCustomElement: createIsCustomElement(customElements),
...compilerOptions,
nodeTransforms: [
Expand Down

0 comments on commit 417231e

Please sign in to comment.