@autobest-ui/scripts 3.0.0-alpha.2 → 3.0.0-alpha.4
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/apiConfig/build.js +66 -0
- package/apiConfig/start.js +54 -0
- package/apiConfig/webpack.prod.api.js +38 -0
- package/bin/api.js +44 -0
- package/common/appConfig.js +6 -1
- package/package.json +4 -2
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const webpack = require('webpack');
|
|
2
|
+
const chalk = require('chalk');
|
|
3
|
+
const appConfig = require('../common/appConfig');
|
|
4
|
+
const { runCallback } = require('../common/build');
|
|
5
|
+
const getEnvironmentInfo = require('../common/extendEnv');
|
|
6
|
+
const extendWebpackConfigFromProject = require('../common/extendWebpackConfigFromProject');
|
|
7
|
+
const apiConfigFactory = require('./webpack.prod.api');
|
|
8
|
+
|
|
9
|
+
process.env.BABEL_ENV = 'production';
|
|
10
|
+
process.env.NODE_ENV = 'production';
|
|
11
|
+
|
|
12
|
+
process.on('unhandledRejection', err => {
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
14
|
+
throw err;
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const extendEnv = getEnvironmentInfo();
|
|
18
|
+
|
|
19
|
+
function build() {
|
|
20
|
+
const apiConfig = extendWebpackConfigFromProject(apiConfigFactory, appConfig, extendEnv, {
|
|
21
|
+
isServer: true,
|
|
22
|
+
isHotServer: false,
|
|
23
|
+
isProd: true
|
|
24
|
+
});
|
|
25
|
+
// eslint-disable-next-line no-console
|
|
26
|
+
console.log(chalk.green('Creating an optimized production build...'));
|
|
27
|
+
const apiCompiler = webpack(apiConfig);
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
apiCompiler.run(runCallback(resolve, reject));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
build()
|
|
34
|
+
.then(
|
|
35
|
+
([clientInfo, serverInfo]) => {
|
|
36
|
+
if (clientInfo.warnings.length || serverInfo.warnings.length) {
|
|
37
|
+
// eslint-disable-next-line no-console
|
|
38
|
+
console.log(chalk.yellow('Compiled with warnings.\n'));
|
|
39
|
+
// eslint-disable-next-line no-console
|
|
40
|
+
console.log(clientInfo.warnings.join('\n\n'));
|
|
41
|
+
// eslint-disable-next-line no-console
|
|
42
|
+
console.log(serverInfo.warnings.join('\n\n'));
|
|
43
|
+
// eslint-disable-next-line no-console
|
|
44
|
+
console.log(`\nSearch for the ${chalk.underline(chalk.yellow('keywords'))} to learn more about each warning.`);
|
|
45
|
+
// eslint-disable-next-line no-console
|
|
46
|
+
console.log(`To ignore, add ${chalk.cyan('// eslint-disable-next-line')} to the line before.\n`);
|
|
47
|
+
} else {
|
|
48
|
+
// eslint-disable-next-line no-console
|
|
49
|
+
console.log(chalk.green('Compiled successfully.\n'));
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
err => {
|
|
53
|
+
// eslint-disable-next-line no-console
|
|
54
|
+
console.log(chalk.red('Failed to compile.\n'));
|
|
55
|
+
// eslint-disable-next-line no-console
|
|
56
|
+
console.log(err);
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
.catch(err => {
|
|
61
|
+
// eslint-disable-next-line no-console
|
|
62
|
+
console.log(chalk.red('Failed to compile.\n'));
|
|
63
|
+
// eslint-disable-next-line no-console
|
|
64
|
+
console.log(err);
|
|
65
|
+
process.exit(1);
|
|
66
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const nodemon = require('nodemon');
|
|
2
|
+
const open = require('open');
|
|
3
|
+
const getEnvironmentInfo = require('../common/extendEnv');
|
|
4
|
+
const appConfig = require('../common/appConfig');
|
|
5
|
+
const { getProjectPath, parseArgsToJson } = require('../common/utils');
|
|
6
|
+
|
|
7
|
+
const extendEnv = getEnvironmentInfo();
|
|
8
|
+
|
|
9
|
+
const PORT = process.env.PORT || parseArgsToJson.port || appConfig.startDefaultPort;
|
|
10
|
+
const startOpenPath = appConfig.startOpenPath ? appConfig.startOpenPath : '';
|
|
11
|
+
|
|
12
|
+
process.env.BABEL_ENV = 'development';
|
|
13
|
+
process.env.NODE_ENV = 'development';
|
|
14
|
+
|
|
15
|
+
const options = {
|
|
16
|
+
// 配置文件
|
|
17
|
+
script: getProjectPath(appConfig.apiEntry),
|
|
18
|
+
restartable: 're',
|
|
19
|
+
verbose: true,
|
|
20
|
+
delay: 1000,
|
|
21
|
+
watch: appConfig.hotApiWatch,
|
|
22
|
+
env: {
|
|
23
|
+
PORT: PORT,
|
|
24
|
+
BABEL_ENV: 'development',
|
|
25
|
+
NODE_ENV: 'development',
|
|
26
|
+
...extendEnv.raw
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
let isOpened = false;
|
|
31
|
+
|
|
32
|
+
nodemon(options)
|
|
33
|
+
.on('start', () => {
|
|
34
|
+
// 启动时的事件
|
|
35
|
+
// eslint-disable-next-line no-console
|
|
36
|
+
console.log('Server app has started');
|
|
37
|
+
if (!isOpened) {
|
|
38
|
+
setTimeout(() => {
|
|
39
|
+
open(`http://localhost:${PORT}${startOpenPath}`);
|
|
40
|
+
}, 3000);
|
|
41
|
+
isOpened = true;
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
.on('quit', () => {
|
|
45
|
+
// 退出时的事件
|
|
46
|
+
// eslint-disable-next-line no-console
|
|
47
|
+
console.log('Server app has quit');
|
|
48
|
+
process.exit();
|
|
49
|
+
})
|
|
50
|
+
.on('restart', () => {
|
|
51
|
+
// 重启时的事件
|
|
52
|
+
// eslint-disable-next-line no-console
|
|
53
|
+
console.log('Server app has restarted');
|
|
54
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const { merge } = require('webpack-merge');
|
|
2
|
+
const { getProjectPath, deleteFolders } = require('../common/utils');
|
|
3
|
+
const webpackCommon = require('../common/webpack.common');
|
|
4
|
+
|
|
5
|
+
module.exports = function (appConfig, extendEnv, { isServer, isHotServer, isProd }) {
|
|
6
|
+
const result = merge(webpackCommon(appConfig, extendEnv, { isProd, isServer, isHotServer }), {
|
|
7
|
+
target: 'node',
|
|
8
|
+
entry: getProjectPath(appConfig.apiEntry),
|
|
9
|
+
output: {
|
|
10
|
+
path: getProjectPath(appConfig.apiOutputPath),
|
|
11
|
+
publicPath: '/',
|
|
12
|
+
filename: 'index.js',
|
|
13
|
+
libraryTarget: 'commonjs2',
|
|
14
|
+
chunkFilename: '[name].[chunkhash].js'
|
|
15
|
+
},
|
|
16
|
+
module: {
|
|
17
|
+
rules: [
|
|
18
|
+
{
|
|
19
|
+
oneOf: [
|
|
20
|
+
{
|
|
21
|
+
test: /\.(scss|css|sass)$/,
|
|
22
|
+
use: [
|
|
23
|
+
{
|
|
24
|
+
loader: require.resolve('ignore-loader')
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const buildServerOutput = getProjectPath(result.output.path);
|
|
35
|
+
deleteFolders(buildServerOutput);
|
|
36
|
+
|
|
37
|
+
return result;
|
|
38
|
+
};
|
package/bin/api.js
ADDED
|
@@ -0,0 +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(`../apiConfig/${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/common/appConfig.js
CHANGED
|
@@ -58,7 +58,12 @@ const defaultConfig = {
|
|
|
58
58
|
// 静态资源css基础文件夹
|
|
59
59
|
baseStaticCssPath: 'css',
|
|
60
60
|
// 静态资源js基础文件夹
|
|
61
|
-
baseStaticJsPath: 'js'
|
|
61
|
+
baseStaticJsPath: 'js',
|
|
62
|
+
|
|
63
|
+
// API 配置
|
|
64
|
+
apiEntry: '/src/index.ts',
|
|
65
|
+
apiOutputPath: '/build',
|
|
66
|
+
hotApiWatch: ['src']
|
|
62
67
|
};
|
|
63
68
|
|
|
64
69
|
module.exports = {
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autobest-ui/scripts",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "utils for project tools",
|
|
6
6
|
"bin": {
|
|
7
7
|
"react-spa": "./bin/spa.js",
|
|
8
|
-
"react-ssr": "./bin/ssr.js"
|
|
8
|
+
"react-ssr": "./bin/ssr.js",
|
|
9
|
+
"node-api": "./bin/api.js"
|
|
9
10
|
},
|
|
10
11
|
"scripts": {
|
|
11
12
|
"clean": "rimraf lib esm",
|
|
@@ -21,6 +22,7 @@
|
|
|
21
22
|
"bin",
|
|
22
23
|
"spaConfig",
|
|
23
24
|
"ssrConfig",
|
|
25
|
+
"apiConfig",
|
|
24
26
|
"common"
|
|
25
27
|
],
|
|
26
28
|
"keywords": [
|