Skip to content

Commit

Permalink
chore(x-ios): 自动替换内置页面路径
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Jan 10, 2025
1 parent b31d939 commit 3fd6208
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 23 additions & 2 deletions packages/uni-app-plus/x.vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
} from '@dcloudio/uni-cli-shared'
import { isAppIOSUVueNativeTag } from '@dcloudio/uni-shared'
import autoprefixer from 'autoprefixer'
import { uts2ts, writePagesFile } from '../../scripts/ext-api'
import { uts2ts, syncPagesFile } from '../../scripts/ext-api'

import { initUniAppJsEngineCssPlugin } from '@dcloudio/uni-app-uts'
import { OutputChunk } from 'rollup'

function resolve(file: string) {
return path.resolve(__dirname, file)
Expand All @@ -24,7 +25,7 @@ process.env.UNI_APP_X = 'true'
process.env.UNI_UTS_PLATFORM = 'app-ios'
initPreContext('app', {}, 'app-ios', true)

writePagesFile()
const systemPagePaths = syncPagesFile()

const rollupPlugins = [
replace({
Expand Down Expand Up @@ -139,6 +140,26 @@ export default defineConfig({
},
}),
vueJsx({ optimize: true, isCustomElement: isAppIOSUVueNativeTag }),
{
name: 'uni:replace-page-paths',
generateBundle(_, bundle) {
Object.keys(bundle).forEach((key) => {
if (key.endsWith('.js')) {
const chunk = bundle[key] as OutputChunk
let newCode = chunk.code
Object.keys(systemPagePaths).forEach((path) => {
if (newCode.includes(path)) {
newCode = newCode.replace(
new RegExp(path, 'g'),
systemPagePaths[path]
)
}
})
chunk.code = newCode
}
})
},
},
],
build: {
emptyOutDir: false,
Expand Down
7 changes: 6 additions & 1 deletion scripts/ext-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ async function checkExtApiTypes(target: Target) {
})
}

export function writePagesFile() {
export function syncPagesFile() {
const systemPagePaths: Record<string, string> = {}
const apiDir = process.env.UNI_APP_EXT_API_DIR
if (apiDir && fs.existsSync(apiDir)) {
const importCodes: string[] = []
Expand All @@ -179,6 +180,9 @@ export function writePagesFile() {
disableSwipeBack: false,
})`
)
systemPagePaths[
`/uni_modules/${module}/pages/${page}/${page}`
] = `uni:${page}`
}
})
}
Expand All @@ -199,4 +203,5 @@ ${registerCodes.join('\n')}
`
)
}
return systemPagePaths
}

0 comments on commit 3fd6208

Please sign in to comment.