-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
162 lines (146 loc) · 4.52 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
/* eslint-env node */
var fs = require('fs')
var path = require('path')
var webpack = require('webpack')
var CleanWebpackPlugin = require('clean-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var CopyWebpackPlugin = require('copy-webpack-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var appVersion = require('./package.json').version
var isProd = process.env.NODE_ENV === 'production'
var buildBaseName = 'build'
var buildBase = path.join(__dirname, buildBaseName)
var mainPath = path.join(__dirname, 'app', 'entry')
var entryPoints = {}
var entryPointFiles
try {
entryPointFiles = fs.readdirSync(mainPath)
} catch(err) {
entryPointFiles = []
}
entryPointFiles
.filter(fileName => ['.DS_Store', 'desktop.ini', 'Desktop.ini'].indexOf(fileName) === -1)
.forEach(fileName => {
var entryPointName = path.basename(fileName, path.extname(fileName))
entryPoints[entryPointName] = path.join(mainPath, fileName)
})
var extractStyles = new ExtractTextPlugin({
filename: 'style_app_[chunkhash:8].css',
allChunks: true,
})
var config = {
entry: entryPoints,
output: {
path: buildBase,
filename: 'app_[name]_[chunkhash:8].js',
chunkFilename: 'app_[name]_[chunkhash:8].js',
},
module: {
rules: [
{
test: /\.html$/,
use: {
loader: 'html',
options: {
interpolate: 'require',
attrs: ['img:src', 'link:href'],
},
},
},
{
test: /\.jsx?$/i,
exclude: /node_modules/,
use: {
loader: 'babel',
options: {
presets: [
['es2015', { modules: false }],
'stage-2',
'react',
],
plugins: [
'transform-runtime',
'transform-strict-mode',
'transform-decorators-legacy',
[ 'emotion/babel', { inline: true }],
],
env: {
development: {
plugins: [
'transform-react-stateless-component-name',
]
},
production: {
presets: [
'react-optimize',
],
},
}
}
}
},
{
test: /\.css$/,
use: extractStyles.extract({
fallback: 'style',
use: 'css'
})
},
]
},
resolve: {
extensions: ['.js', '.jsx', '.json'],
},
resolveLoader: {
moduleExtensions: ['-loader'],
},
performance: {
hints: isProd ? 'warning' : false,
},
plugins: [
extractStyles,
new webpack.DefinePlugin({
APP_VERSION: JSON.stringify(appVersion),
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: path.join(__dirname, 'app/index.tmpl.html'),
minify: isProd ? {
collapseWhitespace: true,
minifyJS: true,
} : false,
}),
new CopyWebpackPlugin([
{ from: path.join(__dirname, 'app/favicon.ico'), to: 'favicon.ico' },
]),
new webpack.optimize.CommonsChunkPlugin({
async: false,
children: true,
minChunks: 2,
}),
]
}
if (isProd) {
config.plugins.unshift(new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
}))
config.plugins.push(new webpack.optimize.UglifyJsPlugin({
output: {
comments: false,
},
}))
config.plugins.push(new CleanWebpackPlugin([buildBaseName + '/*.*']))
} else {
config.devServer = {
inline: true,
port: process.env.PORT ? parseInt(process.env.PORT, 10) : 3333,
contentBase: buildBase,
historyApiFallback: {
index: '/index.html',
},
}
config.devtool = '#source-map'
}
module.exports = config