@easyv/react-component-builder 0.19.2 → 0.19.3
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/config/paths.js +24 -24
- package/config/webpack.config.build.js +70 -70
- package/config/webpack.config.common.js +126 -126
- package/config/webpack.config.js +126 -126
- package/launcher/build.js +120 -118
- package/launcher/generate.js +99 -99
- package/launcher/start.js +238 -238
- package/loaders/react-entry-loader.js +430 -430
- package/package.json +76 -76
- package/public/component.ejs +51 -51
- package/public/home.ejs +21 -21
- package/templates/basic/assets/logo.png +0 -0
- package/templates/basic/config/main.data.json +14 -14
- package/templates/basic/config/main.json +6 -6
- package/templates/basic/index.jsx +26 -26
- package/templates/basic/styles/index.module.css +3 -3
- package/templates/basic/utils/bind-children.js +10 -10
- package/templates/basic/utils/reduce-config.js +20 -20
- package/templates/three-component/config/main.data.json +14 -14
- package/templates/three-component/config/main.json +76 -76
- package/templates/three-component/index.jsx +122 -122
- package/templates/three-component/utils/Component.js +274 -274
- package/templates/three-component/utils/OrbitControls.js +1051 -1051
- package/templates/three-component/utils/reduce-config.js +20 -20
- package/utils/auto-assign-port.js +11 -11
- package/utils/index.js +91 -91
- package/utils/openBrowser.js +153 -153
- package/utils/openChrome.applescript +94 -94
|
@@ -1,430 +1,430 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 给导出的组件添加static方法获取配置文件
|
|
3
|
-
*/
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const glob = require('glob');
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
// const vm = require('vm');
|
|
8
|
-
|
|
9
|
-
const parser = require('@babel/parser').parse;
|
|
10
|
-
const traverse = require('@babel/traverse').default;
|
|
11
|
-
const generate = require('@babel/generator').default;
|
|
12
|
-
const t = require('@babel/types');
|
|
13
|
-
|
|
14
|
-
function getOldDimension(width, height, hasUndeline) {
|
|
15
|
-
if (hasUndeline) {
|
|
16
|
-
return {
|
|
17
|
-
_name: 'dimension', // 定义位置宽高
|
|
18
|
-
_displayName: '位置尺寸',
|
|
19
|
-
_value: [
|
|
20
|
-
{
|
|
21
|
-
_name: 'chartPosition',
|
|
22
|
-
_displayName: '图表位置',
|
|
23
|
-
_value: [
|
|
24
|
-
{
|
|
25
|
-
_name: 'left',
|
|
26
|
-
_displayName: 'X轴坐标',
|
|
27
|
-
_value: 100, // 开发环境默认显示位置
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
_name: 'top',
|
|
31
|
-
_displayName: 'Y轴坐标',
|
|
32
|
-
_value: 100, // 开发环境默认显示位置
|
|
33
|
-
},
|
|
34
|
-
],
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
_name: 'chartDimension',
|
|
38
|
-
_displayName: '图表尺寸',
|
|
39
|
-
_value: [
|
|
40
|
-
{
|
|
41
|
-
_name: 'width',
|
|
42
|
-
_displayName: '宽度',
|
|
43
|
-
_value: width, // 组件默认宽
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
_name: 'height',
|
|
47
|
-
_displayName: '高度',
|
|
48
|
-
_value: height, // 组件默认高
|
|
49
|
-
},
|
|
50
|
-
],
|
|
51
|
-
},
|
|
52
|
-
],
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
return {
|
|
56
|
-
name: 'dimension', // 定义位置宽高
|
|
57
|
-
displayName: '位置尺寸',
|
|
58
|
-
value: [
|
|
59
|
-
{
|
|
60
|
-
name: 'chartPosition',
|
|
61
|
-
displayName: '图表位置',
|
|
62
|
-
value: [
|
|
63
|
-
{
|
|
64
|
-
name: 'left',
|
|
65
|
-
displayName: 'X轴坐标',
|
|
66
|
-
value: 100, // 开发环境默认显示位置
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
name: 'top',
|
|
70
|
-
displayName: 'Y轴坐标',
|
|
71
|
-
value: 100, // 开发环境默认显示位置
|
|
72
|
-
},
|
|
73
|
-
],
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
name: 'chartDimension',
|
|
77
|
-
displayName: '图表尺寸',
|
|
78
|
-
value: [
|
|
79
|
-
{
|
|
80
|
-
name: 'width',
|
|
81
|
-
displayName: '宽度',
|
|
82
|
-
value: width, // 组件默认宽
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
name: 'height',
|
|
86
|
-
displayName: '高度',
|
|
87
|
-
value: height, // 组件默认高
|
|
88
|
-
},
|
|
89
|
-
],
|
|
90
|
-
},
|
|
91
|
-
],
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
module.exports = function (content, map, meta) {
|
|
96
|
-
var callback = this.async();
|
|
97
|
-
const query = this.query || {};
|
|
98
|
-
|
|
99
|
-
const modulePath = this.context;
|
|
100
|
-
let file = content;
|
|
101
|
-
|
|
102
|
-
// 兼容旧的写法
|
|
103
|
-
if (~file.indexOf('getDefaultConfig')) {
|
|
104
|
-
return callback(null, file);
|
|
105
|
-
}
|
|
106
|
-
const configPath = `${modulePath}/config`;
|
|
107
|
-
// 添加watcher依赖
|
|
108
|
-
this.addContextDependency(path.resolve(configPath));
|
|
109
|
-
|
|
110
|
-
glob(
|
|
111
|
-
`{*.data.{json,js},subConfigs/*.data.{json,js}}`,
|
|
112
|
-
{
|
|
113
|
-
cwd: configPath,
|
|
114
|
-
},
|
|
115
|
-
function (err, matches) {
|
|
116
|
-
if (err || matches.length === 0) {
|
|
117
|
-
callback(
|
|
118
|
-
`main.json or main.data.json cannot be found in ${configPath}`,
|
|
119
|
-
);
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
try {
|
|
124
|
-
// 读取配置文件
|
|
125
|
-
const main = {};
|
|
126
|
-
let subConfigs = [];
|
|
127
|
-
const subDatas = [];
|
|
128
|
-
const names = new Set();
|
|
129
|
-
if (
|
|
130
|
-
matches
|
|
131
|
-
.map((filename) => filename.split('.')[0])
|
|
132
|
-
.some((name, i) => {
|
|
133
|
-
let config;
|
|
134
|
-
try {
|
|
135
|
-
let jsonFile = fs.existsSync(
|
|
136
|
-
path.join(configPath, name + '.json'),
|
|
137
|
-
);
|
|
138
|
-
|
|
139
|
-
if (jsonFile) {
|
|
140
|
-
config = JSON.parse(
|
|
141
|
-
fs.readFileSync(path.resolve(configPath, name + '.json')),
|
|
142
|
-
{ encoding: 'utf8' },
|
|
143
|
-
);
|
|
144
|
-
} else {
|
|
145
|
-
Object.keys(require.cache).forEach(function (key) {
|
|
146
|
-
if (key.includes('node_modules')) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
delete require.cache[key];
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
config = require(path.resolve(
|
|
153
|
-
configPath,
|
|
154
|
-
name + '.js',
|
|
155
|
-
)).default;
|
|
156
|
-
}
|
|
157
|
-
} catch (error) {
|
|
158
|
-
error.message += ` in ${configPath}${name}.json or ${configPath}${name}.js`;
|
|
159
|
-
callback(error);
|
|
160
|
-
return true;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
let data;
|
|
164
|
-
try {
|
|
165
|
-
let dataJsonFile = fs.existsSync(
|
|
166
|
-
path.join(configPath, name + '.data.json'),
|
|
167
|
-
);
|
|
168
|
-
|
|
169
|
-
if (dataJsonFile) {
|
|
170
|
-
data = JSON.parse(
|
|
171
|
-
fs.readFileSync(
|
|
172
|
-
path.resolve(configPath, name + '.data.json'),
|
|
173
|
-
{ encoding: 'utf8' },
|
|
174
|
-
),
|
|
175
|
-
);
|
|
176
|
-
} else {
|
|
177
|
-
delete require.cache[
|
|
178
|
-
require.resolve(path.resolve(configPath, name + '.data.js'))
|
|
179
|
-
];
|
|
180
|
-
data = require(path.resolve(
|
|
181
|
-
configPath,
|
|
182
|
-
name + '.data.js',
|
|
183
|
-
)).default;
|
|
184
|
-
}
|
|
185
|
-
} catch (error) {
|
|
186
|
-
error.message += ` in ${configPath}${name}.data.json or ${configPath}${name}.data.js`;
|
|
187
|
-
callback(error);
|
|
188
|
-
return true;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (names.has(config.base.module_name)) {
|
|
192
|
-
callback(
|
|
193
|
-
new Error(
|
|
194
|
-
`base.module_name ${config.base.module_name} was duplicated in ${configPath}`,
|
|
195
|
-
),
|
|
196
|
-
);
|
|
197
|
-
return true;
|
|
198
|
-
} else {
|
|
199
|
-
names.add(config.base.module_name);
|
|
200
|
-
}
|
|
201
|
-
if (name === 'main') {
|
|
202
|
-
main.config = config;
|
|
203
|
-
main.data = data;
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
const id = Math.random().toString(16).slice(2);
|
|
207
|
-
config.id = id;
|
|
208
|
-
data.id = id;
|
|
209
|
-
subConfigs.push(config);
|
|
210
|
-
subDatas.push(data);
|
|
211
|
-
})
|
|
212
|
-
)
|
|
213
|
-
return;
|
|
214
|
-
|
|
215
|
-
// 解析导出变量名
|
|
216
|
-
let vname = '',
|
|
217
|
-
exportName = `__EASYV_COMPONENT__`;
|
|
218
|
-
const ast = parser(content, {
|
|
219
|
-
sourceType: 'module',
|
|
220
|
-
plugins: ['jsx', 'typescript', 'classProperties'],
|
|
221
|
-
});
|
|
222
|
-
traverse(ast, {
|
|
223
|
-
ExportDefaultDeclaration(path) {
|
|
224
|
-
const child = path.node.declaration;
|
|
225
|
-
|
|
226
|
-
if (child.type === 'ClassDeclaration') {
|
|
227
|
-
// e.g: export default class a {}
|
|
228
|
-
if (child.id) {
|
|
229
|
-
vname = child.id.name;
|
|
230
|
-
// e.g: export default class {}
|
|
231
|
-
} else {
|
|
232
|
-
vname = exportName;
|
|
233
|
-
child.id = t.identifier(exportName);
|
|
234
|
-
}
|
|
235
|
-
// e.g: export default memo()
|
|
236
|
-
// e.g: export default () => {}
|
|
237
|
-
} else if (
|
|
238
|
-
!!~[
|
|
239
|
-
'CallExpression',
|
|
240
|
-
'ArrowFunctionExpression',
|
|
241
|
-
'FunctionExpression',
|
|
242
|
-
].indexOf(child.type)
|
|
243
|
-
) {
|
|
244
|
-
const exportIdentifier = t.identifier(exportName);
|
|
245
|
-
path.insertBefore(
|
|
246
|
-
t.variableDeclaration('const', [
|
|
247
|
-
t.variableDeclarator(exportIdentifier, child),
|
|
248
|
-
]),
|
|
249
|
-
);
|
|
250
|
-
path.node.declaration = exportIdentifier;
|
|
251
|
-
vname = exportName;
|
|
252
|
-
// e.g: export default function(){}
|
|
253
|
-
} else if (!!~['FunctionDeclaration'].indexOf(child.type)) {
|
|
254
|
-
const exportIdentifier = t.identifier(exportName);
|
|
255
|
-
child.id = exportIdentifier;
|
|
256
|
-
vname = exportName;
|
|
257
|
-
// e.g: export default a;
|
|
258
|
-
} else if (child.type === 'Identifier') {
|
|
259
|
-
vname = child.name;
|
|
260
|
-
}
|
|
261
|
-
path.stop();
|
|
262
|
-
},
|
|
263
|
-
ExportNamedDeclaration(path) {
|
|
264
|
-
const child = path.node.declaration;
|
|
265
|
-
// e.g: export const a = {};
|
|
266
|
-
if (child.type === 'VariableDeclaration') {
|
|
267
|
-
const exportIdentifier = child.declarations[0].id;
|
|
268
|
-
vname = exportIdentifier.name;
|
|
269
|
-
path.replaceWith(child);
|
|
270
|
-
path.insertAfter(t.exportDefaultDeclaration(exportIdentifier));
|
|
271
|
-
// e.g: export function a(){};
|
|
272
|
-
} else if (child.type === 'FunctionDeclaration') {
|
|
273
|
-
const exportIdentifier = child.id;
|
|
274
|
-
path.replaceWith(child);
|
|
275
|
-
path.insertAfter(t.exportDefaultDeclaration(exportIdentifier));
|
|
276
|
-
}
|
|
277
|
-
path.stop();
|
|
278
|
-
},
|
|
279
|
-
});
|
|
280
|
-
file = generate(ast, { quotes: 'single', retainLines: false }).code;
|
|
281
|
-
if (!vname) {
|
|
282
|
-
return callback(
|
|
283
|
-
`cannot found default export object in ${path.join(
|
|
284
|
-
modulePath,
|
|
285
|
-
'index',
|
|
286
|
-
)}`,
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
// 检查width/height
|
|
290
|
-
const mainConfig = main.config;
|
|
291
|
-
// if (!('width' in mainConfig) || !('height' in mainConfig)) {
|
|
292
|
-
// return callback(`main.json lack field 'width' or 'height'.`)
|
|
293
|
-
// }
|
|
294
|
-
|
|
295
|
-
// 添加width/height属性兼容老版本
|
|
296
|
-
let updateChart = false;
|
|
297
|
-
const hasUnderline = mainConfig.configuration[0]
|
|
298
|
-
? '_value' in mainConfig.configuration[0]
|
|
299
|
-
: false;
|
|
300
|
-
const keys = {
|
|
301
|
-
name: hasUnderline ? '_name' : 'name',
|
|
302
|
-
displayName: hasUnderline ? '_displayName' : 'displayName',
|
|
303
|
-
value: hasUnderline ? '_value' : 'value',
|
|
304
|
-
type: hasUnderline ? '_type' : 'type',
|
|
305
|
-
children: hasUnderline ? '_children' : 'children',
|
|
306
|
-
};
|
|
307
|
-
mainConfig.configuration = mainConfig.configuration.map((item) => {
|
|
308
|
-
if (item[keys.name] === 'chart') {
|
|
309
|
-
let values = item._value || item.value;
|
|
310
|
-
values = values.map((item) => {
|
|
311
|
-
if (item[keys.name] === 'dimension') {
|
|
312
|
-
updateChart = true;
|
|
313
|
-
return item;
|
|
314
|
-
// return getOldDimension(mainConfig.width, mainConfig.height, hasUnderline)
|
|
315
|
-
}
|
|
316
|
-
return item;
|
|
317
|
-
});
|
|
318
|
-
if (!updateChart) {
|
|
319
|
-
values.unshift(
|
|
320
|
-
getOldDimension(
|
|
321
|
-
mainConfig.width,
|
|
322
|
-
mainConfig.height,
|
|
323
|
-
hasUnderline,
|
|
324
|
-
),
|
|
325
|
-
);
|
|
326
|
-
updateChart = true;
|
|
327
|
-
}
|
|
328
|
-
item[keys.value] = values;
|
|
329
|
-
return item;
|
|
330
|
-
}
|
|
331
|
-
return item;
|
|
332
|
-
});
|
|
333
|
-
if (!updateChart) {
|
|
334
|
-
const chart = {
|
|
335
|
-
[keys.name]: 'chart',
|
|
336
|
-
[keys.displayName]: '组件',
|
|
337
|
-
[keys.value]: [
|
|
338
|
-
getOldDimension(
|
|
339
|
-
mainConfig.width,
|
|
340
|
-
mainConfig.height,
|
|
341
|
-
hasUnderline,
|
|
342
|
-
),
|
|
343
|
-
],
|
|
344
|
-
};
|
|
345
|
-
mainConfig.configuration.unshift(chart);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// 子组件排序,有配置项则以chilren排序,否则以文件顺序排序
|
|
349
|
-
if (mainConfig.children) {
|
|
350
|
-
subConfigs = mainConfig.children.reduce((all, item) => {
|
|
351
|
-
let childConfig = subConfigs.find(
|
|
352
|
-
(d) => d.base.module_name === item,
|
|
353
|
-
);
|
|
354
|
-
return childConfig ? all.concat(childConfig) : all;
|
|
355
|
-
}, []);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
if (query.isBuild) {
|
|
359
|
-
// 兼容平台子组件配置
|
|
360
|
-
if (subConfigs.length) {
|
|
361
|
-
const defineMap = subConfigs.reduce((a, b) => {
|
|
362
|
-
a[b.base.module_name] = b;
|
|
363
|
-
return a;
|
|
364
|
-
}, {});
|
|
365
|
-
const defineChildren =
|
|
366
|
-
mainConfig.children ||
|
|
367
|
-
subConfigs.map((item) => item.base.module_name);
|
|
368
|
-
const presetChidlren = mainConfig.presetChildren || [];
|
|
369
|
-
|
|
370
|
-
const children = {
|
|
371
|
-
[keys.name]: 'children',
|
|
372
|
-
[keys.displayName]: '子组件',
|
|
373
|
-
[keys.value]: [
|
|
374
|
-
{
|
|
375
|
-
[keys.name]: 'component',
|
|
376
|
-
[keys.displayName]: '管理',
|
|
377
|
-
[keys.value]: [
|
|
378
|
-
{
|
|
379
|
-
[keys.name]: 'list',
|
|
380
|
-
[keys.displayName]: '',
|
|
381
|
-
[keys.value]: presetChidlren
|
|
382
|
-
.filter((id) => defineMap[id])
|
|
383
|
-
.map((id) => {
|
|
384
|
-
const { base } = defineMap[id];
|
|
385
|
-
return { label: base.name, name: base.module_name };
|
|
386
|
-
}),
|
|
387
|
-
[keys.type]: 'children',
|
|
388
|
-
[keys.children]: defineChildren
|
|
389
|
-
.filter((id) => defineMap[id])
|
|
390
|
-
.map((id) => {
|
|
391
|
-
const { base } = defineMap[id];
|
|
392
|
-
return { name: base.name, value: base.module_name };
|
|
393
|
-
}),
|
|
394
|
-
},
|
|
395
|
-
],
|
|
396
|
-
},
|
|
397
|
-
],
|
|
398
|
-
};
|
|
399
|
-
mainConfig.configuration.push(children);
|
|
400
|
-
}
|
|
401
|
-
// 替换modulename
|
|
402
|
-
if (query.uniqueId) {
|
|
403
|
-
mainConfig.base.module_name += '.' + query.uniqueId;
|
|
404
|
-
}
|
|
405
|
-
// 删除平台无法识别的参数
|
|
406
|
-
delete mainConfig.children;
|
|
407
|
-
delete mainConfig.presetChildren;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
file += '\n';
|
|
411
|
-
file += `${vname}.getDefaultConfig=function(){return ${JSON.stringify(
|
|
412
|
-
mainConfig,
|
|
413
|
-
)}};`;
|
|
414
|
-
file += `${vname}.getDefaultData=function(){return ${JSON.stringify(
|
|
415
|
-
main.data,
|
|
416
|
-
)}};`;
|
|
417
|
-
file += `${vname}.getDefaultChildrenConfig=function(){return ${JSON.stringify(
|
|
418
|
-
subConfigs,
|
|
419
|
-
)}};`;
|
|
420
|
-
file += `${vname}.getDefaultChildrenData=function(){return ${JSON.stringify(
|
|
421
|
-
subDatas,
|
|
422
|
-
)}};`;
|
|
423
|
-
|
|
424
|
-
callback(null, file);
|
|
425
|
-
} catch (e) {
|
|
426
|
-
callback(e);
|
|
427
|
-
}
|
|
428
|
-
},
|
|
429
|
-
);
|
|
430
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* 给导出的组件添加static方法获取配置文件
|
|
3
|
+
*/
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const glob = require('glob');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
// const vm = require('vm');
|
|
8
|
+
|
|
9
|
+
const parser = require('@babel/parser').parse;
|
|
10
|
+
const traverse = require('@babel/traverse').default;
|
|
11
|
+
const generate = require('@babel/generator').default;
|
|
12
|
+
const t = require('@babel/types');
|
|
13
|
+
|
|
14
|
+
function getOldDimension(width, height, hasUndeline) {
|
|
15
|
+
if (hasUndeline) {
|
|
16
|
+
return {
|
|
17
|
+
_name: 'dimension', // 定义位置宽高
|
|
18
|
+
_displayName: '位置尺寸',
|
|
19
|
+
_value: [
|
|
20
|
+
{
|
|
21
|
+
_name: 'chartPosition',
|
|
22
|
+
_displayName: '图表位置',
|
|
23
|
+
_value: [
|
|
24
|
+
{
|
|
25
|
+
_name: 'left',
|
|
26
|
+
_displayName: 'X轴坐标',
|
|
27
|
+
_value: 100, // 开发环境默认显示位置
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
_name: 'top',
|
|
31
|
+
_displayName: 'Y轴坐标',
|
|
32
|
+
_value: 100, // 开发环境默认显示位置
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
_name: 'chartDimension',
|
|
38
|
+
_displayName: '图表尺寸',
|
|
39
|
+
_value: [
|
|
40
|
+
{
|
|
41
|
+
_name: 'width',
|
|
42
|
+
_displayName: '宽度',
|
|
43
|
+
_value: width, // 组件默认宽
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
_name: 'height',
|
|
47
|
+
_displayName: '高度',
|
|
48
|
+
_value: height, // 组件默认高
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
name: 'dimension', // 定义位置宽高
|
|
57
|
+
displayName: '位置尺寸',
|
|
58
|
+
value: [
|
|
59
|
+
{
|
|
60
|
+
name: 'chartPosition',
|
|
61
|
+
displayName: '图表位置',
|
|
62
|
+
value: [
|
|
63
|
+
{
|
|
64
|
+
name: 'left',
|
|
65
|
+
displayName: 'X轴坐标',
|
|
66
|
+
value: 100, // 开发环境默认显示位置
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: 'top',
|
|
70
|
+
displayName: 'Y轴坐标',
|
|
71
|
+
value: 100, // 开发环境默认显示位置
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
name: 'chartDimension',
|
|
77
|
+
displayName: '图表尺寸',
|
|
78
|
+
value: [
|
|
79
|
+
{
|
|
80
|
+
name: 'width',
|
|
81
|
+
displayName: '宽度',
|
|
82
|
+
value: width, // 组件默认宽
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'height',
|
|
86
|
+
displayName: '高度',
|
|
87
|
+
value: height, // 组件默认高
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = function (content, map, meta) {
|
|
96
|
+
var callback = this.async();
|
|
97
|
+
const query = this.query || {};
|
|
98
|
+
|
|
99
|
+
const modulePath = this.context;
|
|
100
|
+
let file = content;
|
|
101
|
+
|
|
102
|
+
// 兼容旧的写法
|
|
103
|
+
if (~file.indexOf('getDefaultConfig')) {
|
|
104
|
+
return callback(null, file);
|
|
105
|
+
}
|
|
106
|
+
const configPath = `${modulePath}/config`;
|
|
107
|
+
// 添加watcher依赖
|
|
108
|
+
this.addContextDependency(path.resolve(configPath));
|
|
109
|
+
|
|
110
|
+
glob(
|
|
111
|
+
`{*.data.{json,js},subConfigs/*.data.{json,js}}`,
|
|
112
|
+
{
|
|
113
|
+
cwd: configPath,
|
|
114
|
+
},
|
|
115
|
+
function (err, matches) {
|
|
116
|
+
if (err || matches.length === 0) {
|
|
117
|
+
callback(
|
|
118
|
+
`main.json or main.data.json cannot be found in ${configPath}`,
|
|
119
|
+
);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
// 读取配置文件
|
|
125
|
+
const main = {};
|
|
126
|
+
let subConfigs = [];
|
|
127
|
+
const subDatas = [];
|
|
128
|
+
const names = new Set();
|
|
129
|
+
if (
|
|
130
|
+
matches
|
|
131
|
+
.map((filename) => filename.split('.')[0])
|
|
132
|
+
.some((name, i) => {
|
|
133
|
+
let config;
|
|
134
|
+
try {
|
|
135
|
+
let jsonFile = fs.existsSync(
|
|
136
|
+
path.join(configPath, name + '.json'),
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
if (jsonFile) {
|
|
140
|
+
config = JSON.parse(
|
|
141
|
+
fs.readFileSync(path.resolve(configPath, name + '.json')),
|
|
142
|
+
{ encoding: 'utf8' },
|
|
143
|
+
);
|
|
144
|
+
} else {
|
|
145
|
+
Object.keys(require.cache).forEach(function (key) {
|
|
146
|
+
if (key.includes('node_modules')) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
delete require.cache[key];
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
config = require(path.resolve(
|
|
153
|
+
configPath,
|
|
154
|
+
name + '.js',
|
|
155
|
+
)).default;
|
|
156
|
+
}
|
|
157
|
+
} catch (error) {
|
|
158
|
+
error.message += ` in ${configPath}${name}.json or ${configPath}${name}.js`;
|
|
159
|
+
callback(error);
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let data;
|
|
164
|
+
try {
|
|
165
|
+
let dataJsonFile = fs.existsSync(
|
|
166
|
+
path.join(configPath, name + '.data.json'),
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
if (dataJsonFile) {
|
|
170
|
+
data = JSON.parse(
|
|
171
|
+
fs.readFileSync(
|
|
172
|
+
path.resolve(configPath, name + '.data.json'),
|
|
173
|
+
{ encoding: 'utf8' },
|
|
174
|
+
),
|
|
175
|
+
);
|
|
176
|
+
} else {
|
|
177
|
+
delete require.cache[
|
|
178
|
+
require.resolve(path.resolve(configPath, name + '.data.js'))
|
|
179
|
+
];
|
|
180
|
+
data = require(path.resolve(
|
|
181
|
+
configPath,
|
|
182
|
+
name + '.data.js',
|
|
183
|
+
)).default;
|
|
184
|
+
}
|
|
185
|
+
} catch (error) {
|
|
186
|
+
error.message += ` in ${configPath}${name}.data.json or ${configPath}${name}.data.js`;
|
|
187
|
+
callback(error);
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (names.has(config.base.module_name)) {
|
|
192
|
+
callback(
|
|
193
|
+
new Error(
|
|
194
|
+
`base.module_name ${config.base.module_name} was duplicated in ${configPath}`,
|
|
195
|
+
),
|
|
196
|
+
);
|
|
197
|
+
return true;
|
|
198
|
+
} else {
|
|
199
|
+
names.add(config.base.module_name);
|
|
200
|
+
}
|
|
201
|
+
if (name === 'main') {
|
|
202
|
+
main.config = config;
|
|
203
|
+
main.data = data;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const id = Math.random().toString(16).slice(2);
|
|
207
|
+
config.id = id;
|
|
208
|
+
data.id = id;
|
|
209
|
+
subConfigs.push(config);
|
|
210
|
+
subDatas.push(data);
|
|
211
|
+
})
|
|
212
|
+
)
|
|
213
|
+
return;
|
|
214
|
+
|
|
215
|
+
// 解析导出变量名
|
|
216
|
+
let vname = '',
|
|
217
|
+
exportName = `__EASYV_COMPONENT__`;
|
|
218
|
+
const ast = parser(content, {
|
|
219
|
+
sourceType: 'module',
|
|
220
|
+
plugins: ['jsx', 'typescript', 'classProperties'],
|
|
221
|
+
});
|
|
222
|
+
traverse(ast, {
|
|
223
|
+
ExportDefaultDeclaration(path) {
|
|
224
|
+
const child = path.node.declaration;
|
|
225
|
+
|
|
226
|
+
if (child.type === 'ClassDeclaration') {
|
|
227
|
+
// e.g: export default class a {}
|
|
228
|
+
if (child.id) {
|
|
229
|
+
vname = child.id.name;
|
|
230
|
+
// e.g: export default class {}
|
|
231
|
+
} else {
|
|
232
|
+
vname = exportName;
|
|
233
|
+
child.id = t.identifier(exportName);
|
|
234
|
+
}
|
|
235
|
+
// e.g: export default memo()
|
|
236
|
+
// e.g: export default () => {}
|
|
237
|
+
} else if (
|
|
238
|
+
!!~[
|
|
239
|
+
'CallExpression',
|
|
240
|
+
'ArrowFunctionExpression',
|
|
241
|
+
'FunctionExpression',
|
|
242
|
+
].indexOf(child.type)
|
|
243
|
+
) {
|
|
244
|
+
const exportIdentifier = t.identifier(exportName);
|
|
245
|
+
path.insertBefore(
|
|
246
|
+
t.variableDeclaration('const', [
|
|
247
|
+
t.variableDeclarator(exportIdentifier, child),
|
|
248
|
+
]),
|
|
249
|
+
);
|
|
250
|
+
path.node.declaration = exportIdentifier;
|
|
251
|
+
vname = exportName;
|
|
252
|
+
// e.g: export default function(){}
|
|
253
|
+
} else if (!!~['FunctionDeclaration'].indexOf(child.type)) {
|
|
254
|
+
const exportIdentifier = t.identifier(exportName);
|
|
255
|
+
child.id = exportIdentifier;
|
|
256
|
+
vname = exportName;
|
|
257
|
+
// e.g: export default a;
|
|
258
|
+
} else if (child.type === 'Identifier') {
|
|
259
|
+
vname = child.name;
|
|
260
|
+
}
|
|
261
|
+
path.stop();
|
|
262
|
+
},
|
|
263
|
+
ExportNamedDeclaration(path) {
|
|
264
|
+
const child = path.node.declaration;
|
|
265
|
+
// e.g: export const a = {};
|
|
266
|
+
if (child.type === 'VariableDeclaration') {
|
|
267
|
+
const exportIdentifier = child.declarations[0].id;
|
|
268
|
+
vname = exportIdentifier.name;
|
|
269
|
+
path.replaceWith(child);
|
|
270
|
+
path.insertAfter(t.exportDefaultDeclaration(exportIdentifier));
|
|
271
|
+
// e.g: export function a(){};
|
|
272
|
+
} else if (child.type === 'FunctionDeclaration') {
|
|
273
|
+
const exportIdentifier = child.id;
|
|
274
|
+
path.replaceWith(child);
|
|
275
|
+
path.insertAfter(t.exportDefaultDeclaration(exportIdentifier));
|
|
276
|
+
}
|
|
277
|
+
path.stop();
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
file = generate(ast, { quotes: 'single', retainLines: false }).code;
|
|
281
|
+
if (!vname) {
|
|
282
|
+
return callback(
|
|
283
|
+
`cannot found default export object in ${path.join(
|
|
284
|
+
modulePath,
|
|
285
|
+
'index',
|
|
286
|
+
)}`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
// 检查width/height
|
|
290
|
+
const mainConfig = main.config;
|
|
291
|
+
// if (!('width' in mainConfig) || !('height' in mainConfig)) {
|
|
292
|
+
// return callback(`main.json lack field 'width' or 'height'.`)
|
|
293
|
+
// }
|
|
294
|
+
|
|
295
|
+
// 添加width/height属性兼容老版本
|
|
296
|
+
let updateChart = false;
|
|
297
|
+
const hasUnderline = mainConfig.configuration[0]
|
|
298
|
+
? '_value' in mainConfig.configuration[0]
|
|
299
|
+
: false;
|
|
300
|
+
const keys = {
|
|
301
|
+
name: hasUnderline ? '_name' : 'name',
|
|
302
|
+
displayName: hasUnderline ? '_displayName' : 'displayName',
|
|
303
|
+
value: hasUnderline ? '_value' : 'value',
|
|
304
|
+
type: hasUnderline ? '_type' : 'type',
|
|
305
|
+
children: hasUnderline ? '_children' : 'children',
|
|
306
|
+
};
|
|
307
|
+
mainConfig.configuration = mainConfig.configuration.map((item) => {
|
|
308
|
+
if (item[keys.name] === 'chart') {
|
|
309
|
+
let values = item._value || item.value;
|
|
310
|
+
values = values.map((item) => {
|
|
311
|
+
if (item[keys.name] === 'dimension') {
|
|
312
|
+
updateChart = true;
|
|
313
|
+
return item;
|
|
314
|
+
// return getOldDimension(mainConfig.width, mainConfig.height, hasUnderline)
|
|
315
|
+
}
|
|
316
|
+
return item;
|
|
317
|
+
});
|
|
318
|
+
if (!updateChart) {
|
|
319
|
+
values.unshift(
|
|
320
|
+
getOldDimension(
|
|
321
|
+
mainConfig.width,
|
|
322
|
+
mainConfig.height,
|
|
323
|
+
hasUnderline,
|
|
324
|
+
),
|
|
325
|
+
);
|
|
326
|
+
updateChart = true;
|
|
327
|
+
}
|
|
328
|
+
item[keys.value] = values;
|
|
329
|
+
return item;
|
|
330
|
+
}
|
|
331
|
+
return item;
|
|
332
|
+
});
|
|
333
|
+
if (!updateChart) {
|
|
334
|
+
const chart = {
|
|
335
|
+
[keys.name]: 'chart',
|
|
336
|
+
[keys.displayName]: '组件',
|
|
337
|
+
[keys.value]: [
|
|
338
|
+
getOldDimension(
|
|
339
|
+
mainConfig.width,
|
|
340
|
+
mainConfig.height,
|
|
341
|
+
hasUnderline,
|
|
342
|
+
),
|
|
343
|
+
],
|
|
344
|
+
};
|
|
345
|
+
mainConfig.configuration.unshift(chart);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// 子组件排序,有配置项则以chilren排序,否则以文件顺序排序
|
|
349
|
+
if (mainConfig.children) {
|
|
350
|
+
subConfigs = mainConfig.children.reduce((all, item) => {
|
|
351
|
+
let childConfig = subConfigs.find(
|
|
352
|
+
(d) => d.base.module_name === item,
|
|
353
|
+
);
|
|
354
|
+
return childConfig ? all.concat(childConfig) : all;
|
|
355
|
+
}, []);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (query.isBuild) {
|
|
359
|
+
// 兼容平台子组件配置
|
|
360
|
+
if (subConfigs.length) {
|
|
361
|
+
const defineMap = subConfigs.reduce((a, b) => {
|
|
362
|
+
a[b.base.module_name] = b;
|
|
363
|
+
return a;
|
|
364
|
+
}, {});
|
|
365
|
+
const defineChildren =
|
|
366
|
+
mainConfig.children ||
|
|
367
|
+
subConfigs.map((item) => item.base.module_name);
|
|
368
|
+
const presetChidlren = mainConfig.presetChildren || [];
|
|
369
|
+
|
|
370
|
+
const children = {
|
|
371
|
+
[keys.name]: 'children',
|
|
372
|
+
[keys.displayName]: '子组件',
|
|
373
|
+
[keys.value]: [
|
|
374
|
+
{
|
|
375
|
+
[keys.name]: 'component',
|
|
376
|
+
[keys.displayName]: '管理',
|
|
377
|
+
[keys.value]: [
|
|
378
|
+
{
|
|
379
|
+
[keys.name]: 'list',
|
|
380
|
+
[keys.displayName]: '',
|
|
381
|
+
[keys.value]: presetChidlren
|
|
382
|
+
.filter((id) => defineMap[id])
|
|
383
|
+
.map((id) => {
|
|
384
|
+
const { base } = defineMap[id];
|
|
385
|
+
return { label: base.name, name: base.module_name };
|
|
386
|
+
}),
|
|
387
|
+
[keys.type]: 'children',
|
|
388
|
+
[keys.children]: defineChildren
|
|
389
|
+
.filter((id) => defineMap[id])
|
|
390
|
+
.map((id) => {
|
|
391
|
+
const { base } = defineMap[id];
|
|
392
|
+
return { name: base.name, value: base.module_name };
|
|
393
|
+
}),
|
|
394
|
+
},
|
|
395
|
+
],
|
|
396
|
+
},
|
|
397
|
+
],
|
|
398
|
+
};
|
|
399
|
+
mainConfig.configuration.push(children);
|
|
400
|
+
}
|
|
401
|
+
// 替换modulename
|
|
402
|
+
if (query.uniqueId) {
|
|
403
|
+
mainConfig.base.module_name += '.' + query.uniqueId;
|
|
404
|
+
}
|
|
405
|
+
// 删除平台无法识别的参数
|
|
406
|
+
delete mainConfig.children;
|
|
407
|
+
delete mainConfig.presetChildren;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
file += '\n';
|
|
411
|
+
file += `${vname}.getDefaultConfig=function(){return ${JSON.stringify(
|
|
412
|
+
mainConfig,
|
|
413
|
+
)}};`;
|
|
414
|
+
file += `${vname}.getDefaultData=function(){return ${JSON.stringify(
|
|
415
|
+
main.data,
|
|
416
|
+
)}};`;
|
|
417
|
+
file += `${vname}.getDefaultChildrenConfig=function(){return ${JSON.stringify(
|
|
418
|
+
subConfigs,
|
|
419
|
+
)}};`;
|
|
420
|
+
file += `${vname}.getDefaultChildrenData=function(){return ${JSON.stringify(
|
|
421
|
+
subDatas,
|
|
422
|
+
)}};`;
|
|
423
|
+
|
|
424
|
+
callback(null, file);
|
|
425
|
+
} catch (e) {
|
|
426
|
+
callback(e);
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
);
|
|
430
|
+
};
|