提交 340615c3 编写于 作者: fxy060608's avatar fxy060608

fix(cli): sourcemap

上级 f39c846c
import path from 'path'
import moduleAlias from 'module-alias'
import resovle, { SyncOpts } from 'resolve'
import { isInHBuilderX } from './env'
import type { Formatter } from '../logs/format'
......@@ -31,6 +32,7 @@ export function initModuleAlias() {
})
}
if (isInHBuilderX()) {
// 又是为了复用 HBuilderX 的插件逻辑,硬编码映射
Object.keys(hbxPlugins).forEach((name) => {
moduleAlias.addAlias(
name,
......@@ -40,6 +42,17 @@ export function initModuleAlias() {
)
)
})
// https://github.com/vitejs/vite/blob/892916d040a035edde1add93c192e0b0c5c9dd86/packages/vite/src/node/plugins/css.ts#L1481
const oldSync = resovle.sync
resovle.sync = (id: string, opts?: SyncOpts) => {
if ((hbxPlugins as any)[id]) {
return path.resolve(
process.env.UNI_HBUILDERX_PLUGINS,
hbxPlugins[id as keyof typeof hbxPlugins]
)
}
return oldSync(id, opts)
}
}
}
......@@ -56,6 +69,8 @@ export function installHBuilderXPlugin(plugin: string) {
)
}
const installPreprocessorTips: Record<string, boolean> = {}
export const moduleAliasFormatter: Formatter = {
test(msg) {
return msg.includes('Preprocessor dependency')
......@@ -74,6 +89,11 @@ export const moduleAliasFormatter: Formatter = {
preprocessor = 'compile-stylus'
}
if (lang) {
// 仅提醒一次
if (installPreprocessorTips[lang]) {
return ''
}
installPreprocessorTips[lang] = true
installHBuilderXPlugin(preprocessor)
return formatInstallHBuilderXPluginTips(lang, preprocessor)
}
......
......@@ -27,20 +27,23 @@ export function parseUniExtApis(vite = true) {
if (!fs.existsSync(pkgPath)) {
return
}
const exports = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))?.uni_modules?.[
'uni-ext-api'
] as Exports | undefined
if (exports) {
Object.assign(
injects,
parseInjects(
vite,
process.env.UNI_PLATFORM === 'h5' ? 'web' : process.env.UNI_PLATFORM,
`@/uni_modules/${uniModuleDir}`,
exports
try {
const exports = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
?.uni_modules?.['uni-ext-api'] as Exports | undefined
if (exports) {
Object.assign(
injects,
parseInjects(
vite,
process.env.UNI_PLATFORM === 'h5'
? 'web'
: process.env.UNI_PLATFORM,
`@/uni_modules/${uniModuleDir}`,
exports
)
)
)
}
}
} catch (e) {}
})
return injects
}
......
......@@ -223,7 +223,7 @@ export function uniViteInjectPlugin(
// 比如 App.vue 中,console.log('uniCloud') 触发了 inject 检测,检测完,发现不需要
// 此时返回 ast,会导致 import { setupApp } from '@dcloudio/uni-h5' 不会被编译
// ast
map: sourceMap ? magicString.generateMap({ hires: true }) : null,
map: null,
}
}
const importBlock = Array.from(newImports.values()).join('\n\n')
......
......@@ -92,9 +92,12 @@ export default function uniPlugin(
initPreContext(options.platform, process.env.UNI_CUSTOM_CONTEXT)
const plugins: Plugin[] = [
uniViteInjectPlugin('uni:ext-api-inject', parseUniExtApis()),
]
const plugins: Plugin[] = []
const injects = parseUniExtApis()
if (Object.keys(injects).length) {
plugins.push(uniViteInjectPlugin('uni:ext-api-inject', injects))
}
// 仅限 h5
if (options.viteLegacyOptions && options.platform === 'h5') {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册