-
Notifications
You must be signed in to change notification settings - Fork 13
/
tasks.js
64 lines (53 loc) · 1.57 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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 'docs':
buildDocs();
break;
case 'library':
buildLibrary();
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 buildDocs() {
const compiler = webpack(webpackConfig);
compiler.run((err, stats) => {
if (err || stats.hasErrors()) {
console.info(err);
}
console.info('docs build finished');
});
}
// method to build stand-alone library
function buildLibrary() {
const compiler = webpack(webpackConfig);
compiler.run((err, stats) => {
if (err || stats.hasErrors()) {
console.info('oops, library build failed!');
}
console.info('library build finished');
});
}