-
Notifications
You must be signed in to change notification settings - Fork 147
/
dev-server.js
143 lines (127 loc) · 3.73 KB
/
dev-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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const request = require('request')
const Koa = require('koa')
const send = require('koa-send')
const Router = require('koa-router')
const multer = require('koa-multer')
const fs = require('fs')
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const webpackConfig = require('./webpack/config.dev')
const config = require('./config')
const { version } = require('./package.json')
const ejs = require('./scripts/ejs')
require('./scripts/dev-site')
// webpack server ===========================================
new WebpackDevServer(webpack(webpackConfig), {
hot: true,
quiet: false,
// noInfo: true,
stats: {
colors: true,
// children: false,
},
}).listen(config.dev.webpackPort, 'localhost', err => {
if (err) {
console.log(err)
}
})
// dev server ================================================
const app = new Koa()
const router = new Router()
// use devlopment version React
router.get('**/react.production.min.js', async ctx => {
await send(ctx, 'node_modules/react/umd/react.development.js')
})
router.get('**/react-dom.production.min.js', async ctx => {
await send(ctx, 'node_modules/react-dom/umd/react-dom.development.js')
})
router.get('**/jszip.min.js', async ctx => {
await send(ctx, 'node_modules/jszip/dist/jszip.min.js')
})
router.get('**/docsearch.css', async ctx => {
await send(ctx, 'node_modules/docsearch.js/dist/cdn/docsearch.min.css')
})
router.get('**/versions.json', async ctx => {
await send(ctx, 'site/versions.json')
})
router.get('/images/*', async ctx => {
await send(ctx, `site/${ctx.path}`)
})
const upload = multer({})
router.post('/upload/', upload.single('file'), async ctx => {
fs.writeFileSync(ctx.req.file.originalname, ctx.req.file.buffer)
ctx.body = {
success: true,
model: {
id: Date.now().toString(),
name: ctx.req.file.originalname,
},
}
})
router.post('/upload-test', upload.single('file'), async ctx => {
await new Promise(resolve =>
setTimeout(() => {
resolve()
}, 1000)
)
ctx.body = {
success: true,
model: {
id: Date.now().toString(),
name: ctx.req.file.originalname,
},
}
})
// dev code proxy
router.get(config.dev.scriptPath, async (ctx, next) => {
// console.log(ctx.url)
let url = ctx.url.split('/')
url = url[url.length - 1]
if (url.endsWith('.Form') || url.endsWith('.List')) {
await next()
} else {
const options = {
uri: `http://localhost:${config.dev.webpackPort}/${url}`,
method: 'GET',
}
ctx.body = request(options)
}
})
// react-hot-loader proxy
router.get('/*.hot-update.js(on)?', async ctx => {
const options = {
uri: `http://localhost:${config.dev.webpackPort}/${ctx.url}`,
method: 'GET',
}
ctx.set('Access-Control-Allow-Origin', '*')
ctx.body = request(options)
})
router.get('/*', async ctx => {
if (ctx.url === '/') ctx.redirect('/cn/index/')
const reactVersion = ctx.query.v
if (reactVersion) {
ctx.body = await ejs.renderFile('./site/index-v.html', { version: reactVersion })
return
}
const prepath = config.dev.scriptPath.replace('**', version)
const scripts = [
...(config.dev.scripts || []),
...Object.keys(config.webpack.entry).map(s => prepath.replace('*.*', `${s}.js`)),
'__css_hot_loader.js',
]
const styles = config.dev.styles || []
ctx.type = 'text/html; charest=utf-8'
ctx.body = await ejs.renderFile(`./site/index.html`, {
scripts,
env: 'development',
appName: config.appName,
styles,
description: '',
})
})
if (config.proxy) config.proxy(router)
app.use(router.routes())
app.listen(config.dev.publishPort, () => {
const ps = config.dev.publishPort === 80 ? '' : `:${config.dev.publishPort}`
console.log(`server running on http://localhost${ps}`)
})