-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
83 lines (73 loc) · 1.89 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs')
const path = require('path')
const express = require('express')
const webpack = require('webpack')
const app = express()
// cached pages
const cached = {}
app.get('/', (req, res) => {
if (!cached[req.path]) {
cached[req.path] = fs.readFileSync(path.join(__dirname, './webapp/index.html'))
}
res.write(cached[req.path])
res.end()
})
const genMainScript = () => new Promise((resolve, reject) => {
const filename = 'main.js'
webpack({
entry: path.join(__dirname, './webapp/main.js'),
output: {
path: path.join(__dirname, './dist/'),
filename
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
},
plugins: [
// short-circuits all Vue.js warning code
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
})
],
resolve: {
alias: {
'map-state-vmodel': path.join(__dirname, process.env.NODE_ENV === 'production' ? './dist/index.min.js' : './src/index.js')
}
}
}, (err, stats) => {
const e = err || stats.hasErrors()
if (err) return reject(err)
if (stats.hasErrors()) {
const errors = stats.toJson('minimal').errors
for (let e of errors) {
console.log(e.toString())
}
return reject()
}
const outputFile = path.join(__dirname, `./dist/${filename}`)
resolve(fs.readFileSync(outputFile))
// fs.unlinkSync(outputFile)
})
})
app.get('/static/main.js', async (req, res) => {
if (!cached[req.path]) {
cached[req.path] = await genMainScript()
}
res.setHeader('content-type', 'text/javascript')
res.write(cached[req.path])
res.end()
})
const port = '3000'
app.listen(port, () => console.log(`Listen on: ${port}`))