-
Notifications
You must be signed in to change notification settings - Fork 20
/
server.js
53 lines (38 loc) · 1.18 KB
/
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
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const config = require('./webpack.config');
const chalk = require('chalk');
const app = express();
const yargs = require('yargs');
const serverPort = process.env.PORT || 3000;
if (yargs.argv._.length !== 1) {
process.stderr.write(chalk.red('ERROR: Please, provide one argument\n'));
process.exit(1);
}
const [entryPath] = yargs.argv._;
const aliasPath = path.join(__dirname, entryPath);
if (!fs.existsSync(aliasPath)) {
process.stderr.write(chalk.red(`ERROR: Folder "${chalk.bold(entryPath)}" doesn't exists\n`));
process.exit(1);
}
config.entry.push(`./${entryPath}/index.js`);
const compiler = webpack(config);
app.use(
require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath
})
);
app.use(require('webpack-hot-middleware')(compiler));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(serverPort, '0.0.0.0', function(err) {
if (err) {
console.log(err);
return;
}
console.log(`Listening at http://localhost:${serverPort}`);
});