Skip to content

Commit 4424058

Browse files
authored
fix: localImportMap not works (#1242)
1 parent 20ad6c6 commit 4424058

40 files changed

+1391
-120
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ yarn.lock
66
pnpm-lock.yaml
77
lerna-debug.log
88
packages/design-core/bundle-deps
9+
designer-demo/bundle-deps
910

1011
# local env files
1112
.env.local

designer-demo/env/.env.alpha

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
# alpha mode, used by the "build:alpha" script
22

33
NODE_ENV=production
4-
# VITE_CDN_DOMAIN=https://unpkg.com
54
VITE_CDN_DOMAIN=https://registry.npmmirror.com
65
# 使用npmmirror的cdn 时,需要声明 VITE_CDN_TYPE=npmmirror
76
VITE_CDN_TYPE=npmmirror
8-
VITE_LOCAL_IMPORT_MAPS=false
9-
VITE_LOCAL_BUNDLE_DEPS=false
107
# VITE_ORIGIN=
118

129
# 错误监控上报 url

designer-demo/env/.env.development

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# development mode, used by the "vite" command
22

33
NODE_ENV=development
4-
# VITE_CDN_DOMAIN=https://unpkg.com
54
VITE_CDN_DOMAIN=https://registry.npmmirror.com
65
# 使用npmmirror的cdn 时,需要声明 VITE_CDN_TYPE=npmmirror
76
VITE_CDN_TYPE=npmmirror
8-
VITE_LOCAL_IMPORT_MAPS=false
9-
VITE_LOCAL_BUNDLE_DEPS=false
107
# request data via alpha service
118
# VITE_ORIGIN=
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# CDN 本地化配置示例
2+
3+
# 将画布、页面预览需要的 vue、vue-i18n 等等依赖复制到构建产物中
4+
VITE_LOCAL_IMPORT_MAPS=true
5+
6+
# 将本地物料 bundle.json 的 script 和 css 复制到构建产物中
7+
VITE_LOCAL_BUNDLE_DEPS=true
8+
9+
# 将 VITE_LOCAL_BUNDLE_DEPS 复制到构建产物中的目录名称,默认为 local-cdn-static
10+
VITE_LOCAL_IMPORT_PATH=local-cdn-static

designer-demo/env/.env.production

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ NODE_ENV=production
55
VITE_CDN_DOMAIN=https://registry.npmmirror.com
66
# 使用npmmirror的cdn 时,需要声明 VITE_CDN_TYPE=npmmirror
77
VITE_CDN_TYPE=npmmirror
8-
VITE_LOCAL_IMPORT_MAPS=false
9-
VITE_LOCAL_BUNDLE_DEPS=false
108
#VITE_ORIGIN=

designer-demo/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"type": "module",
66
"scripts": {
77
"dev": "cross-env vite",
8-
"build:alpha": "cross-env NODE_OPTIONS=--max-old-space-size=8192 vite build --mode alpha",
9-
"build": "cross-env NODE_OPTIONS=--max-old-space-size=8192 vite build"
8+
"build:alpha": "cross-env NODE_OPTIONS=--max-old-space-size=10240 vite build --mode alpha",
9+
"build": "cross-env NODE_OPTIONS=--max-old-space-size=10240 vite build",
10+
"test": "vitest run",
11+
"test:watch": "vitest"
1012
},
1113
"dependencies": {
1214
"@opentiny/tiny-engine": "workspace:^",
@@ -25,6 +27,7 @@
2527
"@opentiny/tiny-engine-vite-config": "workspace:^",
2628
"@vitejs/plugin-vue": "^5.1.2",
2729
"cross-env": "^7.0.3",
28-
"vite": "^5.4.2"
30+
"vite": "^5.4.2",
31+
"vitest": "3.0.9"
2932
}
3033
}

designer-demo/public/mock/bundle.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10166,7 +10166,8 @@
1016610166
"devMode": "proCode",
1016710167
"npm": {
1016810168
"package": "@opentiny/vue",
10169-
"exportName": "TinyGridColumn"
10169+
"exportName": "TinyGridColumn",
10170+
"destructuring": true
1017010171
},
1017110172
"group": "component",
1017210173
"priority": 2,
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* localCDN 功能测试
3+
* 这个测试文件用于验证 localCDN 本地化功能是否正常工作
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, 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 distDir = path.resolve(projectRoot, 'dist')
16+
const localCdnDir = path.resolve(distDir, 'local-cdn-static')
17+
const envAlphaPath = path.resolve(projectRoot, 'env', '.env.alpha')
18+
19+
describe('localCDN 功能测试', () => {
20+
beforeAll(() => {
21+
// 备份环境变量文件
22+
backupEnvFile(envAlphaPath)
23+
24+
// 确保环境变量正确设置
25+
let envContent = fs.readFileSync(envAlphaPath, 'utf-8')
26+
27+
// 确保关键环境变量已启用
28+
envContent = ensureEnvVarEnabled(envContent, 'VITE_LOCAL_IMPORT_MAPS')
29+
envContent = ensureEnvVarEnabled(envContent, 'VITE_LOCAL_BUNDLE_DEPS')
30+
envContent = ensureEnvVarEnabled(envContent, 'VITE_LOCAL_IMPORT_PATH', 'local-cdn-static')
31+
32+
// 写回更新后的环境变量
33+
fs.writeFileSync(envAlphaPath, envContent)
34+
35+
// 执行构建
36+
execSync('pnpm run build:alpha', {
37+
cwd: projectRoot,
38+
stdio: 'inherit'
39+
})
40+
})
41+
42+
// 测试结束后恢复原始环境变量
43+
afterAll(() => {
44+
restoreEnvFile(envAlphaPath)
45+
})
46+
47+
it('应该在构建后生成 local-cdn-static 目录', () => {
48+
expect(fs.existsSync(localCdnDir)).toBe(true)
49+
})
50+
51+
it('应该正确复制 @vue/devtools-api 依赖', () => {
52+
// 寻找 @vue/devtools-api 文件夹
53+
const devToolsDirs = fs.readdirSync(path.resolve(localCdnDir, '@vue'))
54+
.find(dir => dir.startsWith('devtools-api@'))
55+
56+
expect(devToolsDirs).toBeDefined()
57+
58+
// 检查 index.js 是否存在
59+
const indexJsExists = fs.existsSync(path.resolve(localCdnDir, '@vue', devToolsDirs, 'lib/esm/index.js'))
60+
61+
expect(indexJsExists).toBe(true)
62+
})
63+
64+
it('应该正确复制 vue 依赖', () => {
65+
// 寻找 vue 文件夹
66+
const runtimeDirs = fs.readdirSync(localCdnDir).find(dir => dir.startsWith('vue@'))
67+
68+
expect(runtimeDirs).toBeDefined()
69+
70+
const vueProdDist = path.resolve(localCdnDir, runtimeDirs, 'dist/vue.runtime.esm-browser.js')
71+
72+
expect(fs.existsSync(vueProdDist)).toBe(true)
73+
})
74+
})
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)