-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.prod.babel.js
200 lines (187 loc) · 6.59 KB
/
webpack.config.prod.babel.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import path from 'path';
import webpack from 'webpack';
import postcssCssnext from 'postcss-cssnext';
import postcssImport from 'postcss-import';
import merge from 'webpack-merge';
import AddAssetHtmlPlugin from 'add-asset-html-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ProgressBarWebpackPlugin from 'progress-bar-webpack-plugin';
import WebpackMd5Hash from 'webpack-md5-hash';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import * as ReactManifest from './frontend/dist/dll/react_manifest.json'; // eslint-disable-line import/no-unresolved
import * as ImmutableManifest from './frontend/dist/dll/immutable_manifest.json'; // eslint-disable-line import/no-unresolved
import * as MaterializeManifest from './frontend/dist/dll/materialize_manifest.json'; // eslint-disable-line import/no-unresolved
import * as MiscManifest from './frontend/dist/dll/misc_manifest.json'; // eslint-disable-line import/no-unresolved
const isProfile = process.env.profile;
let config = {
// The base directory, an absolute path, for resolving entry points and loaders from configuration
context: path.resolve(__dirname),
// Start entry point(s)
entry: {
app: [
'./frontend/src/index',
],
},
// Affecting the output of the compilation
output: {
// path: the output directory as an absolute path (required)
path: path.resolve(__dirname, 'frontend/dist/prod'),
// filename: specifies the name of output file on disk (required)
// Use hash string to handle client side cache problem
filename: '[name]-[chunkhash:10].js',
},
// Determine how the different types of modules within a project will be treated
module: {
rules: [
// Use awesome-typescript-loader and babel-loader for ts(x) files
{
test: /\.tsx?$/,
use: [
{ loader: 'babel-loader' },
// Use those two flags to speed up babel compilation
// https://github.com/s-panferov/awesome-typescript-loader#differences-between-ts-loader
{ loader: 'awesome-typescript-loader', options: { useBabel: true, useCache: true, configFileName: 'tsconfig.frontend.json' } },
// Alternatively, we can use ts-loader instead of awesome-typescript-loader
// { loader: 'ts-loader' },
],
exclude: /node_modules/,
},
// Use ExtractTextPlugin and list of loaders to load css files
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{ loader: 'css-loader', options: { importLoaders: 1 } }, // https://github.com/webpack-contrib/css-loader#importloaders
{ loader: 'postcss-loader', options: { plugins: () => [postcssImport, postcssCssnext] } },
],
}),
},
// Use ExtractTextPlugin and list of loaders to load scss files
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{ loader: 'css-loader', options: { importLoaders: 2 } },
{ loader: 'postcss-loader', options: { plugins: () => [postcssImport, postcssCssnext] } },
'sass-loader',
],
}),
},
// Use file-loader and image-loader to load images
{
test: /\.(png|jpe?g|gif|svg)$/,
use: [
{ loader: 'url-loader', options: { limit: 10000 } },
{ loader: 'image-webpack-loader', options: { bypassOnDebug: true } },
],
},
// Use file-loader to load font related files and icon
{
test: /\.(eot|woff2?|ttf|ico)$/,
use: [
{ loader: 'file-loader', options: { name: '[name].[ext]' } },
],
},
],
},
// A list of used webpack plugins
plugins: [
// Better building progress display
new ProgressBarWebpackPlugin({
clear: false,
}),
// Minimize javascript files with source map generated
new webpack.optimize.UglifyJsPlugin({
output: { comments: false },
sourceMap: true,
}),
// Module concatenation optimization from webpack v3
new webpack.optimize.ModuleConcatenationPlugin(),
// Define production env which shaved off 75% of the build output size
// http://moduscreate.com/optimizing-react-es6-webpack-production-build
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
}),
// jQuery support
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
'root.jQuery': 'jquery',
}),
// Load pre-build dll reference files
new webpack.DllReferencePlugin({
manifest: ReactManifest,
context: __dirname,
}),
new webpack.DllReferencePlugin({
manifest: ImmutableManifest,
context: __dirname,
}),
new webpack.DllReferencePlugin({
manifest: MaterializeManifest,
context: __dirname,
}),
new webpack.DllReferencePlugin({
manifest: MiscManifest,
context: __dirname,
}),
// Generate html file to dist folder
new HtmlWebpackPlugin({
title: 'Express Gateway Manager',
template: 'frontend/template/index.ejs',
}),
// Add dll reference files to html
new AddAssetHtmlPlugin([
{ filepath: 'frontend/dist/dll/react_dll.js', includeSourcemap: false },
{ filepath: 'frontend/dist/dll/immutable_dll.js', includeSourcemap: false },
{ filepath: 'frontend/dist/dll/materialize_dll.js', includeSourcemap: false },
{ filepath: 'frontend/dist/dll/misc_dll.js', includeSourcemap: false },
]),
// Extract css part from javascript bundle into a file
new ExtractTextPlugin('[name]-[contenthash:10].css'),
// Better hash for [hash] and [chunkhash]
new WebpackMd5Hash(),
],
// Change how modules are resolved
resolve: {
// What directories should be searched when resolving modules
modules: [
'node_modules',
'frontend/src',
],
// Automatically resolve certain extensions (Ex. import 'folder/name(.ext)')
extensions: [
'.ts',
'.tsx',
'.js',
'.jsx',
'.json',
'.scss',
],
},
// Source map mode
// https://webpack.js.org/configuration/devtool
devtool: 'source-map',
};
// Profile
if (isProfile) {
config = merge(config, {
plugins: [
// Extend base config
...config.plugins,
// Webpack bundle analyzer for profiling
new BundleAnalyzerPlugin({
analyzerPort: 3003,
}),
],
});
}
// Export const (import/no-mutable-exports)
const constConfig = config;
export default constConfig;