forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
240 lines (226 loc) · 6.73 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
230
231
232
233
234
235
236
237
238
239
240
const pkgJson = require('./package.json');
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const importHelper = require('@babel/helper-module-imports');
/* 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 createDefinePlugin = (type) => {
const buildConstants = {
__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)
};
return new webpack.DefinePlugin(buildConstants);
};
const basePlugins = [
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.BannerPlugin({ entryOnly: true, raw: true, banner: 'typeof window !== "undefined" &&' }) // SSR/Node.js guard
];
const mainPlugins = [...basePlugins, createDefinePlugin('main')];
const lightPlugins = [...basePlugins, createDefinePlugin('light')];
const baseConfig = {
mode: 'development',
entry: './src/hls',
optimization: {
splitChunks: false
},
resolve: {
// Add `.ts` as a resolvable extension.
extensions: ['.ts', '.js']
},
module: {
strictExportPresence: true,
rules: [
{
test: /\.(ts|js)$/,
exclude: [
path.resolve(__dirname, 'node_modules')
],
loader: 'babel-loader',
options: {
babelrc: false,
presets: [
'@babel/preset-typescript',
['@babel/preset-env', {
loose: true,
modules: false,
targets: {
browsers: [
'chrome >= 47',
'firefox >= 51',
'ie >= 11',
'safari >= 8',
'ios >= 8',
'android >= 4'
]
}
}]
],
plugins: [
['@babel/plugin-proposal-class-properties', {
loose: true
}],
'@babel/plugin-proposal-object-rest-spread',
{
visitor: {
CallExpression: function (espath) {
if (espath.get('callee').matchesPattern('Number.isFinite')) {
espath.node.callee = importHelper.addNamed(espath, 'isFiniteNumber', path.resolve('src/polyfills/number-isFinite'));
}
}
}
}
]
}
}
]
},
node: {
global: false,
process: false,
__filename: false,
__dirname: false,
Buffer: false,
setImmediate: false
}
};
function getAliasesForLightDist () {
let aliases = {};
if (!addEMESupport) {
aliases = Object.assign({}, aliases, {
'./controller/eme-controller': './empty.js'
});
}
if (!addSubtitleSupport) {
aliases = Object.assign(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 = Object.assign(aliases, {
'./controller/audio-track-controller': './empty.js',
'./controller/audio-stream-controller': './empty.js'
});
}
return aliases;
}
const multiConfig = [
{
name: 'debug',
mode: 'development',
output: {
filename: 'hls.js',
chunkFilename: '[name].js',
sourceMapFilename: 'hls.js.map',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
library: 'Hls',
libraryTarget: 'umd',
libraryExport: 'default',
globalObject: 'this' // https://github.com/webpack/webpack/issues/6642#issuecomment-370222543
},
plugins: mainPlugins,
devtool: 'source-map'
},
{
name: 'dist',
mode: 'production',
output: {
filename: 'hls.min.js',
chunkFilename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
library: 'Hls',
libraryTarget: 'umd',
libraryExport: 'default',
globalObject: 'this'
},
plugins: mainPlugins,
devtool: 'source-map'
},
{
name: 'light',
mode: 'development',
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',
globalObject: 'this'
},
resolve: {
alias: getAliasesForLightDist()
},
plugins: lightPlugins,
devtool: 'source-map'
},
{
name: 'light-dist',
mode: 'production',
output: {
filename: 'hls.light.min.js',
chunkFilename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
library: 'Hls',
libraryTarget: 'umd',
libraryExport: 'default',
globalObject: 'this'
},
resolve: {
alias: getAliasesForLightDist()
},
plugins: lightPlugins,
devtool: 'source-map'
},
{
name: 'demo',
entry: './demo/main',
mode: 'development',
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',
globalObject: 'this' // https://github.com/webpack/webpack/issues/6642#issuecomment-370222543
},
plugins: mainPlugins,
devtool: 'source-map'
}
].map(config => merge(baseConfig, config));
// webpack matches the --env arguments to a string; for example, --env.debug.min translates to { debug: true, min: true }
module.exports = (envArgs) => {
let configs;
if (!envArgs) {
// If no arguments are specified, return every configuration
configs = multiConfig;
} else {
// 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) {
throw new Error(`Couldn't find a valid config with the name "${enabledConfigName}". Known configs are: ${multiConfig.map(config => config.name).join(', ')}`);
}
configs = [enabledConfig];
}
console.log(`Building configs: ${configs.map(config => config.name).join(', ')}.\n`);
return configs;
};