1 | const path = require('path')
|
2 | const os = require('os')
|
3 | const tiresias = require('tiresias')
|
4 | const tiresiasWebpack = require('tiresias-webpack')
|
5 | const opn = require('opn')
|
6 |
|
7 | const webrootDir = path.join(os.tmpdir(), './webroot')
|
8 | const testBuildRootDir = path.join(__dirname, '../test')
|
9 |
|
10 | const defaultConfig = {}
|
11 | defaultConfig.port = 9999
|
12 | defaultConfig.rootDir = testBuildRootDir
|
13 | defaultConfig.distDir = webrootDir
|
14 |
|
15 | var firstBuildEnd = false
|
16 |
|
17 | function serve (port) {
|
18 | var serverConfig = {}
|
19 | serverConfig.rootDir = webrootDir
|
20 |
|
21 | tiresias.setConfig(serverConfig)
|
22 | tiresias.simpleStart(port)
|
23 | }
|
24 |
|
25 | function compileAndServe (customConfig = defaultConfig) {
|
26 | var buildConfig = {}
|
27 | buildConfig.rootDir = defaultConfig.rootDir
|
28 | buildConfig.distDir = defaultConfig.distDir
|
29 |
|
30 | buildConfig = Object.assign({}, buildConfig, customConfig)
|
31 |
|
32 | var port = buildConfig.port
|
33 |
|
34 | tiresiasWebpack(buildConfig, webpackConfig => {
|
35 | return webpackConfig
|
36 | }, compiler => {
|
37 | compiler.watch({
|
38 | aggregateTimeout: 300,
|
39 | poll: true
|
40 | }, function(err, stats) {
|
41 | compiler.run((err, stats) => {
|
42 | if (err) throw err
|
43 | process.stdout.write(stats.toString({
|
44 | colors: true,
|
45 | modules: false,
|
46 | children: false,
|
47 | chunks: false,
|
48 | chunkModules: false
|
49 | }) + '\n')
|
50 | if (!firstBuildEnd) {
|
51 | serve(port)
|
52 | opn('http://127.0.0.1:' + port)
|
53 | firstBuildEnd = true
|
54 | }
|
55 | })
|
56 | });
|
57 | console.log('building resource please wait...')
|
58 | })
|
59 | }
|
60 |
|
61 |
|
62 | module.exports = compileAndServe
|
63 |
|
64 |
|