-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.server.config.js
136 lines (126 loc) · 2.84 KB
/
webpack.server.config.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
135
136
'use strict';
/*
* Load dependencies
*/
const webpack = require('webpack');
const path = require('path');
const config = require('./lib/config');
const packageJSON = require('./package.json');
const deps = [].concat(
Object.keys(packageJSON.dependencies),
Object.keys(packageJSON.devDependencies)
);
/*
* Main webpack config
*/
let webpackCfg = {
target: 'node',
devtool: 'source-map',
entry: {
index: [path.join(config.root, 'server')],
},
resolve: config.webpack.resolve,
stats: {
colors: true,
reasons: true,
children: false,
},
output: {
path: path.join(config.root, 'server_build'),
filename: '[name].js',
},
performance: {
hints: false,
},
node: {
console: false,
global: false,
process: false,
__filename: false,
__dirname: false,
Buffer: false,
setImmediate: false,
},
externals: [
(context, request, callback) => {
// Check if dependency or attempt to resolve the module via Node
if (request.match(/^(\.{0,2})\//)) {
// Absolute & Relative paths are not externals
return callback();
}
try {
if (deps.includes(request) || require.resolve(request)) {
return callback(null, 'commonjs ' + request);
}
} catch (e) {
// Node couldn't find it, so it must be user-aliased
return callback();
}
},
],
};
/*
* Webpack modules
*/
webpackCfg.module = {
rules: [
{
// JS/JSX loader + hot reload
test: /\.jsx?$/,
use: [{ loader: 'babel-loader' }],
},
{
test: /\.marko$/,
use: [{ loader: 'marko-loader', query: { target: 'server' } }],
},
{
// SVG Icons sprite loader
test: /\.svg$/,
include: [path.join(config.root, 'app', 'assets', 'icons')],
use: [
{
loader: 'svg-sprite-loader',
options: {
symbolId: 'i-[name]',
},
},
],
},
],
};
/*
* Webpack plugins
*/
webpackCfg.plugins = [
// Variable replacement to bridge client/server side globals
new webpack.DefinePlugin(
Object.assign(
{
// 'process.env.NODE_ENV': JSON.stringify(config.env),
__CLIENT__: false, // allow detection if serverside rendering
},
// provide server side config vars
pathKeys(config.client, 'CONFIG_CLIENT')
)
),
new webpack.BannerPlugin({
banner: 'require("source-map-support").install();',
raw: true,
entryOnly: false,
}),
new webpack.NormalModuleReplacementPlugin(
config.server.resolve.ignore,
res => {
if (!/assets\/icons\/(.*)\.svg$/.test(res.request)) {
res.request = 'lodash/noop';
}
}
),
];
function pathKeys(obj, root) {
return Object.keys(obj).reduce((r, k) => {
r[root + '.' + k] = JSON.stringify(obj[k]);
return r;
}, {});
}
module.exports = webpackCfg;