@easyv/react-component-builder 0.17.1 → 0.18.0

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/launcher/build.js CHANGED
@@ -1,107 +1,107 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const chalk = require("chalk");
4
- const webpack = require("webpack");
5
- const filesize = require("filesize");
6
- const { appComponents, appBuild } = require("../config/paths");
7
- const { checkComponentFiles } = require("../utils");
8
- const getWebpackConfig = require("../config/webpack.config.build");
9
-
10
- function canReadAsset(asset) {
11
- return (
12
- /\.(js|css)$/.test(asset) &&
13
- !/service-worker\.js/.test(asset) &&
14
- !/precache-manifest\.[0-9a-f]+\.js/.test(asset)
15
- );
16
- }
17
-
18
- module.exports = (name, option = {}) => {
19
- const { unique = true, removeConsole } = option;
20
- return new Promise((resolve, reject) => {
21
- // 检查组件是否存在
22
- // 开始打包
23
- // 打包成功 返回bundle路径
24
-
25
- const sourcePath = path.join(appComponents, name);
26
-
27
- let easyvcid;
28
- if (unique) {
29
- // 生成组件唯一标识
30
- const easyvc = path.join(sourcePath, ".easyvc");
31
- if (fs.existsSync(easyvc)) {
32
- easyvcid = fs.readFileSync(easyvc, { encoding: "utf8" });
33
- }
34
- if (!easyvcid) {
35
- easyvcid = Array.from({ length: 6 })
36
- .map(() => Math.random().toString(32).slice(2, 3))
37
- .join("");
38
- fs.writeFileSync(easyvc, easyvcid, { encoding: "utf8" });
39
- }
40
- }
41
-
42
- // 重写base的modulename
43
- const { base } = checkComponentFiles(sourcePath);
44
- base.filename = `${base.module_name}`;
45
- base.module_name = `${base.module_name}${unique ? "." + easyvcid : ""}`;
46
- const compiler = webpack(
47
- getWebpackConfig(base, { uniqueId: easyvcid, removeConsole })
48
- );
49
-
50
- console.log("Creating an optimized production build...");
51
-
52
- compiler.run((err, stats) => {
53
- const message = stats.toJson({ all: true, warnings: true, errors: true });
54
-
55
- const errors = err || message.errors;
56
- if (errors.length) {
57
- console.log(chalk.red("Failed to compile.\n"));
58
- console.log(chalk.red(errors + "\n"));
59
- process.exit(1);
60
- }
61
-
62
- if (message.warnings.length) {
63
- console.log(chalk.yellow("Compiled with warnings.\n"));
64
- console.log(message.warnings.join("\n\n"));
65
- } else {
66
- // successful
67
- console.log(chalk.green("Compiled successfully."));
68
- console.log(`Time: ${message.time / 1000}s`);
69
- console.log(`Built at: ${new Date(message.builtAt)}\n`);
70
- console.log("File sizes:\n");
71
-
72
- message.assets
73
- .filter((asset) => canReadAsset(asset.name))
74
- .map((asset) => {
75
- var fileContents = fs.readFileSync(
76
- path.resolve(appBuild, asset.name)
77
- );
78
- return {
79
- folder: path.join(
80
- path.basename(appBuild),
81
- path.dirname(asset.name)
82
- ),
83
- name: path.basename(asset.name),
84
- size: filesize(fileContents.length),
85
- };
86
- })
87
- .sort((a, b) => b.size - a.size)
88
- .map((asset) => {
89
- console.log(
90
- " " +
91
- asset.size +
92
- " " +
93
- chalk.dim(asset.folder + path.sep) +
94
- chalk.cyan(asset.name)
95
- );
96
- });
97
-
98
- console.log();
99
- }
100
- resolve({
101
- file: path.join(message.outputPath, message.assetsByChunkName.main),
102
- base,
103
- sourcePath: sourcePath,
104
- });
105
- });
106
- });
107
- };
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const chalk = require("chalk");
4
+ const webpack = require("webpack");
5
+ const filesize = require("filesize");
6
+ const { appComponents, appBuild } = require("../config/paths");
7
+ const { checkComponentFiles } = require("../utils");
8
+ const getWebpackConfig = require("../config/webpack.config.build");
9
+
10
+ function canReadAsset(asset) {
11
+ return (
12
+ /\.(js|css)$/.test(asset) &&
13
+ !/service-worker\.js/.test(asset) &&
14
+ !/precache-manifest\.[0-9a-f]+\.js/.test(asset)
15
+ );
16
+ }
17
+
18
+ module.exports = (name, option = {}) => {
19
+ const { unique = true, removeConsole } = option;
20
+ return new Promise((resolve, reject) => {
21
+ // 检查组件是否存在
22
+ // 开始打包
23
+ // 打包成功 返回bundle路径
24
+
25
+ const sourcePath = path.join(appComponents, name);
26
+
27
+ let easyvcid;
28
+ if (unique) {
29
+ // 生成组件唯一标识
30
+ const easyvc = path.join(sourcePath, ".easyvc");
31
+ if (fs.existsSync(easyvc)) {
32
+ easyvcid = fs.readFileSync(easyvc, { encoding: "utf8" });
33
+ }
34
+ if (!easyvcid) {
35
+ easyvcid = Array.from({ length: 6 })
36
+ .map(() => Math.random().toString(32).slice(2, 3))
37
+ .join("");
38
+ fs.writeFileSync(easyvc, easyvcid, { encoding: "utf8" });
39
+ }
40
+ }
41
+
42
+ // 重写base的modulename
43
+ const { base } = checkComponentFiles(sourcePath);
44
+ base.filename = `${base.module_name}`;
45
+ base.module_name = `${base.module_name}${unique ? "." + easyvcid : ""}`;
46
+ const compiler = webpack(
47
+ getWebpackConfig(base, { uniqueId: easyvcid, removeConsole })
48
+ );
49
+
50
+ console.log("Creating an optimized production build...");
51
+
52
+ compiler.run((err, stats) => {
53
+ const message = stats.toJson({ all: true, warnings: true, errors: true });
54
+
55
+ const errors = err || message.errors;
56
+ if (errors.length) {
57
+ console.log(chalk.red("Failed to compile.\n"));
58
+ console.log(chalk.red(errors + "\n"));
59
+ process.exit(1);
60
+ }
61
+
62
+ if (message.warnings.length) {
63
+ console.log(chalk.yellow("Compiled with warnings.\n"));
64
+ console.log(message.warnings.join("\n\n"));
65
+ } else {
66
+ // successful
67
+ console.log(chalk.green("Compiled successfully."));
68
+ console.log(`Time: ${message.time / 1000}s`);
69
+ console.log(`Built at: ${new Date(message.builtAt)}\n`);
70
+ console.log("File sizes:\n");
71
+
72
+ message.assets
73
+ .filter((asset) => canReadAsset(asset.name))
74
+ .map((asset) => {
75
+ var fileContents = fs.readFileSync(
76
+ path.resolve(appBuild, asset.name)
77
+ );
78
+ return {
79
+ folder: path.join(
80
+ path.basename(appBuild),
81
+ path.dirname(asset.name)
82
+ ),
83
+ name: path.basename(asset.name),
84
+ size: filesize(fileContents.length),
85
+ };
86
+ })
87
+ .sort((a, b) => b.size - a.size)
88
+ .map((asset) => {
89
+ console.log(
90
+ " " +
91
+ asset.size +
92
+ " " +
93
+ chalk.dim(asset.folder + path.sep) +
94
+ chalk.cyan(asset.name)
95
+ );
96
+ });
97
+
98
+ console.log();
99
+ }
100
+ resolve({
101
+ file: path.join(message.outputPath, message.assetsByChunkName.main),
102
+ base,
103
+ sourcePath: sourcePath,
104
+ });
105
+ });
106
+ });
107
+ };
@@ -1,99 +1,99 @@
1
- var inquirer = require('inquirer');
2
- var path = require('path');
3
- const fs = require('fs');
4
- const fse = require('fs-extra');
5
- const chalk = require('chalk');
6
- const { appComponents, appPackage, templates } = require('../config/paths');
7
-
8
- const defaultValues = {
9
- module_name: 'text',
10
- name: '文本',
11
- };
12
- let templateValues = [
13
- {
14
- name: 'React演示',
15
- value: 'basic',
16
- },
17
- {
18
- name: 'Three Component',
19
- value: 'three-component',
20
- },
21
- ];
22
-
23
- module.exports = (cb, addCustom, generateCustom, add) => {
24
- if (!add) {
25
- const custom = addCustom();
26
- templateValues = [...templateValues, ...custom];
27
- }
28
-
29
- inquirer
30
- .prompt([
31
- {
32
- type: 'input',
33
- name: 'moduleName',
34
- message: '你的组件名称是什么?',
35
- validate: function (value) {
36
- return /^[a-zA-Z][\w-]+$/.test(value)
37
- ? true
38
- : '名称只能是 英文开头 包含 英文、数字、_- 的字符串';
39
- },
40
- default: defaultValues.module_name,
41
- },
42
- {
43
- type: 'input',
44
- name: 'name',
45
- message: '中文别名是什么?',
46
- default: defaultValues.name,
47
- },
48
- {
49
- type: 'list',
50
- name: 'template',
51
- message: '要添加哪种模版?',
52
- choices: templateValues,
53
- },
54
- ])
55
- .then((answers) => {
56
- let { moduleName, name, template } = answers;
57
- const base = {
58
- name: !add ? name : '##name##',
59
- module_name: !add ? moduleName : '##module_name##',
60
- version: '1.0.0',
61
- show: 1,
62
- };
63
-
64
- const copyPath = path.join(appComponents, moduleName);
65
- // 检查重名
66
- if (fs.existsSync(copyPath)) {
67
- console.log(chalk.red(`组件名称已存在`));
68
- process.exit();
69
- }
70
-
71
- if (template.includes('custom-')) {
72
- generateCustom(name, moduleName, template.split('custom-')[1]);
73
- } else {
74
- // 重写配置
75
- const templatePath = path.join(templates, template);
76
- const configPath = 'config/main.json';
77
-
78
- fse.copySync(templatePath, copyPath, {
79
- filter: (src, dest) => {
80
- if (src === path.join(templatePath, configPath)) {
81
- const config = require(src);
82
- config.base = base;
83
- fs.writeFileSync(
84
- path.join(copyPath, configPath),
85
- JSON.stringify(config, null, 2),
86
- );
87
- return 0;
88
- }
89
- return 1;
90
- },
91
- });
92
- }
93
-
94
- console.log();
95
- console.log(chalk.green(`模版添加成功`));
96
- cb && cb();
97
- })
98
- .catch((error) => cb && cb(error));
99
- };
1
+ var inquirer = require('inquirer');
2
+ var path = require('path');
3
+ const fs = require('fs');
4
+ const fse = require('fs-extra');
5
+ const chalk = require('chalk');
6
+ const { appComponents, appPackage, templates } = require('../config/paths');
7
+
8
+ const defaultValues = {
9
+ module_name: 'text',
10
+ name: '文本',
11
+ };
12
+ let templateValues = [
13
+ {
14
+ name: 'React演示',
15
+ value: 'basic',
16
+ },
17
+ {
18
+ name: 'Three Component',
19
+ value: 'three-component',
20
+ },
21
+ ];
22
+
23
+ module.exports = (cb, addCustom, generateCustom, add) => {
24
+ if (!add) {
25
+ const custom = addCustom();
26
+ templateValues = [...templateValues, ...custom];
27
+ }
28
+
29
+ inquirer
30
+ .prompt([
31
+ {
32
+ type: 'input',
33
+ name: 'moduleName',
34
+ message: '你的组件名称是什么?',
35
+ validate: function (value) {
36
+ return /^[a-zA-Z][\w-]+$/.test(value)
37
+ ? true
38
+ : '名称只能是 英文开头 包含 英文、数字、_- 的字符串';
39
+ },
40
+ default: defaultValues.module_name,
41
+ },
42
+ {
43
+ type: 'input',
44
+ name: 'name',
45
+ message: '中文别名是什么?',
46
+ default: defaultValues.name,
47
+ },
48
+ {
49
+ type: 'list',
50
+ name: 'template',
51
+ message: '要添加哪种模版?',
52
+ choices: templateValues,
53
+ },
54
+ ])
55
+ .then((answers) => {
56
+ let { moduleName, name, template } = answers;
57
+ const base = {
58
+ name: !add ? name : '##name##',
59
+ module_name: !add ? moduleName : '##module_name##',
60
+ version: '1.0.0',
61
+ show: 1,
62
+ };
63
+
64
+ const copyPath = path.join(appComponents, moduleName);
65
+ // 检查重名
66
+ if (fs.existsSync(copyPath)) {
67
+ console.log(chalk.red(`组件名称已存在`));
68
+ process.exit();
69
+ }
70
+
71
+ if (template.includes('custom-')) {
72
+ generateCustom(name, moduleName, template.substring(7));
73
+ } else {
74
+ // 重写配置
75
+ const templatePath = path.join(templates, template);
76
+ const configPath = 'config/main.json';
77
+
78
+ fse.copySync(templatePath, copyPath, {
79
+ filter: (src, dest) => {
80
+ if (src === path.join(templatePath, configPath)) {
81
+ const config = require(src);
82
+ config.base = base;
83
+ fs.writeFileSync(
84
+ path.join(copyPath, configPath),
85
+ JSON.stringify(config, null, 2),
86
+ );
87
+ return 0;
88
+ }
89
+ return 1;
90
+ },
91
+ });
92
+ }
93
+
94
+ console.log();
95
+ console.log(chalk.green(`模版添加成功`));
96
+ cb && cb();
97
+ })
98
+ .catch((error) => cb && cb(error));
99
+ };