niucloud/uni-app/scripts/vite-plugin-force-shared-external.cjs
wangchen14709853322 6d9c8ff7f5 v2.0-beta-20260626
v2框架公测版测试流程请看v2.0-beta.md
2026-07-01 12:15:20 +08:00

60 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 强制 vue / pinia / vue-i18n / @/locale 走 import map避免 core 与 addon 各打一份运行时
*/
const {
isSharedExternal,
isWapLocaleExternal,
isDiyCoreExternal,
wapLocaleExternalPath,
diyCoreExternalPath,
IMPORT_MAP
} = require('./shared-external.cjs')
function sharedModuleUrl(id) {
if (isDiyCoreExternal(id)) return diyCoreExternalPath()
if (isWapLocaleExternal(id)) return wapLocaleExternalPath()
if (IMPORT_MAP[id]) return IMPORT_MAP[id]
for (const pkg of Object.keys(IMPORT_MAP)) {
if (pkg.startsWith('@')) continue
if (id === pkg || id.startsWith(`${pkg}/`)) return IMPORT_MAP[pkg]
}
return null
}
function createForceSharedExternalPlugin() {
return {
name: 'force-shared-external',
enforce: 'pre',
resolveId(id) {
if (isDiyCoreExternal(id)) {
return { id: diyCoreExternalPath(), external: true }
}
if (!isSharedExternal(id) && !isWapLocaleExternal(id)) return null
const url = sharedModuleUrl(id)
return { id: url || id, external: true }
}
}
}
/** alias 解析后再次 externalize locale / diy-core */
function createForceLocaleExternalPostPlugin() {
return {
name: 'force-locale-external-post',
enforce: 'post',
resolveId(id) {
if (isDiyCoreExternal(id)) {
return { id: diyCoreExternalPath(), external: true }
}
if (!isWapLocaleExternal(id)) return null
return { id: wapLocaleExternalPath(), external: true }
}
}
}
function sharedOutputPaths(id) {
const url = sharedModuleUrl(id)
return url || undefined
}
module.exports = { createForceSharedExternalPlugin, createForceLocaleExternalPostPlugin, sharedOutputPaths, sharedModuleUrl }