forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
229 lines (203 loc) · 5.83 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const pkgJson = require('./package.json');
const path = require('path');
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const clone = (...args) => Object.assign({}, ...args);
/* Allow to customise builds through env-vars */
const env = process.env;
const addSubtitleSupport = !!env.SUBTITLE || !!env.USE_SUBTITLES ;
const addAltAudioSupport = !!env.ALT_AUDIO || !!env.USE_ALT_AUDIO;
const addEMESupport = !!env.EME_DRM || !!env.USE_EME_DRM;
const runAnalyzer = !!env.ANALYZE;
const uglifyJsOptions = {
screwIE8: true,
stats: true,
compress: {
warnings: false
},
mangle: {
toplevel: true,
eval: true
},
sourceMap: true
};
const baseConfig = {
entry: './src/hls.js',
module: {
rules: [
{
test: /\.js$/,
exclude: [
path.resolve(__dirname, 'node_modules')
],
use: {
loader: 'babel-loader'
}
}
]
}
};
const demoConfig = clone(baseConfig, {
name: 'demo',
entry: './demo/main',
output: {
filename: 'hls-demo.js',
chunkFilename: '[name].js',
sourceMapFilename: 'hls-demo.js.map',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
library: 'HlsDemo',
libraryTarget: 'umd',
libraryExport: 'default'
},
plugins: [],
devtool: 'source-map'
});
function getPluginsForConfig(type, minify = false) {
// common plugins.
const defineConstants = getConstantsForConfig(type);
console.log(
`Building <${ minify ? 'minified' : 'non-minified / debug' }> distro-type "${type}" with compile-time defined constants:`,
JSON.stringify(defineConstants, null, 4),
'\n'
);
const plugins = [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.DefinePlugin(defineConstants)
];
if (minify) {
// minification plugins.
return plugins.concat([
new webpack.optimize.UglifyJsPlugin(uglifyJsOptions),
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
})
]);
}
if (runAnalyzer && !minify) {
plugins.push(new BundleAnalyzerPlugin({
analyzerMode: 'static',
reportFilename: `bundle-analyzer-report.${type}.html`
}));
} else {
// https://github.com/webpack-contrib/webpack-bundle-analyzer/issues/115
plugins.push(new webpack.optimize.ModuleConcatenationPlugin())
}
return plugins;
}
function getConstantsForConfig(type) {
// By default the "main" dists (hls.js & hls.min.js) are full-featured.
return {
__VERSION__: JSON.stringify(pkgJson.version),
__USE_SUBTITLES__: JSON.stringify(type === 'main' || addSubtitleSupport),
__USE_ALT_AUDIO__: JSON.stringify(type === 'main' || addAltAudioSupport),
__USE_EME_DRM__: JSON.stringify(type === 'main' || addEMESupport)
};
}
function getAliasesForLightDist() {
let aliases = {};
if (!addEMESupport) {
aliases = Object.assign({}, aliases, {
'./controller/eme-controller': './empty.js'
});
}
if (!addSubtitleSupport) {
aliases = clone(aliases, {
'./utils/cues': './empty.js',
'./controller/timeline-controller': './empty.js',
'./controller/subtitle-track-controller': './empty.js',
'./controller/subtitle-stream-controller': './empty.js'
});
}
if (!addAltAudioSupport) {
aliases = clone(aliases, {
'./controller/audio-track-controller': './empty.js',
'./controller/audio-stream-controller': './empty.js'
});
}
return aliases;
}
const multiConfig = [
{
name: 'debug',
output: {
filename: 'hls.js',
chunkFilename: '[name].js',
sourceMapFilename: 'hls.js.map',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
library: 'Hls',
libraryTarget: 'umd',
libraryExport: 'default'
},
plugins: getPluginsForConfig('main'),
devtool: 'source-map',
},
{
name: 'dist',
output: {
filename: 'hls.min.js',
chunkFilename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
library: 'Hls',
libraryTarget: 'umd',
libraryExport: 'default'
},
plugins: getPluginsForConfig('main', true),
devtool: 'source-map'
},
{
name: 'light',
output: {
filename: 'hls.light.js',
chunkFilename: '[name].js',
sourceMapFilename: 'hls.light.js.map',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
library: 'Hls',
libraryTarget: 'umd',
libraryExport: 'default'
},
resolve: {
alias: getAliasesForLightDist()
},
plugins: getPluginsForConfig('light'),
devtool: 'source-map'
},
{
name: 'light-dist',
output: {
filename: 'hls.light.min.js',
chunkFilename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
library: 'Hls',
libraryTarget: 'umd',
libraryExport: 'default'
},
resolve: {
alias: getAliasesForLightDist()
},
plugins: getPluginsForConfig('light', true),
devtool: 'source-map'
}
].map(config => clone(baseConfig, config));
multiConfig.push(demoConfig);
// 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]);
// Filter out config with name
const 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, demoConfig];
};