1+ /**
2+ * localCDN bundle依赖本地化测试
3+ * 测试物料需要的CDN资源本地化功能
4+ */
5+ import { describe , it , expect , beforeAll , afterAll } from 'vitest'
6+ import fs from 'node:fs'
7+ import path from 'node:path'
8+ import { fileURLToPath } from 'node:url'
9+ import { execSync } from 'node:child_process'
10+ import { ensureEnvVarEnabled , updateCdnDomain , backupEnvFile , restoreEnvFile } from './utils/envHelpers.js'
11+
12+ // 获取当前文件目录
13+ const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
14+ const projectRoot = path . resolve ( __dirname , '..' )
15+ const publicDir = path . resolve ( projectRoot , 'public' )
16+ const bundleJsonDir = path . resolve ( publicDir , 'mock' )
17+ const envAlphaPath = path . resolve ( projectRoot , 'env' , '.env.alpha' )
18+ const distDir = path . resolve ( projectRoot , 'dist' )
19+ const bundleJsonPath = path . resolve ( bundleJsonDir , 'bundle.json' )
20+
21+ // 准备测试用的 bundle.json 文件
22+ const testBundleJson = {
23+ data : {
24+ materials : {
25+ packages : [
26+ {
27+ "name" : "TinyVue组件库" ,
28+ "package" : "@opentiny/vue" ,
29+ "version" : "3.20.0" ,
30+ "script" :
"https://unpkg.com/@opentiny/[email protected] /dist3/tiny-vue-pc.mjs" , 31+ "css" :
"https://unpkg.com/@opentiny/[email protected] /index.css" 32+ } ,
33+ {
34+ "name" : "element-plus组件库" ,
35+ "package" : "element-plus" ,
36+ "version" : "2.4.2" ,
37+ "script" : "https://registry.npmmirror.com/element-plus/2.4.2/files/dist/index.full.mjs" ,
38+ "css" : "https://registry.npmmirror.com/element-plus/2.4.2/files/dist/index.css"
39+ }
40+ ]
41+ }
42+ }
43+ }
44+
45+ describe ( 'localCDN bundle依赖本地化测试' , ( ) => {
46+ let originalBundleJson = null
47+
48+ beforeAll ( ( ) => {
49+ // 备份环境变量
50+ backupEnvFile ( envAlphaPath )
51+
52+ // 确保目录存在
53+ if ( ! fs . existsSync ( bundleJsonDir ) ) {
54+ fs . mkdirSync ( bundleJsonDir , { recursive : true } )
55+ }
56+
57+ // 备份原始的 bundle.json 文件(如果存在)
58+ if ( fs . existsSync ( bundleJsonPath ) ) {
59+ originalBundleJson = fs . readFileSync ( bundleJsonPath , 'utf-8' )
60+ }
61+
62+ // 创建测试用的 bundle.json
63+ fs . writeFileSync ( bundleJsonPath , JSON . stringify ( testBundleJson , null , 2 ) )
64+
65+ // 设置环境变量
66+ let envContent = fs . readFileSync ( envAlphaPath , 'utf-8' )
67+
68+ // 更新CDN域名
69+ envContent = updateCdnDomain ( envContent , 'https://unpkg.com' )
70+
71+ // 确保启用了 bundle 依赖本地化
72+ envContent = ensureEnvVarEnabled ( envContent , 'VITE_LOCAL_BUNDLE_DEPS' )
73+
74+ fs . writeFileSync ( envAlphaPath , envContent )
75+
76+ // 执行构建
77+ execSync ( 'pnpm run build:alpha' , {
78+ cwd : projectRoot ,
79+ stdio : 'inherit'
80+ } )
81+ } )
82+
83+ // 测试完成后清理测试文件并恢复环境变量
84+ afterAll ( ( ) => {
85+ // 恢复原始的 bundle.json 文件
86+ if ( originalBundleJson ) {
87+ fs . writeFileSync ( bundleJsonPath , originalBundleJson )
88+ } else if ( fs . existsSync ( bundleJsonPath ) ) {
89+ // 如果原始文件不存在,则删除测试创建的文件
90+ fs . unlinkSync ( bundleJsonPath )
91+ }
92+
93+ // 恢复环境变量
94+ restoreEnvFile ( envAlphaPath )
95+ } )
96+
97+ it ( '应该将物料CDN依赖本地化' , ( ) => {
98+ const distBundleJsonPath = path . resolve ( distDir , 'mock' , 'bundle.json' )
99+
100+ // 检查构建后的 bundle.json 是否存在
101+ expect ( fs . existsSync ( distBundleJsonPath ) ) . toBe ( true )
102+
103+ // 检查构建后的 bundle.json 中是否已将远程URL替换为本地路径
104+ const packages = JSON . parse ( fs . readFileSync ( distBundleJsonPath , 'utf-8' ) ) . data . materials . packages
105+
106+ // 检查 vue 的路径是否已本地化
107+ expect ( packages [ 0 ] . script ) . not . toContain ( 'https://unpkg.com' )
108+ expect ( packages [ 0 ] . script ) . toContain ( './material-static/' )
109+
110+ // 检查 element-plus 的路径是否已本地化
111+ expect ( packages [ 1 ] . script ) . toContain ( 'https://registry.npmmirror.com' )
112+ expect ( packages [ 1 ] . script ) . not . toContain ( './material-static/' )
113+ expect ( packages [ 1 ] . css ) . toContain ( 'https://registry.npmmirror.com' )
114+ expect ( packages [ 1 ] . css ) . not . toContain ( './material-static/' )
115+ } )
116+
117+ it ( '应该将物料依赖包复制到产物CDN目录' , ( ) => {
118+ const localCdnDir = path . resolve ( distDir , 'material-static/@opentiny' )
119+
120+ // 检查 vue 是否已复制
121+ const tinyVueDir = fs . readdirSync ( localCdnDir )
122+ . find ( dir => dir . startsWith ( 'vue-runtime@' ) )
123+ const tinyVueThemeDir = fs . readdirSync ( localCdnDir )
124+ . find ( dir => dir . startsWith ( 'vue-theme@' ) )
125+ expect ( tinyVueDir ) . toBeDefined ( )
126+
127+ // 检查 tiny-vue-pc.mjs 是否存在
128+ const tinyVueJsPath = path . resolve ( localCdnDir , tinyVueDir , 'dist3' , 'tiny-vue-pc.mjs' )
129+ const tinyVueCssPath = path . resolve ( localCdnDir , tinyVueThemeDir , 'index.css' )
130+
131+ expect ( fs . existsSync ( tinyVueJsPath ) ) . toBe ( true )
132+ expect ( fs . existsSync ( tinyVueCssPath ) ) . toBe ( true )
133+
134+ // 检查 element-plus 是否已复制
135+ const elementDir = fs . readdirSync ( localCdnDir )
136+ . find ( dir => dir . startsWith ( 'element-plus@' ) )
137+
138+ expect ( elementDir ) . not . toBeDefined ( )
139+ } )
140+ } )
0 commit comments