-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.config.js
126 lines (117 loc) · 2.97 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
const _ = require('lodash')
const path = require('path')
const webpack = require('webpack')
const pkg = require('./package')
const autoprefixer = require('autoprefixer')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
require('dotenv').config()
// Webpack: The Confusing Parts
// https://medium.com/@rajaraodv/webpack-the-confusing-parts-58712f8fcad9
const isDevelopment = process.env.NODE_ENV === 'development'
const envKeys = [ 'API_HOST', 'API_PORT', 'NODE_ENV' ]
const envValues = _.map(envKeys, key => JSON.stringify(process.env[key]))
const plugins = [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({
'process.env': _.zipObject(envKeys, envValues)
}),
new ExtractTextPlugin({
filename: `${pkg.name}.css`,
disable: false,
allChunks: true
})
]
if (!isDevelopment) {
plugins.push(new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
beautify: false,
mangle: {
screw_ie8: true,
keep_fnames: true
},
compress: { screw_ie8: true },
comments: false
}))
}
// this default is production
const production = {
devtool: 'source-map',
entry: {
[pkg.name]: ['babel-polyfill', './src/index']
},
output: {
path: path.join(__dirname, 'www'),
filename: '[name].js',
publicPath: 'http://localhost:8080/'
},
resolve: {
extensions: ['.js']
},
module: {
rules: [
{
test: /\.js?$/,
use: [
{ loader: 'babel-loader', options: { sourceMap: true } },
],
include: path.join(__dirname, 'src')
},
{
test: /\.s[ac]ss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{ loader: 'css-loader', options: { sourceMap: true } },
{ loader: 'postcss-loader',
options: {
plugins () {
return [autoprefixer('last 2 versions')]
},
sourceMap: true
}
},
{ loader: 'sass-loader', options: { sourceMap: true } }
]
})
},
{
test: /\.(png|jpg|gif|svg)$/,
use: [
{ loader: 'url-loader', options: { limit: 100000 } }
]
},
{
test: /\.(ttf|eot|woff|woff2)$/,
use: [
{ loader: 'file-loader' }
]
}
]
},
plugins
}
let config = _.cloneDeep(production)
if (isDevelopment) {
_.assign(config, {
devtool: 'eval-source-map',
devServer: {
contentBase: path.join(__dirname, 'www'),
inline: true,
historyApiFallback: {
index: 'index.html'
},
host: '0.0.0.0',
watchOptions: {
ignored: [
/node_modules/,
/mocks/,
/tests/,
/www/
]
},
}
})
// despite webpack-dev-server not actually outputting a file, without this, webpack will throw a misguided error about loaders
_.set(config, 'output.filename', `${pkg.name}.js`)
}
module.exports = config