* localCDN 自定义配置测试
* 测试文档中描述的自定义配置功能
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { execSync } from 'node:child_process'
import { ensureEnvVarEnabled, backupEnvFile, restoreEnvFile } from './utils/envHelpers.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const projectRoot = path.resolve(__dirname, '..')
const viteConfigPath = path.resolve(projectRoot, 'vite.config.js')
const envAlphaPath = path.resolve(projectRoot, 'env', '.env.alpha')
const originalViteConfig = fs.readFileSync(viteConfigPath, 'utf-8')
const distDir = path.resolve(projectRoot, 'dist')
* 更新 registry.js 文件以支持自定义 importMap
*/
function updateRegistryFile() {
const registryPath = path.resolve(projectRoot, 'registry.js')
if (!fs.existsSync(registryPath)) {
return
}
fs.copyFileSync(registryPath, registryPath + '.bak')
const registryContent = fs.readFileSync(registryPath, 'utf-8')
const updatedContent = registryContent.replace(
/config: {([^}]*)}/,
`config: {$1,
importMap: {
imports: {
'vue': "\${VITE_CDN_DOMAIN}/vue\${versionDelimiter}3.4.21\${fileDelimiter}/dist/vue.runtime.esm-browser.js"
}
}
}`
)
fs.writeFileSync(registryPath, updatedContent)
}
* 恢复 registry.js 文件
*/
function restoreRegistryFile() {
const registryPath = path.resolve(projectRoot, 'registry.js')
const backupPath = registryPath + '.bak'
if (fs.existsSync(backupPath)) {
fs.copyFileSync(backupPath, registryPath)
fs.unlinkSync(backupPath)
}
}
describe('localCDN 自定义配置测试', () => {
beforeAll(() => {
fs.writeFileSync(viteConfigPath + '.bak', originalViteConfig)
backupEnvFile(envAlphaPath)
const updatedViteConfig = originalViteConfig.replace(
'const baseConfig = useTinyEngineBaseConfig({',
`const baseConfig = useTinyEngineBaseConfig({
importMapLocalConfig: {
importMap: {
imports: {
'vue': "\${VITE_CDN_DOMAIN}/vue\${versionDelimiter}3.4.21\${fileDelimiter}/dist/vue.runtime.esm-browser.prod.js"
}
},
copy: {
'vue': {
filePathInPackage: '/dist/'
}
}
},`
)
fs.writeFileSync(viteConfigPath, updatedViteConfig)
let envContent = fs.readFileSync(envAlphaPath, 'utf-8')
envContent = ensureEnvVarEnabled(envContent, 'VITE_LOCAL_IMPORT_MAPS')
envContent = ensureEnvVarEnabled(envContent, 'VITE_LOCAL_BUNDLE_DEPS')
envContent = ensureEnvVarEnabled(envContent, 'VITE_LOCAL_IMPORT_PATH', 'local-cdn-static')
fs.writeFileSync(envAlphaPath, envContent)
updateRegistryFile()
execSync('pnpm run build:alpha', {
cwd: projectRoot,
stdio: 'inherit'
})
})
afterAll(() => {
if (fs.existsSync(viteConfigPath + '.bak')) {
fs.copyFileSync(viteConfigPath + '.bak', viteConfigPath)
fs.unlinkSync(viteConfigPath + '.bak')
}
restoreEnvFile(envAlphaPath)
restoreRegistryFile()
})
it('应该正确应用自定义 importMap 配置', () => {
const localCdnDir = path.resolve(distDir, 'local-cdn-static')
const vueDirs = fs.readdirSync(localCdnDir)
.filter(dir => dir.startsWith('vue@'))
.map(dir => path.resolve(localCdnDir, dir))
expect(vueDirs.length).toBeGreaterThan(0)
const distExists = vueDirs.some(dir => {
return fs.existsSync(path.resolve(dir, 'dist'))
})
expect(distExists).toBe(true)
const vueJsExists = vueDirs.some(dir => {
return fs.existsSync(path.resolve(dir, 'dist', 'vue.runtime.esm-browser.js'))
})
expect(vueJsExists).toBe(true)
const distFileCount = vueDirs.some(dir => {
const distPath = path.resolve(dir, 'dist')
return fs.existsSync(distPath) && fs.readdirSync(distPath).length > 1
})
expect(distFileCount).toBe(true)
})
})