-
Notifications
You must be signed in to change notification settings - Fork 29
/
webpack.config.js
236 lines (214 loc) · 8.54 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'use strict';
// Modules
const path = require('path');
var APP_PATH = path.resolve(__dirname, 'src');
const webpack = require('webpack');
const helpers = require('./webpack/helpers');
const autoprefixer = require('autoprefixer');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
// set the environment by npm lifecycle event , `npm run build` npm_lifecycle_event is build
const ENV = process.env.npm_lifecycle_event;
const isTest = ENV === 'test' || ENV === 'test-watch';
const isProd = ENV === 'build';
module.exports = function () {
const config = {
context: helpers.root('./src'),
entry: {
'vendor': ['angular', 'angular-ui-router'],
'app': './app.js',
},
output: {
path: helpers.root('./dist'),
publicPath: '/',
//library : '[name]_[hash:8]',
filename: isProd ? '[name].[hash:8].js' : '[name].bundle.js',
chunkFilename: isProd ? '[name].[hash:8].js' : '[name].bundle.js'
},
/*
* Options affecting the normal modules.
* See: http://webpack.github.io/docs/configuration.html#module
*/
module: {
/*
* An array of applied pre and post loaders.
*
* See: http://webpack.github.io/docs/configuration.html#module-preloaders-module-postloaders
*/
preLoaders: [
/*
* Source map loader support for *.js files
* Extracts SourceMaps for source files that as added as sourceMappingURL comment.
*
* See: https://github.com/webpack/source-map-loader
*/
{
test: /\.js$/,
loader: 'source-map-loader',
exclude: [
// these packages have problems with their sourcemaps
helpers.root('node_modules/angular'),
helpers.root('node_modules/angular-ui-router')
]
}
],
/*
* An array of automatically applied loaders.
*
* IMPORTANT: The loaders here are resolved relative to the resource which they are applied to.
* This means they are not resolved relative to the configuration file.
*
* See: http://webpack.github.io/docs/configuration.html#module-loaders
*/
loaders: [
// JS LOADER
// Reference: https://github.com/babel/babel-loader
// Compiles ES6 and ES7 into ES5 code
{
test: /\.js$/, loaders: ['babel', 'eslint-loader'], exclude: /node_modules/
},
/*
* Json loader support for *.json files.
*
* See: https://github.com/webpack/json-loader
*/
{
test: /\.json$/,
loader: 'json-loader'
},
/*
* Reference https://github.com/webpack/less-loader
*/
{
test: /\.less$/,
exclude: helpers.root('./src/css/main.less'),
loader: ExtractTextPlugin.extract('css!postcss!less')
},
//{test: /\.less$/, loader: extractLESS.extract(['css', 'postcss!less'])},
//all css required in src/app files will be merged in js files
{
test: /\.less/,
include: helpers.root('./src/css/main.less'),
loader: 'style!css!postcss!less'
},
/*
* to string and css loader support for *.css files
* Returns file content as string
*
*/
// {
// test : /\.css$/,
// loaders: ['to-string-loader', 'css-loader']
// },
// support for .html as raw text
// todo: change the loader to something that adds a hash to images
//{test: /\.html$/, loader: 'raw'},
// HTML LOADER
// Reference: https://github.com/webpack/html-loader
// Allow loading html through js
{test: /\.html$/, loader: 'html?root=/&attrs=img:src img:data-src link:href'},
// FILE LOADER
// Reference: https://github.com/webpack/file-loader
// Copy resource files to output
{
test: /\.(png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$/i,
loader: 'file?name=images/[name].[ext]?[hash]'
},
]
},
/**
* Plugins
* Reference: http://webpack.github.io/docs/configuration.html#plugins
* List: http://webpack.github.io/docs/list-of-plugins.html
*/
plugins: [
//vendor
new webpack.optimize.CommonsChunkPlugin({
name: 'commons.chunk',
chunks: ['app']
}),
new webpack.optimize.CommonsChunkPlugin('vendor', isProd ? 'vendor.[hash:8].js' : 'vendor.bundle.js'),
// new webpack.DllPlugin({
// path : 'manifest.json',
// name : "[name]_[hash:8]",
// context: helpers.root(".")
// }),
// Reference: https://github.com/ampedandwired/html-webpack-plugin
// Render index.html
new HtmlWebpackPlugin({
template: helpers.root('./src/index.html'),
//inject : 'body',
chunks: ['commons.chunk', 'vendor', 'app'],
chunksSortMode: 'dependency'
}),
new HtmlWebpackPlugin({
filename: 'app.other.html',
template: helpers.root('./src/index.html'),
//inject : 'body',
chunks: ['commons.chunk.js', 'vendor'],
chunksSortMode: 'dependency'
}),
// Reference: https://github.com/webpack/extract-text-webpack-plugin
// Extract css files
new ExtractTextPlugin(isProd ? '[name].[hash:8].css' : '[name].css'),
//new RenamePlugin()
],
/**
* PostCSS
* Reference: https://github.com/postcss/autoprefixer
*/
postcss: [
autoprefixer({
browsers: ['last 2 version']
})
],
/**
* Dev server configuration
* Reference: http://webpack.github.io/docs/configuration.html#devserver
* Reference: http://webpack.github.io/docs/webpack-dev-server.html
*/
devServer: {
// Base path for the serve content.
contentBase: 'src',
// Minimize output infomation
//stats : 'minimal',
// Server port
colors: true,
historyApiFallback: true,
port: 7070
},
resolve: {
alias: {
_components: path.resolve(APP_PATH, 'components'),
_commonComponents: path.resolve(APP_PATH, 'commonComponents'),
_config: path.resolve(APP_PATH, 'config'),
_assets: path.resolve(APP_PATH, 'assets'),
_server: path.resolve(APP_PATH, 'server')
}
}
};
if (isProd) {
config.plugins.push(
//http://npm.taobao.org/package/clean-webpack-plugin
new CleanWebpackPlugin(['dist']),
// Reference: http://webpack.github.io/docs/list-of-plugins.html#noerrorsplugin
// Only emit files when there are no errors
new webpack.NoErrorsPlugin(),
// Reference: http://webpack.github.io/docs/list-of-plugins.html#dedupeplugin
// Dedupe modules in the output
new webpack.optimize.DedupePlugin(),
// Reference: http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin
// Minify all javascript, switch loaders to minimizing mode
new webpack.optimize.UglifyJsPlugin()
);
}
if (isProd) {
//config.devtool = 'source-map';
} else {
config.devtool = 'source-map';
}
// add debug messages
config.debug = !isProd || !isTest;
return config;
}();