web-component-gallery 2.2.26 → 2.2.27

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.
@@ -1,24 +1,22 @@
1
1
  const fs = require("fs")
2
2
  const path = require("path")
3
3
 
4
- // 导入UpdateTheme中的共享函数
5
- const {
6
- CONFIG,
7
- fetchRemoteFiles,
8
- fetchAndParseLessFile,
9
- parseLessVariables,
10
- parseCSSVariables,
11
- parseExtendVariables
12
- } = require('./UpdateTheme')
13
-
14
4
  // 构建配置
15
- const BUILD_CONFIG = {
5
+ const BUILD_CONFIG = {
16
6
  // 宿主路径
17
7
  DEPD_PATH: path.join(__dirname, '../../'),
18
8
  // 入口路径
19
9
  ENTRY_PATH: path.join(__dirname, '../../../src/')
20
10
  }
21
11
 
12
+
13
+ // 提取配置常量到独立对象
14
+ const THEME_CONFIG = {
15
+ DEFAULT_THEME: 'dark',
16
+ REMOTE_URL: (process.env.VUE_APP_STATIC_URL || 'http://qxfnkj.com') + '/base-subject/iframe/theme.js'
17
+ }
18
+
19
+
22
20
  /**
23
21
  * 提取颜色变量
24
22
  * @param {string} content - 文件内容
@@ -30,6 +28,83 @@ function extractColor(content, varName) {
30
28
  return match ? match[1].trim() : null
31
29
  }
32
30
 
31
+
32
+ /**
33
+ * 自定义LESS函数
34
+ */
35
+ const customFunctions = {
36
+ install: function(less, pluginManager, functions) {
37
+
38
+ // 检查是否为CSS变量
39
+ function isCSSVariable(color) {
40
+ return color.type === 'Call' &&
41
+ color.name === 'var' &&
42
+ color.args &&
43
+ color.args[0].value.indexOf('--') === 0
44
+ }
45
+
46
+ // 提取CSS变量信息
47
+ function extractCSSVariable(callNode) {
48
+ if (callNode.name === 'var') {
49
+ return {
50
+ varName: callNode.args[0].value,
51
+ defaultValue: callNode.args[1] ? callNode.args[1] : null
52
+ }
53
+ }
54
+ return null
55
+ }
56
+
57
+ // tint函数实现
58
+ functions.add('tint', function(color, amount) {
59
+ if (isCSSVariable(color)) {
60
+ const {varName, defaultValue} = extractCSSVariable(color)
61
+ return new less.tree.Quoted('"', `tint(${defaultValue}, ${amount.value}%)`)
62
+ }
63
+
64
+ const ratio = Math.max(0, Math.min(1, parseFloat(amount.value) / 100))
65
+ return new less.tree.Color(
66
+ [
67
+ Math.min(255, Math.round(color.rgb[0] + (255 - color.rgb[0]) * ratio)),
68
+ Math.min(255, Math.round(color.rgb[1] + (255 - color.rgb[1]) * ratio)),
69
+ Math.min(255, Math.round(color.rgb[2] + (255 - color.rgb[2]) * ratio))
70
+ ],
71
+ color.alpha
72
+ )
73
+ })
74
+
75
+ // lighten函数实现
76
+ functions.add('lighten', function(color, amount) {
77
+ if (isCSSVariable(color)) {
78
+ const {varName, defaultValue} = extractCSSVariable(color)
79
+ return new less.tree.Quoted('"', `lighten(${defaultValue}, ${amount.value}%)`)
80
+ }
81
+
82
+ const hsl = color.toHSL()
83
+ const newLightness = Math.min(100, hsl.l + parseFloat(amount.value))
84
+ return new less.tree.Color(
85
+ [
86
+ Math.round(hsl.h * 360),
87
+ Math.round(hsl.s * 100),
88
+ Math.round(newLightness)
89
+ ],
90
+ color.alpha
91
+ )
92
+ })
93
+
94
+ // fade函数实现
95
+ functions.add('fade', function(color, amount) {
96
+ if (isCSSVariable(color)) {
97
+ const {varName, defaultValue} = extractCSSVariable(color)
98
+ return new less.tree.Quoted('"', `fade(${defaultValue}, ${amount.value}%)`)
99
+ }
100
+
101
+ const alpha = Math.max(0, Math.min(1, color.alpha * (amount.value / 100)))
102
+ return new less.tree.Color(color.rgb, alpha)
103
+ })
104
+ }
105
+ }
106
+
107
+
33
108
  /**
34
109
  * 向LESS文件添加新变量并保持原有格式
35
110
  * @param {Object} newVars - 要添加的新变量对象
@@ -67,12 +142,40 @@ function addLessVariables(newVars, lessContent) {
67
142
  return lines.join('\n')
68
143
  }
69
144
 
145
+
146
+ // 添加模块缓存避免重复加载
147
+ let remoteModuleCache = null
148
+
149
+ async function loadRemoteModule() {
150
+ if (remoteModuleCache) {
151
+ return remoteModuleCache
152
+ }
153
+
154
+ // 获取JS文件内容
155
+ const response = await fetch(THEME_CONFIG.REMOTE_URL)
156
+ const jsCode = await response.text()
157
+
158
+ // 获取导出类
159
+ remoteModuleCache = eval(jsCode)
160
+ return remoteModuleCache
161
+ }
162
+
163
+
70
164
  /**
71
165
  * 初始化处理并输出主题文件
72
166
  * @param {string} resetTheme - 重置主题
73
167
  */
74
168
  async function createStyleFiles(resetTheme) {
75
- const theme = resetTheme || 'dark'
169
+
170
+ const {
171
+ CONFIG,
172
+ fetchAndParseLessFile,
173
+ parseLessVariables,
174
+ parseCSSVariables,
175
+ parseExtendVariables
176
+ } = await loadRemoteModule()
177
+
178
+ const theme = resetTheme || THEME_CONFIG.DEFAULT_THEME
76
179
 
77
180
  try {
78
181
  // 并行获取文件内容
@@ -134,86 +237,17 @@ async function createStyleFiles(resetTheme) {
134
237
  }
135
238
 
136
239
 
137
- /**
138
- * 自定义LESS函数
139
- */
140
- const customFunctions = {
141
- install: function(less, pluginManager, functions) {
142
-
143
- // 检查是否为CSS变量
144
- function isCSSVariable(color) {
145
- return color.type === 'Call' &&
146
- color.name === 'var' &&
147
- color.args &&
148
- color.args[0].value.indexOf('--') === 0
149
- }
150
-
151
- // 提取CSS变量信息
152
- function extractCSSVariable(callNode) {
153
- if (callNode.name === 'var') {
154
- return {
155
- varName: callNode.args[0].value,
156
- defaultValue: callNode.args[1] ? callNode.args[1] : null
157
- }
158
- }
159
- return null
160
- }
161
-
162
- // tint函数实现
163
- functions.add('tint', function(color, amount) {
164
- if (isCSSVariable(color)) {
165
- const {varName, defaultValue} = extractCSSVariable(color)
166
- return new less.tree.Quoted('"', `tint(${defaultValue}, ${amount.value}%)`)
167
- }
168
-
169
- const ratio = Math.max(0, Math.min(1, parseFloat(amount.value) / 100))
170
- return new less.tree.Color(
171
- [
172
- Math.min(255, Math.round(color.rgb[0] + (255 - color.rgb[0]) * ratio)),
173
- Math.min(255, Math.round(color.rgb[1] + (255 - color.rgb[1]) * ratio)),
174
- Math.min(255, Math.round(color.rgb[2] + (255 - color.rgb[2]) * ratio))
175
- ],
176
- color.alpha
177
- )
178
- })
179
-
180
- // lighten函数实现
181
- functions.add('lighten', function(color, amount) {
182
- if (isCSSVariable(color)) {
183
- const {varName, defaultValue} = extractCSSVariable(color)
184
- return new less.tree.Quoted('"', `lighten(${defaultValue}, ${amount.value}%)`)
185
- }
186
-
187
- const hsl = color.toHSL()
188
- const newLightness = Math.min(100, hsl.l + parseFloat(amount.value))
189
- return new less.tree.Color(
190
- [
191
- Math.round(hsl.h * 360),
192
- Math.round(hsl.s * 100),
193
- Math.round(newLightness)
194
- ],
195
- color.alpha
196
- )
197
- })
198
-
199
- // fade函数实现
200
- functions.add('fade', function(color, amount) {
201
- if (isCSSVariable(color)) {
202
- const {varName, defaultValue} = extractCSSVariable(color)
203
- return new less.tree.Quoted('"', `fade(${defaultValue}, ${amount.value}%)`)
204
- }
205
-
206
- const alpha = Math.max(0, Math.min(1, color.alpha * (amount.value / 100)))
207
- return new less.tree.Color(color.rgb, alpha)
208
- })
209
- }
210
- }
211
-
212
240
  /**
213
241
  * 生成传输信息文件
214
242
  * @param {string} theme - 主题名称
215
243
  */
216
244
  async function createTransferFile(theme) {
245
+
246
+ const {
247
+ CONFIG,
248
+ fetchRemoteFiles
249
+ } = await loadRemoteModule()
250
+
217
251
  // 输出自定义样式文件
218
252
  await createStyleFiles(theme)
219
253
 
@@ -1,3 +1,6 @@
1
+ /** 加载必备依赖 */
2
+ require('less')
3
+
1
4
  /** 加载antDesignVue */
2
5
  import {
3
6
  Input,
@@ -18,11 +21,6 @@ import {
18
21
  import Dialog from "./Dialog"
19
22
  /** 全局页面加载框 */
20
23
  import PageLoading from "./PageLoading"
21
- /**
22
- * 全局切换主题
23
- * @theme 主题key 使用方法: this.$setTheme(@theme)
24
- * */
25
- import { updateTheme } from './UpdateTheme'
26
24
 
27
25
  /**
28
26
  * 全局挂载组件自定义指令
@@ -57,7 +55,6 @@ export default {
57
55
  /** 加载便捷弹窗等 */
58
56
  Vue.use(Dialog)
59
57
  Vue.use(PageLoading)
60
- Vue.prototype.$setTheme = updateTheme
61
58
 
62
59
  // 指令注册
63
60
  Object.keys(Directives).forEach(key => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-component-gallery",
3
- "version": "2.2.26",
3
+ "version": "2.2.27",
4
4
  "description": "基础vue、antdvue、less实现的私有组件库",
5
5
  "main": "dist/index.umd.js",
6
6
  "files": [
@@ -1,221 +0,0 @@
1
- // 引入所需依赖
2
- const less = require('less')
3
-
4
- // CSS变量搭桥文件名
5
- const colorPalette = require('../utils/ColorPalette')
6
-
7
- // 配置常量
8
- const CONFIG = {
9
- FILE: {
10
- CSS: 'root.css',
11
- VAR: 'vars.module.less',
12
- MESSAGE: 'transferData.js'
13
- },
14
- REQUEST: {
15
- IP: process.env.VUE_APP_STATIC_URL || 'http://qxfnkj.com',
16
- PATHS: {
17
- THEMES: '/base-subject/themes/',
18
- IFRAME: '/base-subject/iframe/'
19
- }
20
- }
21
- }
22
-
23
- /**
24
- * 获取服务器文件
25
- * @param {string} dirPath - 文件夹名称
26
- * @param {string} fileName - 文件名称
27
- */
28
- async function fetchRemoteFiles(dirPath, fileName) {
29
- // 远程请求IP
30
- let requestIP = CONFIG.REQUEST.IP
31
-
32
- // 浏览器环境:使用当前页面的origin
33
- if (typeof window !== 'undefined' && window.location && window.location.origin && process.env.NODE_ENV !== 'development') {
34
- requestIP = window.location.origin
35
- }
36
-
37
- const response = await fetch(`${requestIP}${CONFIG.REQUEST.PATHS[dirPath]}${fileName}`, {
38
- method: 'GET',
39
- mode: 'cors'
40
- })
41
-
42
- if (!response.ok) throw new Error(`${fileName}文件请求失败!`)
43
-
44
- const responseText = await response.text()
45
-
46
- return responseText
47
- }
48
-
49
- /**
50
- * 获取并解析LESS文件
51
- * @param {string} fileName - 文件名
52
- * @param {boolean} isParseVariables - 是否解析变量
53
- */
54
- async function fetchAndParseLessFile(fileName, isParseVariables) {
55
- // 检测并补全文件后缀
56
- let processedFileName = fileName
57
- if (!fileName.endsWith('.less') && !fileName.endsWith('.css')) {
58
- processedFileName = fileName + '.less'
59
- }
60
-
61
- const lessContent = await fetchRemoteFiles('THEMES', processedFileName)
62
-
63
- return isParseVariables
64
- ? await parseLessVariables(lessContent)
65
- : lessContent
66
- }
67
-
68
- /**
69
- * 解析LESS变量
70
- * @param {string} content - LESS文件内容
71
- */
72
- async function parseLessVariables(content) {
73
- try {
74
- const lessRoot = await less.parse(content, {
75
- javascriptEnabled: true,
76
- math: 'always'
77
- })
78
-
79
- const env = new less.contexts.Eval()
80
- const evaluatedRoot = lessRoot.eval(env)
81
- const variables = {}
82
-
83
- evaluatedRoot.rules.forEach(rule => {
84
- if (rule.variable && rule.name) {
85
- variables[rule.name] = rule.value.toCSS()
86
- }
87
- })
88
-
89
- return variables
90
- } catch (err) {
91
- console.error('LESS文件解析失败', err)
92
- return {}
93
- }
94
- }
95
-
96
-
97
- /**
98
- * 解析CSS变量
99
- * @param {Object} cssVariables - CSS变量对象
100
- * @param {Object} resetVar - 重置变量对象
101
- */
102
- function parseCSSVariables(cssVariables, resetVar) {
103
- const result = {}
104
- for (const [key, value] of Object.entries(cssVariables)) {
105
- const match = value.match(/var\(\s*([^,)]+)\s*(?:,\s*((?:[^()]|\([^)]*\))+))?\s*\)/)
106
- if (match) result[match[1]] = (match[2] || resetVar[key])
107
- }
108
- return result
109
- }
110
-
111
- /**
112
- * 解析扩展变量
113
- * @param {Object} extendVar - 扩展变量对象
114
- * @param {Object} lessVariables - LESS变量对象
115
- */
116
- function parseExtendVariables(extendVar, lessVariables) {
117
- const { '@extend-colors': extendColors, '@extend-light': extendLight } = extendVar
118
-
119
- if (!extendColors || !extendLight) {
120
- return { nonExport: {}, export: {} }
121
- }
122
-
123
- return extendColors.split(',').reduce((acc, key) => {
124
- const varName = key.trim().replace(/\"/g, '')
125
- const [lessVar] = varName.split('-')
126
- const cssVar = `--${varName.slice(1)}`
127
- const hex = lessVariables[varName]
128
-
129
- if (!hex) return acc
130
-
131
- // 转换RGB值
132
- const [r, g, b] = [1, 3, 5].map(offset =>
133
- parseInt(hex.slice(offset, offset + 2), 16)
134
- )
135
-
136
- // 添加基础变量
137
- acc.nonExport[`${lessVar}-rgb`] = `var(${cssVar}-rgb)`
138
- acc.export[`${cssVar}-rgb`] = `${r},${g},${b}`
139
-
140
- // 处理亮度变量
141
- extendLight.split(',').forEach(light => {
142
- const lightName = light.trim().replace(/\"/g, '')
143
- acc.nonExport[`${lessVar}-${lightName}`] = `var(${cssVar}-${lightName})`
144
- acc.export[`${cssVar}-${lightName}`] = colorPalette(hex, lightName)
145
- })
146
-
147
- return acc
148
- }, { nonExport: {}, export: {} })
149
- }
150
-
151
-
152
-
153
- /**
154
- * 更新主题
155
- * @param {string} theme - 主题名称
156
- */
157
- async function updateTheme(theme) {
158
- if (!less || !less.modifyVars) return
159
-
160
- const root = document.documentElement
161
-
162
- try {
163
- // 并行获取主题变量和CSS变量
164
- const [themeVariables, cssVariables] = await Promise.all([
165
- fetchAndParseLessFile(theme, true),
166
- fetchAndParseLessFile(CONFIG.FILE.VAR, true)
167
- ])
168
-
169
- // 解析CSS变量
170
- const resetValue = parseCSSVariables(cssVariables, themeVariables)
171
-
172
- const updateVariables = {
173
- css: {},
174
- less: {}
175
- }
176
-
177
- // 对比新旧变量值,收集需要更新的变量
178
- for (const [key, colorA] of Object.entries(resetValue)) {
179
- const colorB = getComputedStyle(root).getPropertyValue(key).trim()
180
-
181
- // 只有当颜色值确实发生变化时才更新
182
- if (colorA !== colorB) {
183
- updateVariables.css[key] = colorA
184
- // 将CSS变量名转换为LESS变量名
185
- updateVariables.less[`@${key.slice(2)}`] = colorA
186
- }
187
- }
188
-
189
- // 应用LESS变量更新
190
- // await less.modifyVars({
191
- // ...themeVariables,
192
- // ...updateVariables.less
193
- // })
194
- // console.log("换肤成功")
195
-
196
- if (!Object.keys(updateVariables.css).length) return
197
-
198
- // 解析并更新CSS变量
199
- const updateCSSVariables = {
200
- ...updateVariables.css,
201
- ...parseExtendVariables(cssVariables, updateVariables.less).export
202
- }
203
-
204
- for (const [key, color] of Object.entries(updateCSSVariables)) {
205
- root.style.setProperty(key, color)
206
- }
207
- } catch (error) {
208
- console.error('主题更新失败:', error)
209
- throw error
210
- }
211
- }
212
-
213
- module.exports = {
214
- CONFIG,
215
- updateTheme,
216
- parseCSSVariables,
217
- parseLessVariables,
218
- parseExtendVariables,
219
- fetchAndParseLessFile,
220
- fetchRemoteFiles
221
- }