Skip to content

Commit e4481a9

Browse files
committed
fix[build.js]: fixed build bug in preview mode PanJiaChen#819
1 parent 572a2d9 commit e4481a9

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

build/build.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const chalk = require('chalk')
88
const webpack = require('webpack')
99
const config = require('../config')
1010
const webpackConfig = require('./webpack.prod.conf')
11-
const server = require('pushstate-server')
11+
var connect = require('connect');
12+
var serveStatic = require('serve-static')
1213

13-
var spinner = ora('building for '+ process.env.env_config+ ' environment...' )
14+
const spinner = ora('building for ' + process.env.env_config + ' environment...')
1415
spinner.start()
1516

1617
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
@@ -27,22 +28,29 @@ rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
2728
}) + '\n\n')
2829

2930
if (stats.hasErrors()) {
30-
console.log(chalk.red(' Build failed with errors.\n'))
31+
console.log(chalk.red(' Build failed with errors.\n'))
3132
process.exit(1)
3233
}
3334

34-
console.log(chalk.cyan(' Build complete.\n'))
35+
console.log(chalk.cyan(' Build complete.\n'))
3536
console.log(chalk.yellow(
36-
' Tip: built files are meant to be served over an HTTP server.\n' +
37-
' Opening index.html over file:// won\'t work.\n'
37+
' Tip: built files are meant to be served over an HTTP server.\n' +
38+
' Opening index.html over file:// won\'t work.\n'
3839
))
39-
if(process.env.npm_config_preview){
40-
server.start({
41-
port: 9526,
42-
directory: './dist',
43-
file: '/index.html'
40+
41+
if (process.env.npm_config_preview) {
42+
const port = 9526
43+
const host = "http://localhost:" + port
44+
const basePath = config.build.assetsPublicPath
45+
const app = connect()
46+
47+
app.use(basePath, serveStatic('./dist', {
48+
'index': ['index.html', '/']
49+
}))
50+
51+
app.listen(port, function () {
52+
console.log(chalk.green(`> Listening at http://localhost:${port}${basePath}`))
4453
});
45-
console.log('> Listening at ' + 'http://localhost:9526' + '\n')
4654
}
4755
})
4856
})

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"axios": "0.17.1",
3030
"clipboard": "1.7.1",
3131
"codemirror": "5.32.0",
32+
"connect": "3.6.6",
3233
"driver.js": "0.5.2",
3334
"dropzone": "5.2.0",
3435
"echarts": "3.8.5",
@@ -42,6 +43,7 @@
4243
"normalize.css": "7.0.0",
4344
"nprogress": "0.2.0",
4445
"screenfull": "3.3.2",
46+
"serve-static": "1.13.2",
4547
"showdown": "1.8.5",
4648
"simplemde": "1.11.2",
4749
"sortablejs": "1.7.0",
@@ -87,7 +89,6 @@
8789
"postcss-import": "11.0.0",
8890
"postcss-loader": "2.0.9",
8991
"postcss-url": "7.3.0",
90-
"pushstate-server": "3.0.1",
9192
"rimraf": "2.6.2",
9293
"sass-loader": "6.0.6",
9394
"script-loader": "0.7.2",

0 commit comments

Comments
 (0)