-
Notifications
You must be signed in to change notification settings - Fork 108
/
webpack.config.js
157 lines (125 loc) · 3.38 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
'use strict';
const path = require('path');
const glob = require('glob');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const packageJSON = require('./package.json');
const getBanner = () =>
packageJSON.name + '\n' +
packageJSON.homepage + '\n' +
'Version: ' + packageJSON.version + ' -- ' + (new Date()).toISOString() + '\n' +
'License: ' + packageJSON.license;
const scriptName = process.env.npm_lifecycle_event;
const ENV = scriptName.indexOf('dev') === 0 ? 'development' : 'production';
const isTest = scriptName.indexOf('test') >= 0;
console.log('***** webpack runs in ' + ENV + (isTest ? ' (test)' : '') + ' environment\n');
const devServerPort = 5005;
const devServerHost = 'localhost';
let configEnv;
if (ENV === 'development') {
configEnv = {
entry: isTest ? ({
'test': glob.sync(path.resolve(__dirname, 'test/*.js'))
}) : ({}),
output: {
filename: '[name].js',
publicPath: '/'
},
devtool: 'inline-source-map',
plugins: [],
optimization: {},
devServer: !isTest ? {
proxy: {
'/dist': {
target: 'http://' + devServerHost + ':' + devServerPort,
pathRewrite: { '^/dist': '' }
}
},
port: devServerPort,
host: devServerHost,
static: "demo",
devMiddleware: {
stats: {
modules: false,
errors: true,
warnings: true
},
publicPath: '/'
},
} : {},
watch: true
}
}
if (ENV === 'production') {
configEnv = {
entry: {
'ui-scroll.min': path.resolve(__dirname, 'src/ui-scroll.js'),
'ui-scroll-grid.min': path.resolve(__dirname, 'src/ui-scroll-grid.js')
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
devtool: 'source-map',
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
parallel: true,
extractComments: false,
terserOptions: {
sourceMap: true,
warnings: true,
compress: {
warnings: true,
},
output: {
comments: false,
},
},
include: /\.min\.js$/
})
],
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [path.join(__dirname, 'dist/**/*')]
}),
new CopyWebpackPlugin({
patterns: [
{ from: 'src/ui-scroll-jqlite.js', to: 'ui-scroll-jqlite.min.js' },
{ from: 'src/ui-scroll-jqlite.js', to: 'ui-scroll-jqlite.js' }
]
}),
new webpack.BannerPlugin(getBanner())
],
devServer: {},
watch: false
}
}
module.exports = {
entry: Object.assign({
'ui-scroll': path.resolve(__dirname, 'src/ui-scroll.js'),
'ui-scroll-grid': path.resolve(__dirname, 'src/ui-scroll-grid.js')
}, configEnv.entry),
output: configEnv.output,
cache: false,
devtool: configEnv.devtool,
mode: ENV,
target: ['web', 'es5'],
optimization: configEnv.optimization,
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
},
plugins: configEnv.plugins,
devServer: configEnv.devServer,
watch: configEnv.watch
};