-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
204 lines (193 loc) · 4.7 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
/* global __dirname, process */
const path = require('path'); //node module to resolve paths
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// the path(s) that should be cleaned
const pathsToClean = [
'www'
];
// the clean options to use
const cleanOptions = {
// root: '/full/webpack/root/path',
// exclude: ['shared.js'],
verbose: true,
dry: false
};
const babelSettings = {
extends: path.join(__dirname, '/.babelrc')
};
const PATHS = {
src: path.join(__dirname, 'src'),
build: path.join(__dirname, 'www'),
};
const devEntries = [
'react-hot-loader/patch',
'webpack-dev-server/client?http://0.0.0.0:8081', //DEV only
'webpack/hot/only-dev-server', //DEV only
];
let prodEntries = [
'@babel/polyfill',
'es6-promise',
'whatwg-fetch',
PATHS.src
];
module.exports = env => {
env = { options: process.env.NODE_ENV };
console.log('process.env=', process.env.NODE_ENV);
if (env && env.options && env.options === 'development') {
prodEntries = devEntries.concat(prodEntries);
}
return {
// devServer is for dev only
devServer: {
compress: true,
inline: true,
port: 8081,
// outputPath: PATHS.build,
contentBase: PATHS.build
},
devtool: 'inline-source-map',
// devtool: 'eval-source-map',
// devtool: 'cheap-module-source-map',
// devtool: 'inline-source-map',
entry: {
'./www': prodEntries,
vendor: [
'react',
'react-dom',
'react-redux',
'react-router',
'react-router-dom',
'react-router-redux',
'react-transition-group',
'redux',
'redux-thunk',
'reselect',
'immutable',
'whatwg-fetch',
'es6-promise',
'history',
'keymirror',
],
},
output: {
filename: 'bundle.[hash].js', //destination file name
path: PATHS.build, //destination folder
// publicPath: '/public/', //append this to your url when trying to access app
// // export itself to a global var
// libraryTarget: 'var',
// // name of the global var: "Foo"
// library: 'SWSReactApp'
},
resolve: {
modules: [ //will pick up references from other laravel repo the folder needs to be named 'app'
__dirname,
path.join(__dirname, 'node_modules')
],
plugins: [
],
extensions: ['.js','.jsx','.ts', '.tsx'],
alias: {}
},
module: { //add multiple loaders to process files
noParse: /node_modules\/localforage\/dist/, //localforage comes pre-built webpack does'nt need to do anything
rules: [
{
// Only run .js & .jsx files through Babel
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: babelSettings
}
]
},
{
// Transform our own .css files with PostCSS and CSS-modules
test: /\.css$/,
// exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
query: {
modules: true,
importLoaders: 1,
localIdentName: '[local]-[hash:base64:5]'
}
}
]
})
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader',
options: {
limit: 10000,
mimetype: 'application/font-woff',
name: '[name].[ext]'
}
},
{
test: /\.(ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
exclude: [/images/],
loader: 'file-loader',
options: {
name: '[name].[ext]'
}
},
{
test: /.*\.(gif|png|jpe?g)$/i,
loader: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]'
}
},
{
loader: 'image-webpack-loader',
options: {
progressive: true,
optimizationLevel: 7,
interlaced: false,
pngquant: {
quality: '65-90',
speed: 4
}
}
},
]
},
{
test: /\.svg$/,
exclude: /node_modules/,
loader: 'svg-react-loader'
}
]
},
plugins: [
new webpack.EnvironmentPlugin(['NODE_ENV', 'DEBUG']),
new CleanWebpackPlugin(pathsToClean, cleanOptions),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new ExtractTextPlugin('styles.css'),
// new BundleAnalyzerPlugin({ analyzerMode: 'static' }),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: Infinity,
filename: '[name].[hash].js',
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, 'src/index.html'),
filename: 'index.html',
inject: 'body',
}),
]
};
};