-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
164 lines (154 loc) · 3.93 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
const fs = require('fs')
const path = require('path')
const merge = require('webpack-merge')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
/* -----------------------------------------------------------------------------
* helpers
*/
function readFileJSON (filepath) {
return JSON.parse(fs.readFileSync(filepath, { encoding: 'utf8' }))
}
function resolve (dir) {
return path.resolve(__dirname, dir)
}
/* -----------------------------------------------------------------------------
* configs
*/
const parts = require('./webpack.parts')
const pkg = readFileJSON('package.json')
const babelrc = readFileJSON('\.babelrc')
Object.assign(babelrc, { cacheDirectory: '.tmp/babel' })
const PATHS = {
source: resolve('src'),
test: resolve('test'),
public: resolve('dist')
}
/* -----------------------------------------------------------------------------
* common webpack configuration
*/
const commonConfig = merge([
{
entry: {
vew: './src/index.js'
},
output: {
path: PATHS.public,
filename: '[name].js',
},
resolve: {
extensions: ['.js'],
},
module: {
rules: [
{
test: /\.js$/,
include: [ PATHS.source, PATHS.test ],
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: babelrc
}
}
]
},
plugins: []
}
])
/* -----------------------------------------------------------------------------
* development webpack configuration
*/
const TerserPlugin = require('terser-webpack-plugin')
const developmentConfig = merge([
{
mode: 'development',
entry: {
vew: path.join(PATHS.test, 'repl.js')
},
devtool: 'source-map', // 'eval', 'source-map', 'nosources-source-map'
plugins: [
new HtmlWebpackPlugin({
title: 'Vew Playground',
template: path.join(PATHS.test, 'repl.ejs')
}),
new webpack.NamedModulesPlugin()
],
optimization: {
minimize: false,
minimizer: [
new TerserPlugin({
cache: '.tmp/terser-webpack-plugin/',
terserOptions: {
compress: {
sequences: false,
pure_funcs: ['isNative'],
toplevel: true,
top_retain: ['extend', 'create'],
passes: 2
},
output: {
ecma: 5,
beautify: true,
ascii_only: true,
indent_level: 2,
max_line_len: 8196,
keep_quoted_props: true,
},
mangle: {
reserved: ['extend', 'create']
},
ie8: true,
safari10: true,
}
})
],
usedExports: true,
concatenateModules: true,
}
},
parts.cssLoader(),
parts.devServer({ host: process.env.HOST, port: process.env.PORT })
])
/* -----------------------------------------------------------------------------
* production webpack configuration
*/
const productionConfig = merge([
{
mode: 'production',
entry: {
runtime: path.join(PATHS.source, 'component.js'),
buildtime: path.join(PATHS.source, 'template.js')
},
output: {
filename: '[name].min.js',
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
cache: '.tmp/terser-webpack-plugin/',
terserOptions: {
compress: {
sequences: false,
},
output: {
ecma: 5,
ascii_only: true,
max_line_len: 8196,
keep_quoted_props: true,
},
ie8: true,
safari10: true,
}
})
]
}
}
])
module.exports = function(env) {
process.env.BABEL_ENV=env
switch (env) {
case 'development': return merge(commonConfig, developmentConfig)
case 'production' : return merge(commonConfig, productionConfig)
}
}