-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config-server.js
134 lines (125 loc) · 3.01 KB
/
webpack.config-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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const path = require('path');
const merge = require('webpack-merge'); // mergey JS objects utility
const validate = require('webpack-validator'); //verbose config validation
const nodeExternals = require('webpack-node-externals');
const webpack = require('webpack'); //util plugins
const CleanWebpackPlugin = require('clean-webpack-plugin'); //extract css to its own bundle
const TARGET = process.env.npm_lifecycle_event;
process.env.BABEL_ENV = TARGET;
const PATHS = {
entry: path.join(__dirname, 'server')+'/server.js',
src: [
path.join(__dirname, 'server'),
path.join(__dirname, 'graphQL')
],
build: path.join(__dirname, 'dist'),
build_server: path.join(__dirname, 'dist/server')
};
const UTILS = {
clean : function(path) {
return {
plugins: [
new CleanWebpackPlugin([path], {
// Without `root` CleanWebpackPlugin won't point to our
// project and will fail to work.
root: process.cwd()
})
]
};
},
setFreeVariable : function(key, value) {
const env = {};
env[key] = JSON.stringify(value);
return {
plugins: [
new webpack.DefinePlugin(env)
]
};
},
babel : function(paths) {
return {
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
query: {
cacheDirectory: true,
presets: ['es2015','es2017'],
},
include: paths
}
]
}
};
},
setupJsonLoaders : function(paths) {
return {
module: {
loaders: [
{
test: /\.json$/,
loader: 'json',
include: paths
}
]
}
};
}
}
const base = {
entry: {
// there are may be lot of entries with any names
server: PATHS.entry
// non-existing PATH property will cause an error "TypeError: Cannot read property 'replace' of undefined"
//myapp: PATHS.app
},
output: {
// create the build files in "dist" directory
path: PATHS.build_server,
// non-existing PATHS property will cause the files will be created in the root
// path: PATHS.dist,
// create myapp.js in "dist" directory
filename: '[name].js'
},
resolve: {
extensions: ['', '.js']
},
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
target: 'node'
};
const common = merge(
base,
UTILS.setupJsonLoaders(PATHS.src),
UTILS.babel(PATHS.src)
);
var config;
switch(process.env.npm_lifecycle_event) {
case 'build:server':
case 'stats:server':
config = merge(
common,
{
devtool: 'source-map'
},
UTILS.setFreeVariable(
'process.env.NODE_ENV',
'production'
),
UTILS.clean(PATHS.build_server)
);
break;
case 'watch-dev-build:server':
case 'watch:server':
config = merge(common,
{
devtool: 'eval-source-map'
}
);
break;
default:
config = merge(common, {});
}
module.exports = validate(config, {
quiet: true
});