-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
121 lines (108 loc) · 2.75 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
var Clean = require('clean-webpack-plugin');
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var config = {
context: path.join(__dirname, '/app'),
devtool: 'eval', //'source-map',
entry: {
common: [
'angular',
'angular-ui-router',
'oclazyload',
'./index.js'
],
user: './components/user',
news: './components/news'
},
output: {
path: path.join(__dirname, '/dev'),
filename: '[hash].[name].js'
},
plugins: [
new webpack.DefinePlugin({
/*eslint-disable */
ON_DEV: process.env.NODE_ENV === 'development',
ON_PROD: process.env.NODE_ENV === 'production',
ON_TEST: process.env.NODE_ENV === 'test'
/*eslint-enable */
}),
new webpack.optimize.CommonsChunkPlugin('common', '[hash].common.js')
],
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/
},
{
test: /\.js$/,
loader: 'eslint',
exclude: /node_modules/
},
{
test: /\.html$/,
loader: 'raw',
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style!css',
exclude: /node_modules/
},
{
test: /\.(png|jpg)$/,
loader: 'url-loader?limit=8192',
exclude: /node_modules/
}
]
}
};
if (process.env.NODE_ENV === 'production') {
delete config.devtool;
config.output.path = path.join(__dirname, '/build');
config.plugins.push(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}));
build('build');
}
if (process.env.NODE_ENV === 'development') {
build('dev');
}
if (process.env.NODE_ENV === 'test') {
var data = '';
for (var key in config.entry) {
if (config.entry.hasOwnProperty(key)) {
var val = config.entry[key];
if (typeof val === 'object') {
val.forEach(function (e) {
data += 'require(\'' + e + '\');\n\n'
})
}
if (typeof val === 'string') {
data += 'require(\'' + val + '\');\n\n'
}
}
}
fs.writeFileSync('./app/test.js', data);
config.plugins.splice(1, 1);
config.entry = './test.js';
}
function build(dir) {
config.plugins.push(new Clean([dir]));
config.plugins.push(function () {
this.plugin('done', function (stats) {
fs.writeFileSync(dir + '/index.html', fs.readFileSync('app/index.html'));
fs.writeFileSync(dir + '/news.json', fs.readFileSync('app/news.json'));
var htmlPath = path.join(__dirname, dir, 'index.html');
var template = fs.readFileSync(htmlPath, 'utf8');
var html = _.template(template)({hash: stats.hash + '.'});
fs.writeFile(htmlPath, html);
});
});
}
module.exports = config;