-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.babel.js
139 lines (133 loc) · 3.21 KB
/
webpack.config.babel.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
/**
* Module dependencies.
* @private
*/
import path from 'path';
import webpack from 'webpack';
import cssnano from 'cssnano';
import autoprefixer from 'autoprefixer';
import HtmlPlugin from 'html-webpack-plugin';
import ExtractText from 'extract-text-webpack-plugin';
import OptimizeCssAssetsPlugin from 'optimize-css-assets-webpack-plugin';
import config from './package.json';
/**
* Environment constants.
* @private
*/
const ENV_DEV = 'dev';
const ENV_PROD = 'prod';
/**
* Module constants.
* @private
*/
const env = process.argv.indexOf('-p') === -1 ? ENV_DEV : ENV_PROD;
const srcDir = path.resolve(__dirname, 'src');
const distDir = path.resolve(__dirname, 'docs');
const extractLess = new ExtractText({filename: 'assets/[name].css'});
/**
* Pages.
* @type {[*]}
* @constant
* @private
*/
const pages = [{
title: [config.name, 'version', config.version].join(' '),
filename: 'index.html',
template: 'templates/index.ejs',
favicon: 'styles/favicon.ico',
styles: ['assets/index.css'],
scripts: ['assets/index.js']
}];
/**
* Minify HTML configuration
* @type {Object}
* @constant
* @private
*/
const minifyHtmlOptions = {
collapseWhitespace: true,
html5: true
};
/**
* Production plugins
* @type {Array}
* @constant
* @private
*/
const prodPlugins = [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console: true,
unsafe: true
}
}),
new OptimizeCssAssetsPlugin({
assetNameRegExp: /.*\.css$/g,
cssProcessor: cssnano,
cssProcessorOptions: {discardComments: {removeAll: true}},
canPrint: true
})
];
/**
* Webpack configuration
* @module WebpackConfigBabel
*/
export default {
devtool: env === ENV_DEV ? 'source-map' : false,
context: srcDir,
entry: {
app: './index.js'
},
output: {
filename: 'assets/[name].js',
path: distDir,
publicPath: '/'
},
module: {
rules: [
{
test: /\.less$/,
exclude: /node_modules/,
use: extractLess.extract({
use: [
{loader: 'css-loader'},
{loader: 'less-loader'}
],
fallback: 'style-loader'
})
},
{
test: /\.js$/,
exclude: /node_modules/,
use: [
'babel-loader'
]
},
{
test: /\.(png|jpg|svg|ttf|eot|woff|woff2)$/,
exclude: /node_modules/,
use: [{
loader: 'url-loader',
options: {
limit: 10000,
name: 'assets/[hash:8].[ext]'
}
}]
}
]
},
plugins: [
...pages.map(page => new HtmlPlugin({
...page,
minify: env === ENV_PROD ? minifyHtmlOptions : false,
inject: false
})),
extractLess,
autoprefixer,
...(env === ENV_PROD ? prodPlugins : [])
],
resolve: {
extensions: ['.js', '.json']
}
};