forked from cdnbye/hlsjs-p2p-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
183 lines (165 loc) · 5.21 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
const pkgJson = require('./package.json');
const path = require('path');
const webpack = require('webpack');
const fs= require('fs');
const uglifyJsOptions = {
screwIE8: true,
stats: true,
compress: {
warnings: false,
// drop_debugger: true,
// drop_console: true
},
mangle: {
toplevel: true,
eval: true
},
output: {
comments: false, // remove all comments
// preamble: "Email <86755838@qq.com>*/"
}
};
const prodCorePath = 'cdnbye-core';
const devCorePath = path.resolve(__dirname, 'src/core');
const commonConfig = {
module: {
rules: [
{
test: /\.js$/,
exclude: [
path.resolve(__dirname, 'node_modules')
],
use: {
loader: 'babel-loader',
}
}
]
},
resolve: {
extensions: ['.js'],
alias: {
core: fs.existsSync(devCorePath) ? devCorePath : prodCorePath
}
}
};
function getPluginsForConfig(minify = false, light = false) {
// common plugins.
const plugins = [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.DefinePlugin(getConstantsForConfig(light))
];
if (minify) {
// minification plugins.
return plugins.concat([
new webpack.optimize.UglifyJsPlugin(uglifyJsOptions),
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: true
})
]);
}
return plugins;
}
function getConstantsForConfig(light = false) { //嵌入全局变量
return {
__VERSION__: JSON.stringify(pkgJson.version),
__IS_HLSJS_LIGHT__: light,
};
}
function getAliasesForLightDist() {
let aliases = {};
aliases = Object.assign({}, aliases, {
'./bittorrent': './empty.js'
});
return aliases;
}
const multiConfig = [
{
name: 'bundle-engine',
entry: './src/index.engine.js',
output: {
filename: './hlsjs-p2p-engine.js',
sourceMapFilename: './hlsjs-p2p-engine.js.map',
path: path.resolve(__dirname, 'dist'),
// publicPath: '/src/',
library: ['P2PEngine'],
libraryTarget: 'umd'
},
plugins: getPluginsForConfig(),
devtool: 'source-map',
},
{
name: 'bundle-hlsjs',
entry: './src/index.hls.js',
output: {
filename: 'hls.js',
sourceMapFilename: './hls.js.map',
path: path.resolve(__dirname, 'dist'),
library: ['Hls'],
libraryTarget: 'umd'
},
plugins: getPluginsForConfig(),
devtool: 'source-map',
},
{
name: 'release-engine',
entry: './src/index.engine.js',
output: {
filename: 'hlsjs-p2p-engine.min.js',
path: path.resolve(__dirname, 'dist'),
// publicPath: '/src/',
library: ['P2PEngine'],
libraryTarget: 'umd'
},
plugins: getPluginsForConfig(true)
},
{
name: 'release-hlsjs',
entry: './src/index.hls.js',
output: {
filename: 'hls.min.js',
path: path.resolve(__dirname, 'dist'),
library: ['Hls'],
libraryTarget: 'umd'
},
plugins: getPluginsForConfig(true, false)
},
{
name: 'release-hlsjs-light',
entry: './src/index.hls.js',
output: {
filename: 'hls.light.min.js',
path: path.resolve(__dirname, 'dist'),
library: ['Hls'],
libraryTarget: 'umd'
},
plugins: getPluginsForConfig(true, true)
},
//test
{
name: 'test-bundle',
entry: './test/bundle/index.bundle.js',
output: {
filename: 'test-bundle.js',
// sourceMapFilename: 'hls-peerify-bundle.js.map',
path: path.resolve(__dirname, 'test/bundle')
},
plugins: getPluginsForConfig()
},
].map(fragment => Object.assign({}, commonConfig, fragment));
// webpack matches the --env arguments to a string; for example, --env.debug.min translates to { debug: true, min: true }
module.exports = (envArgs) => {
if (!envArgs) {
// If no arguments are specified, return every configuration
return multiConfig;
}
// Find the first enabled config within the arguments array
const enabledConfigName = Object.keys(envArgs).find(envName => envArgs[envName]);
let enabledConfig = multiConfig.find(config => config.name === enabledConfigName);
if (!enabledConfig) {
console.error(`Couldn't find a valid config with the name "${enabledConfigName}". Known configs are: ${multiConfig.map(config => config.name).join(', ')}`);
return;
}
return enabledConfig;
};