@easyv/react-component-builder 0.19.3 → 0.19.5-beta.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.
@@ -1,126 +1,126 @@
1
- const HtmlPlugin = require('html-webpack-plugin');
2
- const path = require('path');
3
- const fs = require('fs');
4
- const commonConfig = require('./webpack.config.common');
5
- const webpack = require('webpack');
6
- const {
7
- appPackage,
8
- public: publicPath,
9
- webpackConfigExtend,
10
- cliNodeModules,
11
- } = require('./paths');
12
-
13
- let webpackConfigExtendFn;
14
- if (fs.existsSync(webpackConfigExtend)) {
15
- webpackConfigExtendFn = require(webpackConfigExtend);
16
- }
17
-
18
- process.env.NODE_ENV = 'development';
19
-
20
- module.exports = ({ port, modules }) => {
21
- const entrys = modules.reduce((acc, item) => {
22
- acc[item.name] = path.resolve(item.path);
23
- return acc;
24
- }, {});
25
-
26
- const cf = {
27
- ...commonConfig({
28
- isProduction: false,
29
- entrys,
30
- }),
31
-
32
- mode: 'development',
33
- entry: entrys,
34
- output: {
35
- filename: '[name].js',
36
- globalObject: 'this',
37
- libraryTarget: 'var',
38
- library: '__MAIN__', // 暴露到window上的属性名称
39
- },
40
- devtool: 'source-map',
41
- plugins: [
42
- new webpack.ProvidePlugin({
43
- THREE: 'three',
44
- React: 'react',
45
- ReactDom: 'react-dom',
46
- }),
47
- new HtmlPlugin({
48
- filename: 'index.html',
49
- template: path.join(publicPath, 'home.ejs'),
50
- templateParameters: {
51
- modules: Object.keys(entrys).toString(),
52
- },
53
- chunks: [''],
54
- }),
55
- ...Object.keys(entrys).map((name) => {
56
- return new HtmlPlugin({
57
- filename: `${name}.html`,
58
- template: path.join(publicPath, 'component.ejs'),
59
- templateParameters: {
60
- module_name: name,
61
- assets: appPackage.easyv.assets,
62
- },
63
- chunks: [name],
64
- });
65
- }),
66
- ],
67
- externals: {
68
- react: 'react',
69
- 'react-dom': 'ReactDOM',
70
- },
71
-
72
- devServer: {
73
- port,
74
- hot: false,
75
- quiet: true,
76
- before: function (app, server, compiler) {
77
- var multer = require('multer');
78
- var storage = multer.memoryStorage();
79
- var upload = multer({ storage: storage });
80
- const fileCache = new Map();
81
- // 用户资源上传
82
- app.post(
83
- '/__develop__/assets',
84
- upload.single('file'),
85
- function (req, res) {
86
- const a = req.file;
87
- fileCache.set(a.originalname, a.buffer);
88
- res.json({ url: `${req.url}/${a.originalname}` });
89
- },
90
- );
91
- // 用户资源获取
92
- app.get('/__develop__/assets/:name', function (req, res) {
93
- const file = fileCache.get(req.params.name);
94
- if (!file) return res.status(404);
95
- res.send(file);
96
- });
97
- // 静态资源
98
- app.get('/static/*', function (req, res) {
99
- let filepath;
100
- [cliNodeModules].find((root) => {
101
- const p = req.url.replace('/static', root);
102
- if (fs.existsSync(p)) {
103
- filepath = p;
104
- return true;
105
- }
106
- });
107
- if (!filepath) return res.status(404).end();
108
- fs.readFile(filepath, (err, data) => {
109
- if (err) {
110
- return res.status(404).end();
111
- }
112
- res.send(data);
113
- });
114
- });
115
- },
116
- },
117
- };
118
-
119
- if (webpackConfigExtendFn) {
120
- return webpackConfigExtendFn(cf, {
121
- production: false,
122
- });
123
- }
124
-
125
- return cf;
126
- };
1
+ const HtmlPlugin = require('html-webpack-plugin');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+ const commonConfig = require('./webpack.config.common');
5
+ const webpack = require('webpack');
6
+ const {
7
+ appPackage,
8
+ public: publicPath,
9
+ webpackConfigExtend,
10
+ cliNodeModules,
11
+ } = require('./paths');
12
+
13
+ let webpackConfigExtendFn;
14
+ if (fs.existsSync(webpackConfigExtend)) {
15
+ webpackConfigExtendFn = require(webpackConfigExtend);
16
+ }
17
+
18
+ process.env.NODE_ENV = 'development';
19
+
20
+ module.exports = ({ port, modules }) => {
21
+ const entrys = modules.reduce((acc, item) => {
22
+ acc[item.name] = path.resolve(item.path);
23
+ return acc;
24
+ }, {});
25
+
26
+ const cf = {
27
+ ...commonConfig({
28
+ isProduction: false,
29
+ entrys,
30
+ }),
31
+
32
+ mode: 'development',
33
+ entry: entrys,
34
+ output: {
35
+ filename: '[name].js',
36
+ globalObject: 'this',
37
+ libraryTarget: 'var',
38
+ library: '__MAIN__', // 暴露到window上的属性名称
39
+ },
40
+ devtool: 'source-map',
41
+ plugins: [
42
+ new webpack.ProvidePlugin({
43
+ THREE: 'three',
44
+ React: 'react',
45
+ ReactDom: 'react-dom',
46
+ }),
47
+ new HtmlPlugin({
48
+ filename: 'index.html',
49
+ template: path.join(publicPath, 'home.ejs'),
50
+ templateParameters: {
51
+ modules: Object.keys(entrys).toString(),
52
+ },
53
+ chunks: [''],
54
+ }),
55
+ ...Object.keys(entrys).map((name) => {
56
+ return new HtmlPlugin({
57
+ filename: `${name}.html`,
58
+ template: path.join(publicPath, 'component.ejs'),
59
+ templateParameters: {
60
+ module_name: name,
61
+ assets: appPackage.easyv.assets,
62
+ },
63
+ chunks: [name],
64
+ });
65
+ }),
66
+ ],
67
+ externals: {
68
+ react: 'react',
69
+ 'react-dom': 'ReactDOM',
70
+ },
71
+
72
+ devServer: {
73
+ port,
74
+ hot: false,
75
+ quiet: true,
76
+ before: function (app, server, compiler) {
77
+ var multer = require('multer');
78
+ var storage = multer.memoryStorage();
79
+ var upload = multer({ storage: storage });
80
+ const fileCache = new Map();
81
+ // 用户资源上传
82
+ app.post(
83
+ '/__develop__/assets',
84
+ upload.single('file'),
85
+ function (req, res) {
86
+ const a = req.file;
87
+ fileCache.set(a.originalname, a.buffer);
88
+ res.json({ url: `${req.url}/${a.originalname}` });
89
+ },
90
+ );
91
+ // 用户资源获取
92
+ app.get('/__develop__/assets/:name', function (req, res) {
93
+ const file = fileCache.get(req.params.name);
94
+ if (!file) return res.status(404);
95
+ res.send(file);
96
+ });
97
+ // 静态资源
98
+ app.get('/static/*', function (req, res) {
99
+ let filepath;
100
+ [cliNodeModules].find((root) => {
101
+ const p = req.url.replace('/static', root);
102
+ if (fs.existsSync(p)) {
103
+ filepath = p;
104
+ return true;
105
+ }
106
+ });
107
+ if (!filepath) return res.status(404).end();
108
+ fs.readFile(filepath, (err, data) => {
109
+ if (err) {
110
+ return res.status(404).end();
111
+ }
112
+ res.send(data);
113
+ });
114
+ });
115
+ },
116
+ },
117
+ };
118
+
119
+ if (webpackConfigExtendFn) {
120
+ return webpackConfigExtendFn(cf, {
121
+ production: false,
122
+ });
123
+ }
124
+
125
+ return cf;
126
+ };
package/launcher/build.js CHANGED
@@ -1,120 +1,122 @@
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
- const { getBuildEntrys } = require('./start');
10
- const os = require('os');
11
-
12
- function canReadAsset(asset) {
13
- return (
14
- /\.(js|css)$/.test(asset) &&
15
- !/service-worker\.js/.test(asset) &&
16
- !/precache-manifest\.[0-9a-f]+\.js/.test(asset)
17
- );
18
- }
19
-
20
- module.exports = (name, option = {}) => {
21
- const { unique = true, removeConsole } = option;
22
- return new Promise(async (resolve, reject) => {
23
- // 检查组件是否存在
24
- // 开始打包
25
- // 打包成功 返回bundle路径
26
- const result = await getBuildEntrys();
27
- const componentInfo = result.find((d) => d.name === name);
28
- const { base } = componentInfo;
29
- const pathSymbol = os.type() === 'Windows_NT' ? "\\" : "/"
30
- const sourcePath = componentInfo.path.slice(
31
- 0,
32
- componentInfo.path.lastIndexOf(pathSymbol),
33
- );
34
- // const sourcePath = path.join(appComponents, name);
35
-
36
- let easyvcid;
37
- if (unique) {
38
- // 生成组件唯一标识
39
- const easyvc = path.join(sourcePath, '.easyvc');
40
- if (fs.existsSync(easyvc)) {
41
- easyvcid = fs.readFileSync(easyvc, { encoding: 'utf8' });
42
- }
43
- if (!easyvcid) {
44
- easyvcid = Array.from({ length: 6 })
45
- .map(() => Math.random().toString(32).slice(2, 3))
46
- .join('');
47
- fs.writeFileSync(easyvc, easyvcid, { encoding: 'utf8' });
48
- }
49
- }
50
-
51
- // 重写base的modulename
52
- // const { base } = checkComponentFiles(sourcePath);
53
- base.filename = `${componentInfo.dirname}`;
54
- base.module_name = `${base.module_name}${unique ? '.' + easyvcid : ''}`;
55
- const compiler = webpack(
56
- getWebpackConfig(base, { uniqueId: easyvcid, removeConsole }),
57
- );
58
-
59
- console.log('Creating an optimized production build...');
60
-
61
- compiler.run((err, stats) => {
62
- const message = stats.toJson({
63
- all: true,
64
- warnings: true,
65
- errors: true,
66
- });
67
-
68
- const errors = err || message.errors;
69
- if (errors.length) {
70
- console.log(chalk.red('Failed to compile.\n'));
71
- console.log(chalk.red(errors + '\n'));
72
- process.exit(1);
73
- }
74
-
75
- if (message.warnings.length) {
76
- console.log(chalk.yellow('Compiled with warnings.\n'));
77
- console.log(message.warnings.join('\n\n'));
78
- } else {
79
- // successful
80
- console.log(chalk.green('Compiled successfully.'));
81
- console.log(`Time: ${message.time / 1000}s`);
82
- console.log(`Built at: ${new Date(message.builtAt)}\n`);
83
- console.log('File sizes:\n');
84
-
85
- message.assets
86
- .filter((asset) => canReadAsset(asset.name))
87
- .map((asset) => {
88
- var fileContents = fs.readFileSync(
89
- path.resolve(appBuild, asset.name),
90
- );
91
- return {
92
- folder: path.join(
93
- path.basename(appBuild),
94
- path.dirname(asset.name),
95
- ),
96
- name: path.basename(asset.name),
97
- size: filesize(fileContents.length),
98
- };
99
- })
100
- .sort((a, b) => b.size - a.size)
101
- .map((asset) => {
102
- console.log(
103
- ' ' +
104
- asset.size +
105
- ' ' +
106
- chalk.dim(asset.folder + path.sep) +
107
- chalk.cyan(asset.name),
108
- );
109
- });
110
-
111
- console.log();
112
- }
113
- resolve({
114
- file: path.join(message.outputPath, message.assetsByChunkName.main),
115
- base,
116
- sourcePath: sourcePath,
117
- });
118
- });
119
- });
120
- };
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
+ const { getBuildEntrys } = require('./start');
10
+ const os = require('os');
11
+
12
+ function canReadAsset(asset) {
13
+ return (
14
+ /\.(js|css)$/.test(asset) &&
15
+ !/service-worker\.js/.test(asset) &&
16
+ !/precache-manifest\.[0-9a-f]+\.js/.test(asset)
17
+ );
18
+ }
19
+
20
+ module.exports = (name, option = {}) => {
21
+ const { unique = true, removeConsole } = option;
22
+ return new Promise(async (resolve, reject) => {
23
+ // 检查组件是否存在
24
+ // 开始打包
25
+ // 打包成功 返回bundle路径
26
+ const result = await getBuildEntrys();
27
+ const componentInfo = result.find((d) => d.name === name);
28
+ const { base } = componentInfo;
29
+ const pathSymbol = os.type() === 'Windows_NT' ? '\\' : '/';
30
+ const sourcePath = componentInfo.path.slice(
31
+ 0,
32
+ componentInfo.path.lastIndexOf(pathSymbol),
33
+ );
34
+ // const sourcePath = path.join(appComponents, name);
35
+
36
+ let easyvcid;
37
+ if (unique) {
38
+ // 生成组件唯一标识
39
+ const easyvc = path.join(sourcePath, '.easyvc');
40
+ if (fs.existsSync(easyvc)) {
41
+ easyvcid = fs.readFileSync(easyvc, { encoding: 'utf8' });
42
+ }
43
+ if (!easyvcid) {
44
+ easyvcid = Array.from({ length: 6 })
45
+ .map(() => Math.random().toString(32).slice(2, 3))
46
+ .join('');
47
+ fs.writeFileSync(easyvc, easyvcid, { encoding: 'utf8' });
48
+ }
49
+ }
50
+
51
+ // 重写base的modulename
52
+ // const { base } = checkComponentFiles(sourcePath);
53
+ base.filename = `${componentInfo.dirname}`;
54
+ base.module_name = `${base.module_name}${unique ? '.' + easyvcid : ''}`;
55
+ const compiler = webpack(
56
+ getWebpackConfig(base, { uniqueId: easyvcid, removeConsole }),
57
+ );
58
+
59
+ console.log('Creating an optimized production build...');
60
+
61
+ compiler.run((err, stats) => {
62
+ const message = stats.toJson({
63
+ all: true,
64
+ warnings: true,
65
+ errors: true,
66
+ });
67
+
68
+ const errors = err || message.errors;
69
+ if (errors.length) {
70
+ console.log(chalk.red('Failed to compile.\n'));
71
+ console.log(chalk.red(errors + '\n'));
72
+ process.exit(1);
73
+ }
74
+
75
+ if (message.warnings.length) {
76
+ console.log(chalk.yellow('Compiled with warnings.\n'));
77
+ console.log(message.warnings.join('\n\n'));
78
+ } else {
79
+ // successful
80
+ console.log(chalk.green('Compiled successfully.'));
81
+ console.log(`Time: ${message.time / 1000}s`);
82
+ console.log(`Built at: ${new Date(message.builtAt)}\n`);
83
+ console.log('File sizes:\n');
84
+
85
+ message.assets
86
+ .filter((asset) => canReadAsset(asset.name))
87
+ .map((asset) => {
88
+ var fileContents = fs.readFileSync(
89
+ path.resolve(appBuild, asset.name),
90
+ );
91
+ return {
92
+ folder: path.join(
93
+ path.basename(appBuild),
94
+ path.dirname(asset.name),
95
+ ),
96
+ name: path.basename(asset.name),
97
+ size: filesize(fileContents.length),
98
+ };
99
+ })
100
+ .sort((a, b) => b.size - a.size)
101
+ .map((asset) => {
102
+ console.log(
103
+ ' ' +
104
+ asset.size +
105
+ ' ' +
106
+ chalk.dim(asset.folder + path.sep) +
107
+ chalk.cyan(asset.name),
108
+ );
109
+ });
110
+
111
+ console.log();
112
+ }
113
+ resolve({
114
+ file: path.join(message.outputPath, message.assetsByChunkName.main),
115
+ base,
116
+ sourcePath: sourcePath,
117
+ });
118
+ });
119
+ });
120
+ };
121
+
122
+ module.exports.getBuildEntrys = getBuildEntrys;