forked from fluid-player/fluid-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
148 lines (135 loc) · 4.96 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
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const semver = require('semver');
const cheerio = require('cheerio');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin');
// Loading the current package.json - will be used to determine version etc.
const packageJSON = require(path.resolve(__dirname, 'package.json'));
// Validate package version is valid semver
if (!semver.valid(packageJSON.version)) {
throw 'Invalid package version - ' + packageJSON.version;
}
// Distribution options configure how build paths are going to be configured.
const getDistOptions = (mode) => {
const fullVersion = packageJSON.version;
const majorVersion = semver.major(packageJSON.version);
const cdnRoot = packageJSON.com_fluidplayer.cdn;
switch (mode) {
case 'development':
return {
path: path.resolve(__dirname, 'dist'),
publicPath: '/'
};
case 'current':
return {
path: path.resolve(__dirname, 'dist-cdn/v' + majorVersion + '/current/'),
publicPath: cdnRoot + '/v' + majorVersion + '/current/'
};
case 'versioned':
return {
path: path.resolve(__dirname, 'dist-cdn/' + fullVersion + '/'),
publicPath: cdnRoot + '/' + fullVersion + '/'
};
default:
throw 'Unknown distribution type provided in --dist!';
}
}
// Webpack configuration
module.exports = (env, argv) => {
const wpMode = typeof argv.mode !== 'undefined' ? argv.mode : 'development';
const wpDebug = wpMode === 'development' && typeof argv.debug !== 'undefined' && !!argv.debug;
const wpDist = typeof argv.dist !== 'undefined' ? argv.dist : 'development';
const wpDistOptions = getDistOptions(wpDist);
if ('development' !== wpDist && (wpMode !== 'production' || wpDebug)) {
throw 'Building a production distribution in development mode or with debug enabled is not allowed!'
}
const plugins = [
// Define common variables for use in Fluid Player
new webpack.DefinePlugin({
FP_BUILD_VERSION: JSON.stringify(packageJSON.version),
FP_HOMEPAGE: JSON.stringify(packageJSON.homepage),
FP_ENV: JSON.stringify(wpMode),
FP_DEBUG: JSON.stringify(wpDebug),
FP_WITH_CSS: false
})
];
// Development mode builds and development server specifics
if ('development' === wpMode) {
// Locate all E2E cases
const caseFiles = [];
fs.readdirSync(path.resolve(__dirname, 'test/html/')).forEach(file => {
const absPath = path.resolve(__dirname, 'test/html/', file);
const caseHtml = cheerio.load(fs.readFileSync(absPath));
const publicName = file.replace('.tpl', '');
plugins.push(new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'test/html/', file),
inject: false,
filename: publicName
}));
caseFiles.push({
file: publicName,
name: caseHtml('title').text()
});
});
// Emit all cases as separate HTML pages
plugins.push(new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'test/index.html'),
filename: 'index.html',
inject: false,
templateParameters: {
cases: caseFiles
}
}));
// Copy static assets for E2E
plugins.push(new CopyPlugin(
[
{from: path.resolve(__dirname, 'test/static/'), to: path.resolve(wpDistOptions.path, 'static')}
]
));
}
return {
devServer: {
contentBase: wpDistOptions.path,
index: 'index.html',
watchContentBase: true
},
devtool: wpMode === 'development' ? 'source-map' : false,
plugins,
entry: {
fluidplayer: './src/browser.js'
},
optimization: {
minimize: wpMode !== 'development'
},
output: {
filename: '[name].min.js',
chunkFilename: '[name].[chunkhash].min.js',
path: wpDistOptions.path,
publicPath: wpDistOptions.publicPath
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.svg$/,
loader: 'svg-url-loader'
}
],
}
};
}