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

feat: support vitest (#3398)

上级 06d67c91
import path from 'path'
process.env.UNI_CLI_CONTEXT = __dirname
process.env.UNI_INPUT_DIR = path.resolve(__dirname, 'src')
process.env.UNI_OUTPUT_DIR = path.resolve(__dirname, 'dist')
process.env.UNI_PLATFORM = 'h5'
import './env'
import path from 'path'
import { terser } from 'rollup-plugin-terser'
import uniH5VitePlugins from '@dcloudio/uni-h5-vite'
......
......@@ -34,7 +34,7 @@ const rollupPlugins = [
_NODE_JS_: 0,
},
// 忽略 pako 内部条件编译
exclude: ['**/pako/**/*.js'],
exclude: [/pako/ as unknown as string],
}),
babel({
babelHelpers: 'bundled',
......
......@@ -22,6 +22,11 @@ export function initModuleAlias() {
'vue/compiler-sfc': compilerSfcPath,
'vue/server-renderer': serverRendererPath,
})
if (process.env.VITEST) {
moduleAlias.addAliases({
vue: '@dcloudio/uni-h5-vue',
})
}
if (isInHBuilderX()) {
Object.keys(hbxPlugins).forEach((name) => {
moduleAlias.addAlias(
......
......@@ -3,6 +3,9 @@ import { hasOwn } from '@vue/shared'
export const pixelRatio = __NODE_JS__
? 1
: /*#__PURE__*/ (function () {
if (__PLATFORM__ === 'h5' && navigator.userAgent.includes('jsdom')) {
return 1
}
const canvas = document.createElement('canvas')
canvas.height = canvas.width = 0
const context = canvas.getContext('2d')
......@@ -134,7 +137,7 @@ export function initHidpi() {
// 会导致 canvas 无法绘制。
if (args[3] && typeof args[3] === 'number') {
args[3] *= pixelRatio
}
}
var font = this.font
this.font = font.replace(
......@@ -164,7 +167,7 @@ export function initHidpi() {
// 会导致 canvas 无法绘制。
if (args[3] && typeof args[3] === 'number') {
args[3] *= pixelRatio
}
}
var font = this.font
this.font = font.replace(
......
......@@ -17,7 +17,10 @@ export function uniResolveIdPlugin(): Plugin {
path.join(ownerModuleName, 'dist/uni-h5.es.js')
)
resolveCache['@dcloudio/uni-h5-vue'] = resolveBuiltIn(
path.join('@dcloudio/uni-h5-vue', `dist/vue.runtime.esm.js`)
path.join(
'@dcloudio/uni-h5-vue',
`dist/vue.runtime.${process.env.VITEST ? 'cjs' : 'esm'}.js`
)
)
},
resolveId(id) {
......
......@@ -6320,6 +6320,9 @@ function useResizeSensorLifecycle(rootRef, props2, update, reset) {
});
}
const pixelRatio = /* @__PURE__ */ function() {
if (navigator.userAgent.includes("jsdom")) {
return 1;
}
const canvas = document.createElement("canvas");
canvas.height = canvas.width = 0;
const context = canvas.getContext("2d");
......
......@@ -49,7 +49,15 @@ export function addConfigFile(inlineConfig: InlineConfig) {
return inlineConfig
}
export function initEnv(type: 'dev' | 'build', options: CliOptions) {
let initliazed = false
export function initEnv(
type: 'unknown' | 'dev' | 'build',
options: CliOptions
) {
if (initliazed) {
return
}
initliazed = true
if (options.platform === 'mp-360') {
console.error(M['mp.360.unsupported'])
process.exit(0)
......@@ -87,7 +95,8 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) {
process.env.UNI_SUB_PLATFORM = options.platform
options.platform = 'quickapp-webview'
}
process.env.VITE_ROOT_DIR = process.env.UNI_INPUT_DIR || process.cwd()
process.env.VITE_ROOT_DIR =
process.env.VITE_ROOT_DIR || process.env.UNI_INPUT_DIR || process.cwd()
process.env.UNI_INPUT_DIR =
process.env.UNI_INPUT_DIR || path.resolve(process.cwd(), 'src')
......
......@@ -27,7 +27,7 @@ import {
initPluginVueJsxOptions,
initPluginVueOptions,
} from './vue'
// import { createResolveId } from './resolveId'
import { initEnv } from './cli/utils'
const debugUni = debug('uni:plugin')
......@@ -63,6 +63,9 @@ export { runDev, runBuild } from './cli/action'
export default function uniPlugin(
rawOptions: VitePluginUniOptions = {}
): Plugin[] {
// 三方插件(如vitest)可能提供了自己的入口命令,需要补充 env 初始化逻辑
initEnv('unknown', { platform: process.env.UNI_PLATFORM || 'h5' })
const options: VitePluginUniResolvedOptions = {
...rawOptions,
base: '/',
......
......@@ -8,9 +8,9 @@ export function createResolveId(
const inputDir = normalizePath(options.inputDir)
return function (id) {
if (id.startsWith('@/')) {
return inputDir + id.substr(1)
return inputDir + id.slice(1)
} else if (id.startsWith('~@/')) {
return inputDir + id.substr(2)
return inputDir + id.slice(2)
}
}
}
......@@ -127,6 +127,7 @@ async function build(target) {
{
stdio: 'inherit',
env: Object.assign({ FORMAT: 'es' }, process.env),
cwd: pkgDir,
}
)
if (target === 'uni-h5') {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册