-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
49 lines (40 loc) · 1.26 KB
/
tasks.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
const webpack = require('webpack');
// webpack configuration object
const webpackConfig = require('./webpack.config.js');
// webpack development server
const WebpackDevServer = require('webpack-dev-server');
// load project's configurations
const conf = require('./conf/conf');
// Set the event based on the npm script that is running (start, build, test, etc.)
const npmEvent = process.env.npm_lifecycle_event;
// decide which method to run based on the event
switch (npmEvent) {
case 'serve':
startDevelopmentServer();
break;
case 'build':
buildDistFolder();
break;
default:
console.info('tasks.js must run via "npm run ...". you tired running it directly, or ');
break;
}
// method to create and run development server
function startDevelopmentServer() {
const compiler = webpack(webpackConfig);
const server = new WebpackDevServer(compiler, {
hot: true,
stats: 'minimal'
});
server.listen('8080', 'localhost', () => console.log(`WebpackDevServer running on port 8080`));
}
// method to build static webapp
function buildDistFolder() {
const compiler = webpack(webpackConfig);
compiler.run((err, stats) => {
if (err || stats.hasErrors()) {
console.info(err);
}
console.info('dist build finished');
});
}