@autobest-ui/scripts 0.0.5 → 0.1.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/README.md CHANGED
@@ -1 +1,120 @@
1
- # @autobest-ui/scripts
1
+ # @autobest-ui/scripts
2
+
3
+ ## 介绍
4
+ 集成react项目在webpack下debug、build, 统一命令,方便维护与升级。
5
+
6
+ ## 安装
7
+ `npm install @autobest-ui/scripts --save-dev`
8
+
9
+ ## 使用方式
10
+
11
+ #### 单页面应用(SPA)
12
+ 1. 本地 hot debug: `react-spa start`
13
+
14
+ 2. build: `react-spa build`
15
+
16
+ #### 服务端渲染应用(SSR)
17
+ 1. 本地 client hot debug: `react-ssr start`
18
+
19
+ 2. 本地 server hot debug: `react-ssr server`
20
+
21
+ 3. build: `react-ssr build`
22
+
23
+ ## 配置文件
24
+ 根目录下创建`config`文件夹
25
+
26
+ ```
27
+ config
28
+ app.config.js // 为命令提供的私有配置
29
+ app.webpack.js // 扩展webpack配置, 非必须选
30
+ env.js // 项目私有配置(比如: API BaseUrl配置),这个名字可以自定义
31
+ ```
32
+
33
+ #### app.webpack.js 介绍
34
+
35
+ 该文件用于webpack扩展, 非必写。写法如下:
36
+
37
+ ```
38
+ module.exports = function(
39
+ result,
40
+ appConfig,
41
+ extendEnv,
42
+ { isProd, isServer, isHotServer, isSetCommonAlias }
43
+ ) {
44
+ // 根据条件,对不同环境的配置进行扩展
45
+ ......
46
+ // 最终返回扩展后的结果
47
+ return result;
48
+ }
49
+ ```
50
+
51
+ #### app.config.js 介绍
52
+
53
+ ##### start 配置参数说明(作用于`react-spa start` 或者 `react-ssr start`)
54
+ | 参数名 | 说明 |
55
+ | ---- | ---- |
56
+ | startEntry | start入口文件 |
57
+ | startDefaultPort | start默认端口号, 可自定义,下面介绍 |
58
+ | startOpenPath | start 打开浏览器的路径 |
59
+ | startOutput.publicPath | start加载资源的路径 |
60
+ | startOutput.path | start 资源根路径 |
61
+ | getStartEnvConfig | start 自定义环境变量的方法,会追加到process.env中 |
62
+
63
+ ##### hot ssr 配置参数说明(作用于`react-ssr ssr`)
64
+ | 参数名 | 说明 |
65
+ | ---- | ---- |
66
+ | hotSsrClientEntry | hot ssr 客户端入口文件 |
67
+ | hotSsrServerEntry | hot ssr 服务端入口文件 |
68
+ | hotSsrDefaultPort | hot ssr默认端口号, 可自定义,下面介绍 |
69
+ | hotSsrOpenPath | hot ssr 打开浏览器的路径 |
70
+ | hotSsrRunPath | hot ssr 执行nodejs 入口文件路径 |
71
+ | hotSsrWatch | hot ssr 需要watch的文件夹,类型: Array |
72
+ | hotSsrClientOutput.publicPath | hot ssr客户端加载资源的路径 |
73
+ | hotSsrClientOutput.path | hot ssr 客户端资源根路径 |
74
+ | hotSsrServerOutput.publicPath | hot ssr 服务端加载资源的路径 |
75
+ | hotSsrServerOutput.path | hot ssr 服务端资源根路径 |
76
+ | getHotSsrEnvConfig | hot ssr 自定义环境变量的方法,会追加到process.env中 |
77
+
78
+ ##### hot ssr 配置参数说明(作用于`react-ssr build`)
79
+ | 参数名 | 说明 |
80
+ | ---- | ---- |
81
+ | buildClientEntry | build ssr 客户端入口文件 |
82
+ | buildServerEntry | build ssr 客户端入口文件 |
83
+ | buildClientOutput.publicPath | build ssr客户端加载资源的路径 |
84
+ | buildClientOutput.path | build ssr 客户端资源根路径 |
85
+ | buildServerOutput.publicPath | build ssr 服务端加载资源的路径 |
86
+ | buildServerOutput.path | build ssr 服务端资源根路径 |
87
+ | getBuildEnvConfig | build ssr 自定义环境变量的方法,会追加到process.env中 |
88
+ | buildClientLoadableConfig.filename | build ssr 自定义按需加载路径存储库文件名 |
89
+ | buildClientLoadableConfig.writeToDiskFilename | build ssr 自定义按需加载路径存储库文件存放位置 |
90
+
91
+ ##### 公共配置
92
+ | 参数名 | 说明 |
93
+ | ---- | ---- |
94
+ | htmlPluginTemplate | client端,模板html路径 |
95
+ | alias | debug, build 下文件别名,同webpack.resolve.alias一样 |
96
+
97
+ ## 自定义
98
+
99
+ #### 自定义端口
100
+
101
+ ##### react-spa 自定义方式
102
+ `react-spa start --port 3031` 或者已经在package.json.scripts中定义后(`{start: "react-spa start"}`), 将需要这样写 `npm run start -- --port 3031`
103
+
104
+ ##### react-ssr 自定义方式
105
+ 1. `react-ssr start --port 3031` 或者已经在package.json.scripts中定义后(`{start: "react-ssr start"}`), 将需要这样写 `npm run start -- --port 3031`
106
+ 1. `react-ssr server --port 3031` 或者已经在package.json.scripts中定义后(`{server: "react-ssr server"}`), 将需要这样写 `npm run server -- --port 3031`
107
+
108
+
109
+ #### 自定义其他参数
110
+ **例如:**
111
+ `react-spa start --site APW`
112
+ `react-ssr start --port 3032 --site APW`
113
+
114
+ 该参数将解析成对象,通过get**EnvConfig()参数传递,此方式,可区分自定义的环境变量,例如:www2.0项目中site的区分。
115
+
116
+
117
+ ### 自定义环境变量
118
+ 该方式,主要应用于启动程序前,自定义环境变量,主要用于get**EnvConfig()方法的多样性。
119
+
120
+ 例如: `cross-env REACT_APP_ENV=local react-ssr start`
package/bin/spa.js CHANGED
@@ -1,44 +1,44 @@
1
- #!/usr/bin/env node
2
-
3
- process.on('unhandledRejection', err => {
4
- // eslint-disable-next-line @typescript-eslint/no-throw-literal
5
- throw err;
6
- });
7
-
8
- const spawn = require('cross-spawn');
9
-
10
- const args = process.argv.slice(2);
11
-
12
- const scriptIndex = args.findIndex(x => x === 'build' || x === 'start');
13
- const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
14
- const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
15
-
16
- if (['build', 'start'].includes(script)) {
17
- const result = spawn.sync(
18
- process.execPath,
19
- nodeArgs.concat(require.resolve(`../spaConfig/${script}`)).concat(args.slice(scriptIndex + 1)),
20
- { stdio: 'inherit' }
21
- );
22
- if (result.signal) {
23
- if (result.signal === 'SIGKILL') {
24
- // eslint-disable-next-line no-console
25
- console.log(
26
- 'The build failed because the process exited too early. ' +
27
- 'This probably means the system ran out of memory or someone called ' +
28
- '`kill -9` on the process.'
29
- );
30
- } else if (result.signal === 'SIGTERM') {
31
- // eslint-disable-next-line no-console
32
- console.log(
33
- 'The build failed because the process exited too early. ' +
34
- 'Someone might have called `kill` or `killall`, or the system could ' +
35
- 'be shutting down.'
36
- );
37
- }
38
- process.exit(1);
39
- }
40
- process.exit(result.status);
41
- } else {
42
- // eslint-disable-next-line no-console
43
- console.log(`Unknown script "${script}".`);
44
- }
1
+ #!/usr/bin/env node
2
+
3
+ process.on('unhandledRejection', err => {
4
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
5
+ throw err;
6
+ });
7
+
8
+ const spawn = require('cross-spawn');
9
+
10
+ const args = process.argv.slice(2);
11
+
12
+ const scriptIndex = args.findIndex(x => x === 'build' || x === 'start');
13
+ const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
14
+ const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
15
+
16
+ if (['build', 'start'].includes(script)) {
17
+ const result = spawn.sync(
18
+ process.execPath,
19
+ nodeArgs.concat(require.resolve(`../spaConfig/${script}`)).concat(args.slice(scriptIndex + 1)),
20
+ { stdio: 'inherit' }
21
+ );
22
+ if (result.signal) {
23
+ if (result.signal === 'SIGKILL') {
24
+ // eslint-disable-next-line no-console
25
+ console.log(
26
+ 'The build failed because the process exited too early. ' +
27
+ 'This probably means the system ran out of memory or someone called ' +
28
+ '`kill -9` on the process.'
29
+ );
30
+ } else if (result.signal === 'SIGTERM') {
31
+ // eslint-disable-next-line no-console
32
+ console.log(
33
+ 'The build failed because the process exited too early. ' +
34
+ 'Someone might have called `kill` or `killall`, or the system could ' +
35
+ 'be shutting down.'
36
+ );
37
+ }
38
+ process.exit(1);
39
+ }
40
+ process.exit(result.status);
41
+ } else {
42
+ // eslint-disable-next-line no-console
43
+ console.log(`Unknown script "${script}".`);
44
+ }
package/bin/ssr.js CHANGED
@@ -1,44 +1,44 @@
1
- #!/usr/bin/env node
2
-
3
- process.on('unhandledRejection', err => {
4
- // eslint-disable-next-line @typescript-eslint/no-throw-literal
5
- throw err;
6
- });
7
-
8
- const spawn = require('cross-spawn');
9
-
10
- const args = process.argv.slice(2);
11
-
12
- const scriptIndex = args.findIndex(x => x === 'build' || x === 'start' || 'server');
13
- const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
14
- const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
15
-
16
- if (['build', 'start', 'server'].includes(script)) {
17
- const result = spawn.sync(
18
- process.execPath,
19
- nodeArgs.concat(require.resolve(`../ssrConfig/${script}`)).concat(args.slice(scriptIndex + 1)),
20
- { stdio: 'inherit' }
21
- );
22
- if (result.signal) {
23
- if (result.signal === 'SIGKILL') {
24
- // eslint-disable-next-line no-console
25
- console.log(
26
- 'The build failed because the process exited too early. ' +
27
- 'This probably means the system ran out of memory or someone called ' +
28
- '`kill -9` on the process.'
29
- );
30
- } else if (result.signal === 'SIGTERM') {
31
- // eslint-disable-next-line no-console
32
- console.log(
33
- 'The build failed because the process exited too early. ' +
34
- 'Someone might have called `kill` or `killall`, or the system could ' +
35
- 'be shutting down.'
36
- );
37
- }
38
- process.exit(1);
39
- }
40
- process.exit(result.status);
41
- } else {
42
- // eslint-disable-next-line no-console
43
- console.log(`Unknown script "${script}".`);
44
- }
1
+ #!/usr/bin/env node
2
+
3
+ process.on('unhandledRejection', err => {
4
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
5
+ throw err;
6
+ });
7
+
8
+ const spawn = require('cross-spawn');
9
+
10
+ const args = process.argv.slice(2);
11
+
12
+ const scriptIndex = args.findIndex(x => x === 'build' || x === 'start' || 'server');
13
+ const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
14
+ const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
15
+
16
+ if (['build', 'start', 'server'].includes(script)) {
17
+ const result = spawn.sync(
18
+ process.execPath,
19
+ nodeArgs.concat(require.resolve(`../ssrConfig/${script}`)).concat(args.slice(scriptIndex + 1)),
20
+ { stdio: 'inherit' }
21
+ );
22
+ if (result.signal) {
23
+ if (result.signal === 'SIGKILL') {
24
+ // eslint-disable-next-line no-console
25
+ console.log(
26
+ 'The build failed because the process exited too early. ' +
27
+ 'This probably means the system ran out of memory or someone called ' +
28
+ '`kill -9` on the process.'
29
+ );
30
+ } else if (result.signal === 'SIGTERM') {
31
+ // eslint-disable-next-line no-console
32
+ console.log(
33
+ 'The build failed because the process exited too early. ' +
34
+ 'Someone might have called `kill` or `killall`, or the system could ' +
35
+ 'be shutting down.'
36
+ );
37
+ }
38
+ process.exit(1);
39
+ }
40
+ process.exit(result.status);
41
+ } else {
42
+ // eslint-disable-next-line no-console
43
+ console.log(`Unknown script "${script}".`);
44
+ }
@@ -1,11 +1,11 @@
1
- const { getProjectPath } = require('./utils');
2
-
3
- const configPath = getProjectPath('/config/app.config.js');
4
-
5
- const config = require(configPath);
6
-
7
- module.exports = {
8
- startDefaultPort: 3030,
9
- startEntry: '/src/index.tsx',
10
- ...config
11
- };
1
+ const { getProjectPath } = require('./utils');
2
+
3
+ const configPath = getProjectPath('/config/app.config.js');
4
+
5
+ const config = require(configPath);
6
+
7
+ module.exports = {
8
+ startDefaultPort: 3030,
9
+ startEntry: '/src/index.tsx',
10
+ ...config
11
+ };
package/common/build.js CHANGED
@@ -1,40 +1,40 @@
1
- const formatWebpackMessages = require('./formatWebpackMessages');
2
-
3
- function runCallback(resolve, reject) {
4
- return (err, stats) => {
5
- let messages;
6
- if (err) {
7
- if (!err.message) {
8
- return reject(err);
9
- }
10
- let errMessage = err.message;
11
- if (Object.prototype.hasOwnProperty.call(err, 'postcssNode')) {
12
- errMessage += `\nCompileError: Begins at CSS selector ${err.postcssNode.selector}`;
13
- }
14
-
15
- messages = formatWebpackMessages({
16
- errors: [errMessage],
17
- warnings: []
18
- });
19
- } else {
20
- messages = formatWebpackMessages(stats.toJson({ all: false, warnings: true, errors: true }));
21
- }
22
- if (messages.errors.length) {
23
- if (messages.errors.length > 1) {
24
- messages.errors.length = 1;
25
- }
26
- return reject(new Error(messages.errors.join('\n\n')));
27
- }
28
-
29
- const resolveArgs = {
30
- stats,
31
- previousFileSizes: 1,
32
- warnings: messages.warnings
33
- };
34
- return resolve(resolveArgs);
35
- };
36
- }
37
-
38
- module.exports = {
39
- runCallback
40
- };
1
+ const formatWebpackMessages = require('./formatWebpackMessages');
2
+
3
+ function runCallback(resolve, reject) {
4
+ return (err, stats) => {
5
+ let messages;
6
+ if (err) {
7
+ if (!err.message) {
8
+ return reject(err);
9
+ }
10
+ let errMessage = err.message;
11
+ if (Object.prototype.hasOwnProperty.call(err, 'postcssNode')) {
12
+ errMessage += `\nCompileError: Begins at CSS selector ${err.postcssNode.selector}`;
13
+ }
14
+
15
+ messages = formatWebpackMessages({
16
+ errors: [errMessage],
17
+ warnings: []
18
+ });
19
+ } else {
20
+ messages = formatWebpackMessages(stats.toJson({ all: false, warnings: true, errors: true }));
21
+ }
22
+ if (messages.errors.length) {
23
+ if (messages.errors.length > 1) {
24
+ messages.errors.length = 1;
25
+ }
26
+ return reject(new Error(messages.errors.join('\n\n')));
27
+ }
28
+
29
+ const resolveArgs = {
30
+ stats,
31
+ previousFileSizes: 1,
32
+ warnings: messages.warnings
33
+ };
34
+ return resolve(resolveArgs);
35
+ };
36
+ }
37
+
38
+ module.exports = {
39
+ runCallback
40
+ };
@@ -1,48 +1,49 @@
1
- const getCurrentVersion = require('./version');
2
- const appConfig = require('./appConfig');
3
-
4
- const CurrentVersion = getCurrentVersion();
5
-
6
- function extendEnv(isHotSsrEnv) {
7
- const result = {
8
- REACT_APP_VERSION: CurrentVersion
9
- };
10
-
11
- if (process.env.NODE_ENV === 'production') {
12
- return { ...result, ...appConfig.getBuildEnvConfig() };
13
- }
14
-
15
- if (isHotSsrEnv) {
16
- return { ...result, ...appConfig.getHotSsrEnvConfig() };
17
- }
18
-
19
- return { ...result, ...appConfig.getStartEnvConfig() };
20
- }
21
-
22
- const REACT_APP = /^REACT_APP_/i;
23
-
24
- function getEnvironmentInfo(isHotSsrEnv) {
25
- const raw = Object.keys(process.env)
26
- .filter(key => REACT_APP.test(key))
27
- .reduce(
28
- (env, key) => {
29
- env[key] = process.env[key];
30
- return env;
31
- },
32
- {
33
- NODE_ENV: process.env.NODE_ENV || 'development',
34
- BABEL_ENV: process.env.BABEL_ENV || 'development',
35
- ...extendEnv(isHotSsrEnv)
36
- }
37
- );
38
- // DefinePlugin
39
- const stringified = {
40
- 'process.env': Object.keys(raw).reduce((env, key) => {
41
- env[key] = JSON.stringify(raw[key]);
42
- return env;
43
- }, {})
44
- };
45
- return { raw, stringified, version: CurrentVersion };
46
- }
47
-
48
- module.exports = getEnvironmentInfo;
1
+ const getCurrentVersion = require('./version');
2
+ const { parseArgsToJson } = require('./utils');
3
+ const appConfig = require('./appConfig');
4
+
5
+ const CurrentVersion = getCurrentVersion();
6
+
7
+ function extendEnv(isHotSsrEnv) {
8
+ const result = {
9
+ REACT_APP_VERSION: CurrentVersion
10
+ };
11
+
12
+ if (process.env.NODE_ENV === 'production') {
13
+ return { ...result, ...appConfig.getBuildEnvConfig(parseArgsToJson) };
14
+ }
15
+
16
+ if (isHotSsrEnv) {
17
+ return { ...result, ...appConfig.getHotSsrEnvConfig(parseArgsToJson) };
18
+ }
19
+
20
+ return { ...result, ...appConfig.getStartEnvConfig(parseArgsToJson) };
21
+ }
22
+
23
+ const REACT_APP = /^REACT_APP_/i;
24
+
25
+ function getEnvironmentInfo(isHotSsrEnv) {
26
+ const raw = Object.keys(process.env)
27
+ .filter(key => REACT_APP.test(key))
28
+ .reduce(
29
+ (env, key) => {
30
+ env[key] = process.env[key];
31
+ return env;
32
+ },
33
+ {
34
+ NODE_ENV: process.env.NODE_ENV || 'development',
35
+ BABEL_ENV: process.env.BABEL_ENV || 'development',
36
+ ...extendEnv(isHotSsrEnv)
37
+ }
38
+ );
39
+ // DefinePlugin
40
+ const stringified = {
41
+ 'process.env': Object.keys(raw).reduce((env, key) => {
42
+ env[key] = JSON.stringify(raw[key]);
43
+ return env;
44
+ }, {})
45
+ };
46
+ return { raw, stringified, version: CurrentVersion };
47
+ }
48
+
49
+ module.exports = getEnvironmentInfo;