-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
webpack.config.js
227 lines (208 loc) · 6.69 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* eslint-env node */
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const findConfigFromClosestPackageJson = require('./bin/findDependentConfig');
const TagFilterPlugin = require('./bin/TagFilterPlugin');
const config = getConfig();
const includedModules = parseConfig(config);
if (includedModules) {
// eslint-disable-next-line no-console
console.log(
'[INFO] Building custom bundle from this config: '
+ (config.include ? `including ${JSON.stringify(config.include)}` : '')
+ (config.exclude ? `excluding ${JSON.stringify(config.exclude)}` : '')
+ '\n'
);
}
module.exports = {
mode: process.env.NODE_ENV || 'production',
optimization: {
minimizer: [new TerserPlugin({
terserOptions: {
compress: {
arguments: true,
booleans_as_integers: true,
passes: 3,
typeofs: false, // IE10 needs typeofs to be false.
unsafe: true,
},
sourceMap: true,
}
})]
},
entry: {
'exif-reader': path.resolve('./src/exif-reader.js')
},
output: {
library: {
name: 'ExifReader',
type: 'umd'
},
globalObject: 'typeof self !== \'undefined\' ? self : this'
},
devtool: 'source-map',
devServer: {
static: [
{
directory: path.join(__dirname, 'examples'),
publicPath: '/',
watch: !process.env.CI
},
{
directory: path.join(__dirname, 'src'),
publicPath: '/src',
watch: !process.env.CI
}
],
server: 'https',
open: !process.env.CI,
liveReload: !process.env.CI
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules(?!.exifreader)/,
use: {
loader: 'babel-loader',
options: {
plugins: includedModules ? [
[TagFilterPlugin, {include: includedModules}]
] : []
}
}
},
{
test: /[/\\](exif-reader|image-header-?(tiff|jpeg|png|heic|avif|iso-bmff|webp|gif)?|tags|tag-names|tags-helpers|png-text-tags)\.js$/,
loader: 'string-replace-loader',
options: {
multiple: getConstantReplacements(includedModules)
}
}
]
}
};
function parseConfig({include: includesConfig, exclude: excludesConfig}) {
const modules = [
'file',
'jfif',
'png_file',
'exif',
'iptc',
'xmp',
'icc',
'photoshop',
'maker_notes',
'mpf',
'thumbnail',
'tiff',
'jpeg',
'png',
'heic',
'avif',
'webp',
'gif'
];
if (includesConfig) {
const includes = {};
for (const module of modules) {
includes[module] =
(
Object.keys(includesConfig).includes(module)
|| ((module === 'exif') && Object.keys(includesConfig).includes('thumbnail'))
|| ((module === 'exif') && Object.keys(includesConfig).includes('mpf'))
)
&& includesConfig[module];
}
return includes;
}
if (excludesConfig) {
const includes = {};
for (const module of modules) {
includes[module] =
!(
excludesConfig.includes(module)
|| ((module === 'thumbnail') && excludesConfig.includes('exif'))
|| ((module === 'mpf') && excludesConfig.includes('exif'))
);
}
return includes;
}
return false;
}
function getConfig() {
const packageJson = getPackageJson();
if (packageJson && packageJson.include) {
if (Array.isArray(packageJson.include.exif)) {
// Mandatory tags that are needed to be able to find the rest of them.
packageJson.include.exif.push('Exif IFD Pointer');
if (includesGpsTag(packageJson.include.exif)) {
packageJson.include.exif.push('GPS Info IFD Pointer');
}
if (includesInteroperabilityTag(packageJson.include.exif)) {
packageJson.include.exif.push('Interoperability IFD Pointer');
}
if (packageJson.include.iptc) {
packageJson.include.exif.push('IPTC-NAA');
}
if (packageJson.include.xmp) {
packageJson.include.exif.push('ApplicationNotes');
}
if (packageJson.include.icc) {
packageJson.include.exif.push('ICC_Profile');
}
if (packageJson.include.thumbnail) {
packageJson.include.exif.push('JPEGInterchangeFormat', 'JPEGInterchangeFormatLength');
}
if (packageJson.include.maker_notes) {
packageJson.include.exif.push('MakerNote', 'Make');
}
}
return {include: packageJson.include};
}
if (packageJson && packageJson.exclude) {
return {exclude: getConfigValues(packageJson.exclude)};
}
return false;
}
function getPackageJson() {
if (process.env.EXIFREADER_CUSTOM_BUILD) {
return JSON.parse(process.env.EXIFREADER_CUSTOM_BUILD);
}
return findConfigFromClosestPackageJson();
}
function includesGpsTag(tags) {
for (const tag of tags) {
if (tag.toLowerCase().startsWith('gps')) {
return true;
}
}
return false;
}
function includesInteroperabilityTag(tags) {
for (const tag of tags) {
if (tag.toLowerCase().startsWith('interoperability') || tag.toLowerCase().startsWith('relatedimage')) {
return true;
}
}
return false;
}
function getConfigValues(configObject) {
return Object.keys(configObject).filter((key) => !!configObject[key]);
}
function getConstantReplacements(modules) {
const replacements = [];
if (modules) {
for (const module in modules) {
replacements.push({
search: `Constants\\.USE_${module.toUpperCase()}`,
flags: 'g',
replace: JSON.stringify(!!modules[module])
});
}
}
return replacements;
}