-
Notifications
You must be signed in to change notification settings - Fork 18
/
webpack.config.js
208 lines (197 loc) · 7.36 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
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
201
202
203
204
205
206
207
208
const webpack = require('webpack');
const copy = require('copy-webpack-plugin');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const flavors = {
prod: {
mode: 'production',
monacoPath: 'node_modules/monaco-editor/min/vs',
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
}),
new MiniCssExtractPlugin({filename: "[name].[contentHash].css"})
],
},
dev: {
mode: 'development',
monacoPath: 'node_modules/monaco-editor/dev/vs',
plugins: []
},
bundleAnalyze: {
plugins: [
new BundleAnalyzerPlugin()
]
}
};
module.exports = (args) => {
let flavorsInBuild = ['dev'];
if (typeof args === 'string') {
flavorsInBuild = args.split(',')
}
const notFound = flavorsInBuild.filter(f => !flavors[f]);
if (notFound.length > 0) {
console.log('\x1b[31m\033[1m%s\x1b[0m', `ERROR: [${notFound.join(', ')}] not found in flavors`);
return {}
}
const conf = Object.assign({}, ...flavorsInBuild.map(f => flavors[f]));
conf.plugins = flavorsInBuild.map(f => flavors[f].plugins).reduce((a, b) => a.concat(b));
const outputPath = path.resolve(__dirname, 'dist');
return {
entry: {
app: './src/index.tsx'
},
mode: conf.mode,
output: {
filename:'[name].[hash].bundle.js',
// chunkFilename: '[name].[chunkhash].bundle.js',
publicPath: '/',
path: outputPath,
pathinfo: false
},
plugins: [
new copy([
{from: 'build'},
// {from: 'web'},
{from: 'src/assets', to: 'assets'}
]),
new HtmlWebpackPlugin({
template: 'template.html',
hash: true,
production: conf.mode === 'production'
}),
new HtmlWebpackExternalsPlugin({
externals: [
{
module: 'react',
entry: conf.mode === 'production' ? 'umd/react.production.min.js' :'umd/react.development.js' ,
global: 'React'
},
{
module: 'react-dom',
entry: conf.mode === 'production' ? 'umd/react-dom.production.min.js' : 'umd/react-dom.development.js',
global: 'ReactDOM'
},
{
module: '@waves/ride-js',
entry: 'dist/ride.min.js',
global: 'RideJS'
}
],
hash: true
}),
new CleanWebpackPlugin('dist'),
new ForkTsCheckerWebpackPlugin(),
new webpack.HotModuleReplacementPlugin()
].concat(conf.plugins),
//Enable sourcemaps for debugging webpack's output.
devtool: conf.mode === 'development' ? 'eval' : undefined,
resolve: {
//Add '.ts' and '.tsx' as resolvable extensions.
//Add '.ts' and '.tsx' as resolvable extensions.
extensions: ['.ts', '.tsx', '.js', '.json', '.jsx', '.css'],
alias: {
'@components': path.resolve(__dirname, "./src/components"),
'@services': path.resolve(__dirname, "./src/services"),
'@src': path.resolve(__dirname, "./src"),
'@stores': path.resolve(__dirname, "./src/stores"),
'@utils': path.resolve(__dirname, "./src/utils")
}
},
stats: {
warningsFilter: /export .* was not found in/
},
optimization: {
minimize: true,
minimizer: [
new OptimizeCssAssetsPlugin(),
new TerserPlugin()
]
},
module: {
rules: [
{
test: /\.(png|jpg|svg|gif)$/,
loader: "url-loader?limit=1000&name=assets/img/[name].[ext]",
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
experimentalWatchApi: true,
},
},
],
},
{
test: /\.less$/,
use: [
conf.mode === 'production' ? MiniCssExtractPlugin.loader : {loader: "style-loader"},
{
loader: "css-loader",
options: {
modules: true,
localIdentName: '[folder]__[local]--[hash:base64:5]',
}
},
{loader: "less-loader",
options: {
// modifyVars: themeVariables,
root: path.resolve(__dirname, './')
}
},
]
},
{
include: /rc-collapse|rc-select|rc-tree|react-perfect-scrollbar|rc-dialog|rc-notification|rc-dropdown|rc-menu|rc-tooltip|rc-tabs|src|repl|normalize|antd/,
test: /\.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
require('postcss-inline-svg'),
],
},
},
],
},
]
},
externals: {
'monaco-editor': 'monaco',
'monaco-editor/esm/vs/editor/editor.api': 'monaco',
},
devServer: {
hot: true,
historyApiFallback: true,
proxy: {
'/api': {
target: 'http://localhost:3000'
}
}
}
}
};