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