-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
148 lines (141 loc) · 3.69 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
/**
* Created by warren on 3/16/17.
*/
const path = require('path');
const webpack = require('webpack');
const HtmlwebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');
const ENV = {
host: process.env.HOST || 'localhost',
port: process.env.PORT || 8080
};
const PATHS = {
dist: path.join(__dirname, 'dist'),
src: path.join(__dirname, 'src'),
css: path.join(__dirname, 'src', 'css'),
cache: path.join(__dirname, 'cache'),
node_modules: path.join(__dirname, 'node_modules'),
static: path.join(__dirname, 'src', 'static')
};
const common_build = {
context: __dirname,
entry: [
"babel-polyfill",
PATHS.src
],
resolve: {
extensions: ['.js']
},
output: {
path: PATHS.dist,
publicPath: '/'
},
module: {
loaders: [
{
test: /\.(woff|woff2|eot|ttf|csv|svg|ico|png|jpg)$/,
loader: 'url-loader?limit=100000',
include: [PATHS.src]
}, {
test: /\.json$/,
loader: 'json-loader',
include: PATHS.static
}
]
},
plugins: [
new HtmlwebpackPlugin({
template: 'node_modules/html-webpack-template/index.ejs',
title: 'apptab',
appMountId: 'app',
favicon: path.join(PATHS.static, 'favicon.ico'), //TODO: make favicon work
inject: false
})
]
};
const dev_build = {
devtool: 'source-map',
plugins: [
new webpack.HotModuleReplacementPlugin()
],
output: {
filename: '[name].bundle.js',
},
devServer: {
hot: true,
historyApiFallback: true,
inline: true,
contentBase: PATHS.dist,
stats: 'errors-only',
host: ENV.host,
port: ENV.port
},
module: {
loaders: [{
test: /\.js$/,
loaders: ['source-map-loader', 'react-hot-loader', 'babel-loader?cacheDirectory=' + PATHS.cache],
exclude: PATHS.node_modules
}, {
test: /\.css$/,
loaders: ['style-loader', 'css-loader'],
include: PATHS.css
}, {
test: /\.tsx?$/,
loaders: ['typescript-loader', 'react-hot-loader', 'babel-loader?cacheDirectory=' + PATHS.cache],
exclude: PATHS.node_modules
}]
}
};
const prod_build = {
output: {filename: '[name].[chunkhash].js', chunkFilename: '[chunkhash].js'},
module: {
loaders: [{
test: /\.js$/,
loader: 'babel-loader',
exclude: PATHS.node_modules,
}, {
test: /\.css$/,
loader: ExtractTextPlugin.extract({fallback: 'style-loader', use: 'css-loader'}),
include: PATHS.css
}]
},
plugins: [
new webpack.DefinePlugin({'process.env': {NODE_ENV: JSON.stringify("production")}}),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {warnings: false, drop_console: true},
beautify: false,
comments: false,
warnings: false,
drop_console: true,
mangle: {except: ['$', 'webpackJsonp'], screw_ie8: true, keep_fnames: false}
}),
new ExtractTextPlugin('[name].[chunkhash].css')
]
};
const clean_build = {
plugins: [
new CleanWebpackPlugin([PATHS.dist], {root: process.cwd()})
]
};
module.exports = function (env) {
let config;
switch (env) {
case 'development':
config = merge(common_build, dev_build, clean_build);
config.plugins.push(new webpack.DefinePlugin({
__STAGE__: JSON.stringify(env)
}));
return config;
case 'production':
config = merge(common_build, prod_build, clean_build);
config.plugins.push(new webpack.DefinePlugin({
__STAGE__: JSON.stringify(env)
}));
return config;
default:
return merge(common_build, dev_build)
}
};