-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.config.js
206 lines (192 loc) · 5.84 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
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');
const TerserPlugin = require('terser-webpack-plugin');
const WebpackObfuscatorPlugin = require('webpack-obfuscator');
// Loading the current package.json - will be used to determine version etc.
const packageJSON = require(path.resolve(__dirname, 'package.json'));
// Loading cdn
const cdnJSON = require(path.resolve(__dirname, 'deploy.json'));
// Validate package version is valid semver
if (!semver.valid(packageJSON.version)) {
throw new Error('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 = cdnJSON.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 new Error('Unknown distribution type provided in --dist!');
}
};
// Webpack configuration
module.exports = (env, argv) => {
const mode = typeof argv.mode !== 'undefined' ? argv.mode : 'development';
const debug = mode === 'development' && !!env.debug;
const dist = typeof env.dist !== 'undefined' ? env.dist : 'development';
const distOptions = getDistOptions(dist);
const obf = !!env.obf;
if (dist !== 'development' && (mode !== 'production' || debug)) {
throw new Error('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(mode),
FP_DEBUG: JSON.stringify(debug),
}),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1, // disable creating additional chunks
}),
];
// Development mode builds and development server specifics
if (mode === 'development') {
// 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,
scriptLoading: 'blocking',
}),
);
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({
patterns: [
{
from: path.resolve(__dirname, 'test/static/'),
to: path.resolve(distOptions.path, 'static'),
},
],
}),
);
}
if (obf) {
plugins.push(
new WebpackObfuscatorPlugin({
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: false,
identifierNamesGenerator: 'mangled-shuffled',
log: false,
numbersToExpressions: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: false,
shuffleStringArray: true,
simplify: true,
splitStrings: false,
stringArray: true,
stringArrayEncoding: [],
stringArrayIndexShift: true,
stringArrayWrappersCount: 1,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersParametersMaxCount: 2,
stringArrayWrappersType: 'variable',
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false,
}),
);
}
return {
resolve: {
fallback: {
buffer: false,
stream: false,
fs: false,
},
},
mode: mode,
devServer: {
static: distOptions.path,
},
devtool: mode === 'development' ? 'source-map' : false,
plugins,
entry: {
player: './src/browser.js',
},
optimization: {
minimize: mode !== 'development',
splitChunks: false,
minimizer: [new TerserPlugin()],
},
output: {
filename: '[name].min.js',
chunkFilename: '[name].[chunkhash].min.js',
path: distOptions.path,
publicPath: distOptions.publicPath,
clean: true,
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
cacheDirectory: true,
},
},
},
{
test: /\.scss$/i,
use: ['style-loader', 'css-loader', 'sass-loader'],
},
{
test: /\.svg$/,
type: 'asset',
},
],
},
};
};