forked from metabrainz/musicbrainz-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.client.config.js
258 lines (224 loc) · 6.27 KB
/
webpack.client.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright (C) 2018 MetaBrainz Foundation
*
* This file is part of MusicBrainz, the open internet music database,
* and is licensed under the GPL version 2, or (at your option) any
* later version: http://www.gnu.org/licenses/gpl-2.0.txt
*/
const _ = require('lodash');
const canonicalJson = require('canonical-json');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const fs = require('fs');
const ManifestPlugin = require('webpack-manifest-plugin');
const path = require('path');
const shell = require('shelljs');
const shellQuote = require('shell-quote');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const poFile = require('./root/server/gettext/poFile');
const DBDefs = require('./root/static/scripts/common/DBDefs');
const browserConfig = require('./webpack/browserConfig');
const {dirs, GETTEXT_DOMAINS, PUBLIC_PATH} = require('./webpack/constants');
const moduleConfig = require('./webpack/moduleConfig');
const providePluginConfig = require('./webpack/providePluginConfig');
const entries = [
'account/applications/register',
'account/edit',
'account/preferences',
'area/index',
'area/places-map',
'artist/index',
'collection/edit',
'common',
'edit',
'edit/notes-received',
'event/index',
'instrument/index',
'jed-data',
'label/index',
'place',
'place/index',
'place/map',
'release-editor',
'release-group/index',
'series',
'series/index',
'statistics',
'timeline',
'url',
'voting',
'work',
'work/index',
].reduce((accum, name) => {
accum[name] = path.resolve(dirs.SCRIPTS, `${name}.js`);
return accum;
}, {});
function langToPosix(lang) {
return lang.replace(/^([a-zA-Z]+)-([a-zA-Z]+)$/, function (match, l, c) {
l = l.toLowerCase();
c = c.toUpperCase();
/*
* Handle symlinks (see under po/). We want to keep the the result here
* consistent regardless of whether someone has el or el-gr in their
* MB_LANGUAGE setting, for example.
*/
if (/^(?:el_GR|es_ES)$/.test(`${l}_${c}`)) {
return l;
}
return l + '_' + c.toUpperCase();
});
}
function mtime(fpath) {
try {
return fs.statSync(fpath).mtime;
} catch (err) {
if (err.code === 'ENOENT') {
return null;
} else {
throw err;
}
}
}
function findNewerPo(domain, lang, bundleMtime) {
let srcPo = poFile.find(domain, lang);
const poMtime = mtime(srcPo);
if (poMtime === null) {
console.warn(`Warning: ${srcPo} does not exist, skipping`);
return null;
} else if (bundleMtime && bundleMtime >= poMtime) {
return null;
}
return srcPo;
}
function createJsPo(srcPo, lang) {
/*
* We handle the mb_server domain specially by filtering out strings that
* don't appear in any JavaScript file.
*/
/*
* msggrep's -N option supports wildcards which use fnmatch internally.
* The '*' cannot match path separators, so we must generate a list of
* possible terminal paths.
*/
const scriptsDir = shellQuote.quote([dirs.SCRIPTS]);
const nestedDirs = shell.exec(`find ${scriptsDir} -type d`, {silent: true}).output.split('\n');
const msgLocations = _(nestedDirs)
.compact()
.map(dir => '-N ' + shellQuote.quote(['..' + dir.replace(dirs.CHECKOUT, '') + '/*.js']))
.join(' ');
srcPo = shellQuote.quote([srcPo]);
tmpPo = shellQuote.quote([path.resolve(dirs.PO, `javascript.${lang}.po`)]);
/*
* Create a temporary .po file containing only the strings used by
* root/static/scripts.
*/
shell.exec(`msggrep ${msgLocations} ${srcPo} -o ${tmpPo}`);
jedData = poFile.load('javascript', lang, 'mb_server');
fs.unlinkSync(tmpPo);
return jedData;
}
function loadNewerPo(domain, lang, bundleMtime) {
const srcPo = findNewerPo(domain, lang, bundleMtime);
if (!srcPo) {
return null;
}
let jedData;
if (domain === 'mb_server') {
jedData = createJsPo(srcPo, lang)
} else {
jedData = poFile.loadFromPath(srcPo, domain);
jedData.domain = 'mb_server';
}
return jedData;
}
_(DBDefs.MB_LANGUAGES || '')
.split(/\s+/)
.compact()
.without('en')
.map(langToPosix)
.each(function (lang) {
GETTEXT_DOMAINS.forEach(function (domain) {
const fileName = `jed-${lang}-${domain}`;
const filePath = path.resolve(dirs.BUILD, `${fileName}.source.js`);
const fileMtime = mtime(filePath);
const jedData = loadNewerPo(domain, lang, fileMtime);
if (jedData) {
const source = (`
require(${
JSON.stringify(path.resolve(dirs.SCRIPTS, 'jed-data'))
}).mergeData(
${JSON.stringify(domain)},
${JSON.stringify(lang)},
${canonicalJson(jedData)},
);
`);
fs.writeFileSync(filePath, source);
}
if (fs.existsSync(filePath)) {
entries[fileName] = filePath;
}
});
});
const plugins = browserConfig.plugins.concat();
plugins.push(new webpack.ProvidePlugin(providePluginConfig));
if (!DBDefs.DEVELOPMENT_SERVER) {
plugins.push(
new webpack.HashedModuleIdsPlugin({
hashDigestLength: 7,
})
);
}
plugins.push.apply(plugins, [
new CopyWebpackPlugin([
{from: 'favicon.ico', to: '.'},
{from: 'robots.txt.*', to: '.'},
], {context: './root/'}),
new ManifestPlugin({
fileName: 'rev-manifest.json',
}),
]);
module.exports = {
context: dirs.CHECKOUT,
devtool: DBDefs.DEVELOPMENT_SERVER ? 'cheap-source-map' : undefined,
entry: entries,
mode: DBDefs.DEVELOPMENT_SERVER ? 'development' : 'production',
module: moduleConfig,
node: browserConfig.node,
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
'common-chunks': {
chunks: 'initial',
minChunks: 2,
name: 'common-chunks',
priority: -30,
reuseExistingChunk: true,
},
},
},
},
output: {
filename: (
DBDefs.DEVELOPMENT_SERVER
? '[name].js'
: '[name]-[chunkhash:7].js'
),
path: dirs.BUILD,
publicPath: PUBLIC_PATH,
},
plugins,
resolve: browserConfig.resolve,
};
if (String(process.env.WATCH_MODE) === '1') {
Object.assign(module.exports, require('./webpack/watchConfig'));
}
if (!DBDefs.DEVELOPMENT_SERVER) {
module.exports.optimization.minimizer = [
new TerserPlugin({
terserOptions: {
safari10: true,
},
}),
];
}