forked from ENCODE-DCC/encoded
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
165 lines (153 loc) · 4.8 KB
/
webpack.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* global process, __dirname */
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const env = process.env.NODE_ENV;
const PATHS = {
static: path.resolve(__dirname, 'src/encoded/static'),
build: path.resolve(__dirname, 'src/encoded/static/build'),
serverbuild: path.resolve(__dirname, 'src/encoded/static/build-server'),
fonts: path.resolve(__dirname, 'src/encoded/static/font'),
images: path.resolve(__dirname, 'src/encoded/static/img'),
};
const plugins = [];
// To get auth0 v11 to build correctly:
// https://github.com/felixge/node-formidable/issues/337#issuecomment-183388869
plugins.push(new webpack.DefinePlugin({ 'global.GENTLY': false }));
let chunkFilename = '[name].js';
let styleFilename = './css/[name].css';
let mode = 'development';
const optimization = {};
if (env === 'production') {
optimization.minimize = true;
optimization.minimizer = [
new TerserPlugin({
sourceMap: true,
}),
new OptimizeCSSAssetsPlugin({}),
];
// Set production version of React
// https://stackoverflow.com/questions/37311972/react-doesnt-switch-to-production-mode#answer-37311994
plugins.push(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(env),
},
})
);
// add chunkhash to chunk names for production only (it's slower)
chunkFilename = '[name].[chunkhash].js';
styleFilename = './css/[name].[chunkhash].css';
mode = 'production';
}
const rules = [
{
test: /\.js$/,
include: [
PATHS.static,
path.resolve(__dirname, 'node_modules/dagre-d3'),
path.resolve(__dirname, 'node_modules/superagent'),
],
use: {
loader: 'babel-loader',
query: { compact: false },
},
},
{
test: /\.(jpg|png|gif)$/,
include: PATHS.images,
use: [
{
loader: 'url-loader',
options: {
limit: 25000,
},
},
],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: { sourceMap: true } },
{ loader: 'sass-loader', options: { sourceMap: true } },
],
},
];
module.exports = [
// for browser
{
context: PATHS.static,
entry: {
inline: './inline',
style: './scss/style.scss',
},
output: {
path: PATHS.build,
publicPath: '/static/build/',
filename: '[name].js',
chunkFilename,
},
module: {
rules,
},
devtool: 'source-map',
mode,
optimization,
plugins: plugins.concat(
// Add a browser-only plugin to extract Sass-compiled styles and place them into an
// external CSS file
new MiniCssExtractPlugin({
filename: styleFilename,
}),
// Add a browser-only plugin executed when webpack is done with all transforms. it
// writes minimal build statistics to the "build" directory that contains the hashed
// CSS file names that the server can render into the <link rel="stylesheet"> tag.
function writeWPStats() {
this.plugin('done', (stats) => {
// Write hash stats to stats.json so we can extract the CSS hashed file name.
require('fs').writeFileSync(
path.join(PATHS.build, 'stats.json'),
JSON.stringify(stats.toJson({ hash: true }, 'none')));
});
}
),
},
// for server-side rendering
{
entry: {
renderer: './src/encoded/static/server.js',
},
target: 'node',
// make sure compiled modules can use original __dirname
node: {
__dirname: true,
},
externals: [
'brace',
'brace/mode/json',
'brace/theme/solarized_light',
'd3',
'dagre-d3',
'chart.js',
// avoid bundling babel transpiler, which is not used at runtime
'@babel/register',
],
output: {
path: PATHS.serverbuild,
publicPath: '/static/build-server',
filename: '[name].js',
libraryTarget: 'commonjs2',
chunkFilename,
},
module: {
rules,
},
devtool: 'source-map',
mode,
optimization,
plugins,
},
];