-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
189 lines (172 loc) · 5.46 KB
/
webpack.config.ts
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
import path from 'path';
import {compact} from 'lodash';
import {NamedModulesPlugin, HotModuleReplacementPlugin, LoaderOptionsPlugin, DefinePlugin} from 'webpack';
import {rootDOMNodeId} from './src/config/vars';
import {BuildOptions} from './src/config/configConsts';
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const WebpackStrip = require('webpack-strip');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const featureSwitches = require('./featureToggles');
export const rootFolder = __dirname;
export const srcFolder = path.resolve(rootFolder, 'src');
export const entriesFolder = path.resolve(srcFolder, 'entries');
const assetFolder = 'assets';
const features_env_index = process.argv.indexOf('--env.environment');
const features_env = process.argv[features_env_index + 1];
export async function createWebpackConfig(additionalOptions: Partial<BuildOptions> = {}) {
const options: BuildOptions = {
...new BuildOptions(),
...additionalOptions,
};
const filePattern = (name = '[name]', ext = '[ext]', asset = true) => {
const namePattern = `${name}${options.hash ? '.[hash:6]' : ''}.${ext}`;
return asset ? `${assetFolder}/${namePattern}` : namePattern;
};
const fileLoader = {
loader: 'file-loader',
options: {
name: filePattern(),
emitFile: true,
},
};
const stripLoader = {
loader: WebpackStrip.loader('console.log', 'console.error', 'console.warn'),
};
const config: any = {
// What code to build and where to put it
entry: compact([
path.resolve(srcFolder, 'polyfills'),
options.hmr && 'react-hot-loader/patch',
absOrPrefix(options.entry, entriesFolder),
]),
output: {
path: absOrPrefix(options.outputFolder, rootFolder),
filename: '[name].[hash].js',
publicPath: '/',
},
// Most webpack configs are controlled by our options
mode: 'none',
stats: {errorDetails: true},
cache: options.cache,
devtool: options.sourceMaps ? 'source-map' : undefined,
// Determine which extensions to lazy-load and how to look for sources
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
modules: [srcFolder, 'node_modules'],
},
optimization: {
splitChunks: {
chunks: 'all',
},
},
// Teach webpack how to load various modules
module: {
rules: [
{test: /\.pug$/, use: 'pug-loader'},
{
test: /\.(ts|tsx|js|jsx)$/,
use: options.environment === 'demo' ? [stripLoader] : [],
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [fileLoader],
},
{
include: [path.resolve(__dirname, 'src'), path.resolve(__dirname, 'node_modules/@material-ui')],
test: /\.(tsx?|jsx?)$/,
loader: 'babel-loader',
},
{
test: /\.js$/,
use: ['source-map-loader'],
enforce: 'pre',
},
{
test: /\.png$/,
use: [fileLoader],
},
{
test: /\.ico$/,
use: [fileLoader],
},
{
test: /\.svg$/,
use: [fileLoader],
},
{
test: /\.jpg$/,
use: [fileLoader],
},
{
test: /\.(gif)$/,
use: 'url-loader?limit=30000&name=[name]-[hash].[ext]',
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
plugins: compact([
// Generates an index.html with links for all scripts and assets bundled.
options.index &&
new HtmlWebpackPlugin({
rootDOMNodeId,
buildOptions: options,
filename: 'index.html',
template: path.resolve(entriesFolder, 'index.pug'),
}),
// Define environments
new DefinePlugin({
'process.env': {
...featureSwitches[features_env],
},
'process.env.BUILD_OPTIONS': JSON.stringify(options),
'process.env.NODE_ENV': JSON.stringify(options.environment),
}),
new FaviconsWebpackPlugin({
logo: './src/assets/icons/favicon/favicon-32x32.png',
persistentCache: true,
inject: true,
icons: {
android: true,
appleIcon: true,
appleStartup: true,
coast: false,
favicons: true,
firefox: true,
opengraph: false,
twitter: false,
yandex: false,
windows: false,
},
}),
// Optional features
options.hmr && new NamedModulesPlugin(),
options.hmr && new HotModuleReplacementPlugin(),
options.debug && new LoaderOptionsPlugin({debug: true}),
options.analyzeBundles && new BundleAnalyzerPlugin({analyzerMode: 'static'}),
options.minify &&
new UglifyJsPlugin({
test: /\.js$/,
parallel: true,
sourceMap: options.sourceMaps,
compress: {
drop_console: options.environment === 'demo',
},
}),
options.friendly && new FriendlyErrorsWebpackPlugin(),
]),
devServer: {
historyApiFallback: true,
},
};
return config;
}
export default createWebpackConfig; // tslint:disable-line
function absOrPrefix(p: string, prefix: string) {
return path.isAbsolute(p) ? p : path.join(prefix, p);
}