forked from rwfresh/mvc_jquery_webpack_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.dev.js
95 lines (84 loc) · 2.86 KB
/
server.dev.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const path = require('path');
const express = require("express");
const opn = require("opn");
const LOCAL_HOST_PORT = 3000;
const webpack = require("webpack");
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackHotMiddleware = require("webpack-hot-middleware");
const config = require("./webpack.config");
/*********************************
* Entry
*********************************/
const webpackServer = "webpack/hot/dev-server";
const webpackClient = "webpack-hot-middleware/client";
function addServerAndClientToString(str) {
return [str, webpackServer, webpackClient]
}
function addServerAndClientToArray(array) {
array.push(webpackServer, webpackClient);
return array;
}
if (typeof config.entry === "string") {
config.entry = addServerAndClientToString(config.entry);
} else if (Array.isArray(config.entry)) {
config.entry = addServerAndClientToArray(config.entry);
} else {
for (let e in config.entry) {
if (config.entry.hasOwnProperty(e) === false) {
continue;
}
if (typeof config.entry[e] === "string") {
config.entry[e] = addServerAndClientToString(config.entry[e]);
} else if (Array.isArray(config.entry[e])) {
config.entry[e] = addServerAndClientToArray(config.entry[e]);
} else {
console.error(`Properties of the entry object should be either strings or Array<string>.`);
}
}
}
/*********************************
* Output
*********************************/
config.output.path = "/";
config.output.publicPath = `http://localhost:${LOCAL_HOST_PORT}/wwwroot/js/`;
/*********************************
* HMR Plugin
*********************************/
const hmrPlugin = new webpack.HotModuleReplacementPlugin();
if (typeof config.plugins === "undefined" || config.plugins === null) {
config.plugins = [hmrPlugin];
} else {
let foundHmr = false;
config.plugins.forEach(x => {
foundHmr = foundHmr || x.constructor.name === hmrPlugin.constructor.name;
});
if (foundHmr === false) {
config.plugins.push(hmrPlugin);
}
}
/*********************************
* Express
*********************************/
app = express();
const compiler = webpack(config);
app.use(express.static(__dirname));
app.set('views', path.resolve(__dirname, "View/Shared"));
app.set("view engine", "cshtml")
app.use(webpackDevMiddleware(compiler,
{
publicPath: config.output.publicPath,
stats: { colors: true }
}));
app.use(webpackHotMiddleware(compiler,
{
log: console.log
}));
router = express.Router();
router.get("/", (req, res) => res.render("_LayoutTemplate.cshtml"));
app.use(router);
app.listen(LOCAL_HOST_PORT, () => console.log(`listening on ${LOCAL_HOST_PORT}`));
opn(`http://localhost:${LOCAL_HOST_PORT}`);
const entry = {
"main": ["./ClientApp/src/main.js"],
"users": ["./ClientApp/src/users.js"],
}