@hejiayue/x-markdown-test 0.0.1-beta.135 → 0.0.1-beta.136
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -3
- package/src/vite-plugin.js +116 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hejiayue/x-markdown-test",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.136",
|
|
4
4
|
"description": "A markdown component library for Vue",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/x-markdown.cjs.js",
|
|
@@ -17,10 +17,12 @@
|
|
|
17
17
|
"default": "./dist/x-markdown.cjs.js"
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
|
-
"./style": "./dist/style.css"
|
|
20
|
+
"./style": "./dist/style.css",
|
|
21
|
+
"./vite-plugin": "./src/vite-plugin.js"
|
|
21
22
|
},
|
|
22
23
|
"files": [
|
|
23
|
-
"dist"
|
|
24
|
+
"dist",
|
|
25
|
+
"src/vite-plugin.js"
|
|
24
26
|
],
|
|
25
27
|
"sideEffects": [
|
|
26
28
|
"*.css",
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { resolve } from 'node:path'
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* x-markdown Vite 插件
|
|
7
|
+
*
|
|
8
|
+
* 自动处理可选依赖(shiki、shiki-stream、mermaid)的虚拟模块配置
|
|
9
|
+
* 当这些依赖未安装时,自动映射到虚拟模块避免构建失败
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} options - 插件配置选项
|
|
12
|
+
* @param {string[]} options.optionalDeps - 可选依赖列表(默认:['mermaid', 'shiki', 'shiki-stream'])
|
|
13
|
+
* @param {string} options.virtualModulesDir - 虚拟模块目录(相对于项目根目录)
|
|
14
|
+
* @returns {import('vite').Plugin} Vite 插件
|
|
15
|
+
*/
|
|
16
|
+
export function createXMarkdownVitePlugin(options = {}) {
|
|
17
|
+
const {
|
|
18
|
+
optionalDeps = ['mermaid', 'shiki', 'shiki-stream'],
|
|
19
|
+
virtualModulesDir = './node_modules/@hejiayue/x-markdown-test/virtual-modules',
|
|
20
|
+
} = options
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
name: '@hejiayue/x-markdown-test:vite-plugin',
|
|
24
|
+
enforce: 'pre', // 在其他插件之前执行
|
|
25
|
+
config(config) {
|
|
26
|
+
// 获取项目根目录
|
|
27
|
+
const projectRoot = process.cwd()
|
|
28
|
+
|
|
29
|
+
// 动态生成可选依赖的 alias 配置
|
|
30
|
+
const optionalAliases = []
|
|
31
|
+
|
|
32
|
+
for (const dep of optionalDeps) {
|
|
33
|
+
const depPath = resolve(projectRoot, 'node_modules', dep)
|
|
34
|
+
|
|
35
|
+
if (!existsSync(depPath)) {
|
|
36
|
+
// 依赖未安装,使用虚拟模块
|
|
37
|
+
// 尝试从多个可能的位置查找虚拟模块
|
|
38
|
+
const virtualModulePaths = [
|
|
39
|
+
resolve(projectRoot, virtualModulesDir, `${dep}.js`),
|
|
40
|
+
resolve(projectRoot, 'src/virtual-modules', `${dep}.js`),
|
|
41
|
+
// 最后尝试:从 x-markdown 包内部获取
|
|
42
|
+
resolve(__dirname, 'virtual-modules', `${dep}.js`),
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
let virtualModulePath = null
|
|
46
|
+
for (const path of virtualModulePaths) {
|
|
47
|
+
if (existsSync(path)) {
|
|
48
|
+
virtualModulePath = path
|
|
49
|
+
break
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (virtualModulePath) {
|
|
54
|
+
optionalAliases.push({
|
|
55
|
+
find: dep,
|
|
56
|
+
replacement: virtualModulePath,
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
// 在开发环境显示提示信息
|
|
60
|
+
if (config.mode === 'development') {
|
|
61
|
+
console.log(
|
|
62
|
+
`\x1b[33m[@hejiayue/x-markdown-test]\x1b[0m ${dep} 未安装,使用虚拟模块`
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 如果有需要添加的 alias,更新配置
|
|
70
|
+
if (optionalAliases.length > 0) {
|
|
71
|
+
// 保留原有的 alias 配置
|
|
72
|
+
const existingAlias = config.resolve?.alias || []
|
|
73
|
+
|
|
74
|
+
// 合并 alias 配置
|
|
75
|
+
config.resolve = {
|
|
76
|
+
...config.resolve,
|
|
77
|
+
alias: [
|
|
78
|
+
...optionalAliases,
|
|
79
|
+
...(Array.isArray(existingAlias) ? existingAlias : [existingAlias]),
|
|
80
|
+
],
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 确保 optionalDeps 不被预构建
|
|
85
|
+
const optimizeDeps = config.optimizeDeps || {}
|
|
86
|
+
const excludeDeps = optimizeDeps.exclude || []
|
|
87
|
+
const includeDeps = optimizeDeps.include || []
|
|
88
|
+
|
|
89
|
+
// 移除未安装的依赖从 include
|
|
90
|
+
const filteredInclude = includeDeps.filter(dep => {
|
|
91
|
+
const depPath = resolve(projectRoot, 'node_modules', dep)
|
|
92
|
+
return existsSync(depPath)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
// 添加未安装的依赖到 exclude
|
|
96
|
+
const newExclude = [...optionalDeps.filter(dep => {
|
|
97
|
+
const depPath = resolve(projectRoot, 'node_modules', dep)
|
|
98
|
+
return !existsSync(depPath)
|
|
99
|
+
}), ...excludeDeps]
|
|
100
|
+
|
|
101
|
+
config.optimizeDeps = {
|
|
102
|
+
...optimizeDeps,
|
|
103
|
+
include: filteredInclude,
|
|
104
|
+
exclude: [...new Set(newExclude)],
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 默认导出,方便使用
|
|
111
|
+
export default createXMarkdownVitePlugin
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 为了兼容性,也提供一个具名导出
|
|
115
|
+
*/
|
|
116
|
+
export { createXMarkdownVitePlugin as xMarkdownVitePlugin }
|