UNPKG

1.73 kBJavaScriptView Raw
1const path = require('path')
2const os = require('os')
3const tiresias = require('tiresias')
4const tiresiasWebpack = require('tiresias-webpack')
5const opn = require('opn')
6
7const webrootDir = path.join(os.tmpdir(), './webroot')
8const testBuildRootDir = path.join(__dirname, '../test')
9
10const defaultConfig = {}
11defaultConfig.port = 9999
12defaultConfig.rootDir = testBuildRootDir
13defaultConfig.distDir = webrootDir
14
15var firstBuildEnd = false
16
17function serve (port) {
18 var serverConfig = {}
19 serverConfig.rootDir = webrootDir
20
21 tiresias.setConfig(serverConfig)
22 tiresias.simpleStart(port)
23}
24
25function 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({ // watch options:
38 aggregateTimeout: 300, // wait so long for more changes
39 poll: true // use polling instead of native watchers
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// compileAndServe()
62module.exports = compileAndServe
63
64