Skip to content

Commit 4c08ffc

Browse files
committed
feat: 增加预览功能
1 parent 8953f27 commit 4c08ffc

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

build/index.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
'use strict';
22

3-
const { sh, cli } = require('tasksfile');
3+
const {sh, cli} = require('tasksfile');
44
const chalk = require('chalk');
55
const rawArgv = process.argv.slice(2);
66
const args = rawArgv.join(' ');
7+
const config = require('../vue.config.js');
78

89
// 便于捕捉 build 之后的错误,然后进行自定义处理
910
// 配合 jenkins 执行 job
1011
function command() {
1112
sh(`vue-cli-service build ${args}`, {
1213
async: true,
13-
stdio: 'inherit',
14+
stdio: 'inherit'
1415
})
1516
.then((output) => {
1617
console.log(chalk.cyan(output || ''));
@@ -25,4 +26,43 @@ function command() {
2526
});
2627
}
2728

28-
command();
29+
if (process.env.npm_config_preview || rawArgv.includes('--preview')) {
30+
const report = rawArgv.includes('--report');
31+
32+
sh(`vue-cli-service build ${args}`, {
33+
async: true,
34+
stdio: 'inherit'
35+
})
36+
.then((output) => {
37+
const port = 9526;
38+
const publicPath = config.publicPath;
39+
40+
const connect = require('connect');
41+
const serveStatic = require('serve-static');
42+
const app = connect();
43+
44+
app.use(
45+
publicPath,
46+
serveStatic('./dist', {
47+
index: ['index.html', '/']
48+
})
49+
);
50+
51+
app.listen(port, function () {
52+
console.log(chalk.green(`> Preview at http://localhost:${port}${publicPath}`));
53+
if (report) {
54+
console.log(chalk.green(`> Report at http://localhost:${port}${publicPath}report.html`));
55+
}
56+
});
57+
}).catch((err) => {
58+
console.error('\n');
59+
console.error(chalk.magenta('编译打包出错了 ~~~~(>_<)~~~~ \n'));
60+
console.error(chalk.magenta('具体错误信息如下 \n'));
61+
console.error(chalk.red(`${err}.\n`));
62+
console.log(chalk.red(' Build failed with errors.\n'));
63+
process.exit(1);
64+
});
65+
} else {
66+
command();
67+
}
68+

package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"scripts": {
88
"serve": "vue-cli-service serve",
99
"build": "vue-cli-service build",
10+
"build:stage": "vue-cli-service build --mode staging",
11+
"preview": "node build/index.ts --preview",
1012
"lint": "vue-cli-service lint",
1113
"deploy": "npm run build && node build/zip.ts",
1214
"analyz": "vue-cli-service build --mode analyz",
@@ -70,6 +72,7 @@
7072
"@types/node": "^10.12.18",
7173
"@types/nprogress": "^0.0.29",
7274
"@types/screenfull": "^3.3.2",
75+
"@types/serve-static": "^1.13.3",
7376
"@types/sortablejs": "^1.7.2",
7477
"@types/store": "^2.0.1",
7578
"@types/webpack-env": "^1.13.6",
@@ -93,6 +96,7 @@
9396
"progress-bar-webpack-plugin": "^1.12.1",
9497
"script-ext-html-webpack-plugin": "^2.1.3",
9598
"script-loader": "^0.7.2",
99+
"serve-static": "^1.14.1",
96100
"style-resources-loader": "^1.2.1",
97101
"typescript": "^3.2.2",
98102
"vue-cli-plugin-element": "^1.0.1",

0 commit comments

Comments
 (0)