@easyv/react-component-builder 0.15.0 → 0.16.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.
@@ -1,18 +1,22 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const commonConfig = require('./webpack.config.common');
4
- const { appComponents, appBuild, webpackConfigExtend } = require('./paths');
5
- const webpack = require('webpack');
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const TerserWebpackPlugin = require("terser-webpack-plugin");
4
+ const commonConfig = require("./webpack.config.common");
5
+ const { appComponents, appBuild, webpackConfigExtend } = require("./paths");
6
+ const webpack = require("webpack");
6
7
 
7
8
  let webpackConfigExtendFn;
8
9
  if (fs.existsSync(webpackConfigExtend)) {
9
10
  webpackConfigExtendFn = require(webpackConfigExtend);
10
11
  }
11
12
 
12
- process.env.NODE_ENV = 'production';
13
+ process.env.NODE_ENV = "production";
13
14
 
14
- module.exports = ({ filename, module_name, version }, { uniqueId }) => {
15
- const entry = path.resolve(appComponents, filename, 'index')
15
+ module.exports = (
16
+ { filename, module_name, version },
17
+ { uniqueId, removeConsole }
18
+ ) => {
19
+ const entry = path.resolve(appComponents, filename, "index");
16
20
  const cf = {
17
21
  ...commonConfig({ isProduction: true, uniqueId, entrys: [entry] }),
18
22
  mode: process.env.NODE_ENV,
@@ -21,23 +25,46 @@ module.exports = ({ filename, module_name, version }, { uniqueId }) => {
21
25
  path: appBuild,
22
26
  filename: `${filename}.${version}.js`,
23
27
  library: `${module_name}@${version}`,
24
- libraryTarget: 'amd',
28
+ libraryTarget: "amd",
25
29
  },
26
- externals: ['react', 'react-dom', 'three', 'lodash', 'd3', '@material-ui/styles'],
30
+ externals: [
31
+ "react",
32
+ "react-dom",
33
+ "three",
34
+ "lodash",
35
+ "d3",
36
+ "@material-ui/styles",
37
+ ],
27
38
  devtool: false,
28
39
  plugins: [
29
40
  new webpack.ProvidePlugin({
30
- THREE: 'three',
31
- React: 'react',
32
- ReactDom: 'react-dom',
33
- })
34
- ]
35
- }
41
+ THREE: "three",
42
+ React: "react",
43
+ ReactDom: "react-dom",
44
+ }),
45
+ ],
46
+ };
36
47
 
37
48
  if (webpackConfigExtendFn) {
38
49
  return webpackConfigExtendFn(cf, {
39
- production: true
40
- })
50
+ production: true,
51
+ });
52
+ }
53
+
54
+ if (removeConsole) {
55
+ cf["optimization"] = {
56
+ minimizer: [
57
+ new TerserWebpackPlugin({
58
+ parallel: true,
59
+ terserOptions: {
60
+ compress: {
61
+ drop_console: true,
62
+ },
63
+ },
64
+ }),
65
+ ],
66
+ };
41
67
  }
68
+
42
69
  return cf;
43
70
  };
@@ -16,13 +16,6 @@ module.exports = function ({ isProduction, uniqueId, entrys }) {
16
16
  },
17
17
  },
18
18
  },
19
- {
20
- test: /\.worker\.(js|ts)$/,
21
- use: {
22
- loader: "worker-loader",
23
- },
24
- },
25
-
26
19
  {
27
20
  test: /\.(js|mjs|jsx|ts|tsx)$/,
28
21
  include: appSrc,
@@ -126,7 +119,7 @@ module.exports = function ({ isProduction, uniqueId, entrys }) {
126
119
  },
127
120
 
128
121
  resolve: {
129
- extensions: [".js", ".jsx"],
122
+ extensions: [".js", ".jsx", ".tsx", ".ts"],
130
123
  modules: ["node_modules", cliNodeModules],
131
124
  },
132
125
  };
package/launcher/build.js CHANGED
@@ -16,7 +16,7 @@ function canReadAsset(asset) {
16
16
  }
17
17
 
18
18
  module.exports = (name, option = {}) => {
19
- const { unique = true } = option;
19
+ const { unique = true, removeConsole } = option;
20
20
  return new Promise((resolve, reject) => {
21
21
  // 检查组件是否存在
22
22
  // 开始打包
@@ -43,7 +43,9 @@ module.exports = (name, option = {}) => {
43
43
  const { base } = checkComponentFiles(sourcePath);
44
44
  base.filename = `${base.module_name}`;
45
45
  base.module_name = `${base.module_name}${unique ? "." + easyvcid : ""}`;
46
- const compiler = webpack(getWebpackConfig(base, { uniqueId: easyvcid }));
46
+ const compiler = webpack(
47
+ getWebpackConfig(base, { uniqueId: easyvcid, removeConsole })
48
+ );
47
49
 
48
50
  console.log("Creating an optimized production build...");
49
51
 
package/launcher/start.js CHANGED
@@ -148,7 +148,7 @@ function start({ port, modules }) {
148
148
 
149
149
  function getBuildEntrys(modules = []) {
150
150
  // 优先级
151
- const suffix = ["jsx", "js"];
151
+ const suffix = ["jsx", "tsx", "js", "ts"];
152
152
 
153
153
  return new Promise((resolve, reject) => {
154
154
  // 两种获取方式配置方式
@@ -1,49 +1,49 @@
1
1
  /**
2
2
  * 给导出的组件添加static方法获取配置文件
3
3
  */
4
- const path = require("path");
5
- const glob = require("glob");
6
- const fs = require("fs");
4
+ const path = require('path');
5
+ const glob = require('glob');
6
+ const fs = require('fs');
7
7
 
8
- const parser = require("@babel/parser").parse;
9
- const traverse = require("@babel/traverse").default;
10
- const generate = require("@babel/generator").default;
11
- const t = require("@babel/types");
8
+ const parser = require('@babel/parser').parse;
9
+ const traverse = require('@babel/traverse').default;
10
+ const generate = require('@babel/generator').default;
11
+ const t = require('@babel/types');
12
12
 
13
13
  function getOldDimension(width, height, hasUndeline) {
14
14
  if (hasUndeline) {
15
15
  return {
16
- _name: "dimension", // 定义位置宽高
17
- _displayName: "位置尺寸",
16
+ _name: 'dimension', // 定义位置宽高
17
+ _displayName: '位置尺寸',
18
18
  _value: [
19
19
  {
20
- _name: "chartPosition",
21
- _displayName: "图表位置",
20
+ _name: 'chartPosition',
21
+ _displayName: '图表位置',
22
22
  _value: [
23
23
  {
24
- _name: "left",
25
- _displayName: "X轴坐标",
24
+ _name: 'left',
25
+ _displayName: 'X轴坐标',
26
26
  _value: 100, // 开发环境默认显示位置
27
27
  },
28
28
  {
29
- _name: "top",
30
- _displayName: "Y轴坐标",
29
+ _name: 'top',
30
+ _displayName: 'Y轴坐标',
31
31
  _value: 100, // 开发环境默认显示位置
32
32
  },
33
33
  ],
34
34
  },
35
35
  {
36
- _name: "chartDimension",
37
- _displayName: "图表尺寸",
36
+ _name: 'chartDimension',
37
+ _displayName: '图表尺寸',
38
38
  _value: [
39
39
  {
40
- _name: "width",
41
- _displayName: "宽度",
40
+ _name: 'width',
41
+ _displayName: '宽度',
42
42
  _value: width, // 组件默认宽
43
43
  },
44
44
  {
45
- _name: "height",
46
- _displayName: "高度",
45
+ _name: 'height',
46
+ _displayName: '高度',
47
47
  _value: height, // 组件默认高
48
48
  },
49
49
  ],
@@ -52,37 +52,37 @@ function getOldDimension(width, height, hasUndeline) {
52
52
  };
53
53
  }
54
54
  return {
55
- name: "dimension", // 定义位置宽高
56
- displayName: "位置尺寸",
55
+ name: 'dimension', // 定义位置宽高
56
+ displayName: '位置尺寸',
57
57
  value: [
58
58
  {
59
- name: "chartPosition",
60
- displayName: "图表位置",
59
+ name: 'chartPosition',
60
+ displayName: '图表位置',
61
61
  value: [
62
62
  {
63
- name: "left",
64
- displayName: "X轴坐标",
63
+ name: 'left',
64
+ displayName: 'X轴坐标',
65
65
  value: 100, // 开发环境默认显示位置
66
66
  },
67
67
  {
68
- name: "top",
69
- displayName: "Y轴坐标",
68
+ name: 'top',
69
+ displayName: 'Y轴坐标',
70
70
  value: 100, // 开发环境默认显示位置
71
71
  },
72
72
  ],
73
73
  },
74
74
  {
75
- name: "chartDimension",
76
- displayName: "图表尺寸",
75
+ name: 'chartDimension',
76
+ displayName: '图表尺寸',
77
77
  value: [
78
78
  {
79
- name: "width",
80
- displayName: "宽度",
79
+ name: 'width',
80
+ displayName: '宽度',
81
81
  value: width, // 组件默认宽
82
82
  },
83
83
  {
84
- name: "height",
85
- displayName: "高度",
84
+ name: 'height',
85
+ displayName: '高度',
86
86
  value: height, // 组件默认高
87
87
  },
88
88
  ],
@@ -99,7 +99,7 @@ module.exports = function (content, map, meta) {
99
99
  let file = content;
100
100
 
101
101
  // 兼容旧的写法
102
- if (~file.indexOf("getDefaultConfig")) {
102
+ if (~file.indexOf('getDefaultConfig')) {
103
103
  return callback(null, file);
104
104
  }
105
105
  const configPath = `${modulePath}/config`;
@@ -131,25 +131,27 @@ module.exports = function (content, map, meta) {
131
131
  const names = new Set();
132
132
  if (
133
133
  matches
134
- .map((filename) => filename.split(".")[0])
134
+ .map((filename) => filename.split('.')[0])
135
135
  .some((name, i) => {
136
136
  let config;
137
137
  try {
138
138
  let jsonFile = fs.existsSync(
139
- path.join(configPath, name + ".json")
139
+ path.join(configPath, name + '.json')
140
140
  );
141
141
 
142
142
  if (jsonFile) {
143
143
  config = JSON.parse(
144
- fs.readFileSync(path.resolve(configPath, name + ".json")),
145
- { encoding: "utf8" }
144
+ fs.readFileSync(path.resolve(configPath, name + '.json')),
145
+ { encoding: 'utf8' }
146
146
  );
147
147
  } else {
148
148
  delete require.cache[
149
- require.resolve(path.resolve(configPath, name + ".js"))
149
+ require.resolve(path.resolve(configPath, name + '.js'))
150
150
  ];
151
- config = require(path.resolve(configPath, name + ".js"))
152
- .default;
151
+ config = require(path.resolve(
152
+ configPath,
153
+ name + '.js'
154
+ )).default;
153
155
  }
154
156
  } catch (error) {
155
157
  error.message += ` in ${configPath}${name}.json or ${configPath}${name}.js`;
@@ -160,22 +162,24 @@ module.exports = function (content, map, meta) {
160
162
  let data;
161
163
  try {
162
164
  let dataJsonFile = fs.existsSync(
163
- path.join(configPath, name + ".data.json")
165
+ path.join(configPath, name + '.data.json')
164
166
  );
165
167
 
166
168
  if (dataJsonFile) {
167
169
  data = JSON.parse(
168
170
  fs.readFileSync(
169
- path.resolve(configPath, name + ".data.json"),
170
- { encoding: "utf8" }
171
+ path.resolve(configPath, name + '.data.json'),
172
+ { encoding: 'utf8' }
171
173
  )
172
174
  );
173
175
  } else {
174
176
  delete require.cache[
175
- require.resolve(path.resolve(configPath, name + ".data.js"))
177
+ require.resolve(path.resolve(configPath, name + '.data.js'))
176
178
  ];
177
- data = require(path.resolve(configPath, name + ".data.js"))
178
- .default;
179
+ data = require(path.resolve(
180
+ configPath,
181
+ name + '.data.js'
182
+ )).default;
179
183
  }
180
184
  } catch (error) {
181
185
  error.message += ` in ${configPath}${name}.data.json or ${configPath}${name}.data.js`;
@@ -193,7 +197,7 @@ module.exports = function (content, map, meta) {
193
197
  } else {
194
198
  names.add(config.base.module_name);
195
199
  }
196
- if (name === "main") {
200
+ if (name === 'main') {
197
201
  main.config = config;
198
202
  main.data = data;
199
203
  return;
@@ -208,17 +212,17 @@ module.exports = function (content, map, meta) {
208
212
  return;
209
213
 
210
214
  // 解析导出变量名
211
- let vname = "",
215
+ let vname = '',
212
216
  exportName = `__EASYV_COMPONENT__`;
213
217
  const ast = parser(content, {
214
- sourceType: "module",
215
- plugins: ["jsx", "classProperties"],
218
+ sourceType: 'module',
219
+ plugins: ['jsx', 'typescript', 'classProperties'],
216
220
  });
217
221
  traverse(ast, {
218
222
  ExportDefaultDeclaration(path) {
219
223
  const child = path.node.declaration;
220
224
 
221
- if (child.type === "ClassDeclaration") {
225
+ if (child.type === 'ClassDeclaration') {
222
226
  // e.g: export default class a {}
223
227
  if (child.id) {
224
228
  vname = child.id.name;
@@ -231,26 +235,26 @@ module.exports = function (content, map, meta) {
231
235
  // e.g: export default () => {}
232
236
  } else if (
233
237
  !!~[
234
- "CallExpression",
235
- "ArrowFunctionExpression",
236
- "FunctionExpression",
238
+ 'CallExpression',
239
+ 'ArrowFunctionExpression',
240
+ 'FunctionExpression',
237
241
  ].indexOf(child.type)
238
242
  ) {
239
243
  const exportIdentifier = t.identifier(exportName);
240
244
  path.insertBefore(
241
- t.variableDeclaration("const", [
245
+ t.variableDeclaration('const', [
242
246
  t.variableDeclarator(exportIdentifier, child),
243
247
  ])
244
248
  );
245
249
  path.node.declaration = exportIdentifier;
246
250
  vname = exportName;
247
251
  // e.g: export default function(){}
248
- } else if (!!~["FunctionDeclaration"].indexOf(child.type)) {
252
+ } else if (!!~['FunctionDeclaration'].indexOf(child.type)) {
249
253
  const exportIdentifier = t.identifier(exportName);
250
254
  child.id = exportIdentifier;
251
255
  vname = exportName;
252
256
  // e.g: export default a;
253
- } else if (child.type === "Identifier") {
257
+ } else if (child.type === 'Identifier') {
254
258
  vname = child.name;
255
259
  }
256
260
  path.stop();
@@ -258,13 +262,13 @@ module.exports = function (content, map, meta) {
258
262
  ExportNamedDeclaration(path) {
259
263
  const child = path.node.declaration;
260
264
  // e.g: export const a = {};
261
- if (child.type === "VariableDeclaration") {
265
+ if (child.type === 'VariableDeclaration') {
262
266
  const exportIdentifier = child.declarations[0].id;
263
267
  vname = exportIdentifier.name;
264
268
  path.replaceWith(child);
265
269
  path.insertAfter(t.exportDefaultDeclaration(exportIdentifier));
266
270
  // e.g: export function a(){};
267
- } else if (child.type === "FunctionDeclaration") {
271
+ } else if (child.type === 'FunctionDeclaration') {
268
272
  const exportIdentifier = child.id;
269
273
  path.replaceWith(child);
270
274
  path.insertAfter(t.exportDefaultDeclaration(exportIdentifier));
@@ -272,12 +276,12 @@ module.exports = function (content, map, meta) {
272
276
  path.stop();
273
277
  },
274
278
  });
275
- file = generate(ast, { quotes: "single", retainLines: false }).code;
279
+ file = generate(ast, { quotes: 'single', retainLines: false }).code;
276
280
  if (!vname) {
277
281
  return callback(
278
282
  `cannot found default export object in ${path.join(
279
283
  modulePath,
280
- "index"
284
+ 'index'
281
285
  )}`
282
286
  );
283
287
  }
@@ -290,20 +294,20 @@ module.exports = function (content, map, meta) {
290
294
  // 添加width/height属性兼容老版本
291
295
  let updateChart = false;
292
296
  const hasUnderline = mainConfig.configuration[0]
293
- ? "_value" in mainConfig.configuration[0]
297
+ ? '_value' in mainConfig.configuration[0]
294
298
  : false;
295
299
  const keys = {
296
- name: hasUnderline ? "_name" : "name",
297
- displayName: hasUnderline ? "_displayName" : "displayName",
298
- value: hasUnderline ? "_value" : "value",
299
- type: hasUnderline ? "_type" : "type",
300
- children: hasUnderline ? "_children" : "children",
300
+ name: hasUnderline ? '_name' : 'name',
301
+ displayName: hasUnderline ? '_displayName' : 'displayName',
302
+ value: hasUnderline ? '_value' : 'value',
303
+ type: hasUnderline ? '_type' : 'type',
304
+ children: hasUnderline ? '_children' : 'children',
301
305
  };
302
306
  mainConfig.configuration = mainConfig.configuration.map((item) => {
303
- if (item[keys.name] === "chart") {
307
+ if (item[keys.name] === 'chart') {
304
308
  let values = item._value || item.value;
305
309
  values = values.map((item) => {
306
- if (item[keys.name] === "dimension") {
310
+ if (item[keys.name] === 'dimension') {
307
311
  updateChart = true;
308
312
  return item;
309
313
  // return getOldDimension(mainConfig.width, mainConfig.height, hasUnderline)
@@ -327,8 +331,8 @@ module.exports = function (content, map, meta) {
327
331
  });
328
332
  if (!updateChart) {
329
333
  const chart = {
330
- [keys.name]: "chart",
331
- [keys.displayName]: "组件",
334
+ [keys.name]: 'chart',
335
+ [keys.displayName]: '组件',
332
336
  [keys.value]: [
333
337
  getOldDimension(
334
338
  mainConfig.width,
@@ -363,23 +367,23 @@ module.exports = function (content, map, meta) {
363
367
  const presetChidlren = mainConfig.presetChildren || [];
364
368
 
365
369
  const children = {
366
- [keys.name]: "children",
367
- [keys.displayName]: "子组件",
370
+ [keys.name]: 'children',
371
+ [keys.displayName]: '子组件',
368
372
  [keys.value]: [
369
373
  {
370
- [keys.name]: "component",
371
- [keys.displayName]: "管理",
374
+ [keys.name]: 'component',
375
+ [keys.displayName]: '管理',
372
376
  [keys.value]: [
373
377
  {
374
- [keys.name]: "list",
375
- [keys.displayName]: "",
378
+ [keys.name]: 'list',
379
+ [keys.displayName]: '',
376
380
  [keys.value]: presetChidlren
377
381
  .filter((id) => defineMap[id])
378
382
  .map((id) => {
379
383
  const { base } = defineMap[id];
380
384
  return { label: base.name, name: base.module_name };
381
385
  }),
382
- [keys.type]: "children",
386
+ [keys.type]: 'children',
383
387
  [keys.children]: defineChildren
384
388
  .filter((id) => defineMap[id])
385
389
  .map((id) => {
@@ -395,14 +399,14 @@ module.exports = function (content, map, meta) {
395
399
  }
396
400
  // 替换modulename
397
401
  if (query.uniqueId) {
398
- mainConfig.base.module_name += "." + query.uniqueId;
402
+ mainConfig.base.module_name += '.' + query.uniqueId;
399
403
  }
400
404
  // 删除平台无法识别的参数
401
405
  delete mainConfig.children;
402
406
  delete mainConfig.presetChildren;
403
407
  }
404
408
 
405
- file += "\n";
409
+ file += '\n';
406
410
  file += `${vname}.getDefaultConfig=function(){return ${JSON.stringify(
407
411
  mainConfig
408
412
  )}};`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyv/react-component-builder",
3
- "version": "0.15.0",
3
+ "version": "0.16.2",
4
4
  "files": [
5
5
  "launcher",
6
6
  "templates",
@@ -48,14 +48,14 @@
48
48
  "url-loader": "2.3.0",
49
49
  "webpack": "4.42.0",
50
50
  "webpack-dev-server": "3.10.3",
51
- "webpack-manifest-plugin": "2.2.0",
52
- "worker-loader": "^3.0.8"
51
+ "webpack-manifest-plugin": "2.2.0"
53
52
  },
54
53
  "devDependencies": {
55
54
  "@types/lodash": "^4.14.157",
56
55
  "@types/node": "^12.0.0",
57
56
  "@types/react": "^16.9.0",
58
- "@types/react-dom": "^16.9.0"
57
+ "@types/react-dom": "^16.9.0",
58
+ "terser-webpack-plugin": "^4.2.3"
59
59
  },
60
60
  "easyv": {
61
61
  "src": "examples",