| 1343 | |
| 1344 | // Minify the JS in this target |
| 1345 | minifyJs(minifierDef, minifyMode) { |
| 1346 | const staticFiles = []; |
| 1347 | const dynamicFiles = []; |
| 1348 | const { arch } = this; |
| 1349 | const inputHashesByJsFile = new Map; |
| 1350 | |
| 1351 | this.js.forEach(file => { |
| 1352 | const jsf = new JsFile(file, { arch }); |
| 1353 | |
| 1354 | inputHashesByJsFile.set(jsf, file.hash()); |
| 1355 | |
| 1356 | if (file.targetPath.startsWith("dynamic/")) { |
| 1357 | // Dynamic files consist of a single anonymous function |
| 1358 | // expression, which some minifiers (e.g. UglifyJS) either fail to |
| 1359 | // parse or mistakenly eliminate as dead code. To avoid these |
| 1360 | // problems, we temporarily name the function __minifyJs. |
| 1361 | file._contents = Buffer.concat([ |
| 1362 | MINIFY_RENAMED_FUNCTION, |
| 1363 | file.contents().slice(MINIFY_PLAIN_FUNCTION.length) |
| 1364 | ]); |
| 1365 | |
| 1366 | dynamicFiles.push(jsf); |
| 1367 | |
| 1368 | } else { |
| 1369 | staticFiles.push(jsf); |
| 1370 | } |
| 1371 | }); |
| 1372 | |
| 1373 | var markedMinifier = buildmessage.markBoundary( |
| 1374 | minifierDef.userPlugin.processFilesForBundle, |
| 1375 | minifierDef.userPlugin |
| 1376 | ); |
| 1377 | |
| 1378 | buildmessage.enterJob('minifying app code', function () { |
| 1379 | try { |
| 1380 | Promise.all([ |
| 1381 | markedMinifier(staticFiles, { minifyMode }), |
| 1382 | ...dynamicFiles.map( |
| 1383 | file => markedMinifier([file], { minifyMode }) |
| 1384 | ), |
| 1385 | ]).await(); |
| 1386 | } catch (e) { |
| 1387 | buildmessage.exception(e); |
| 1388 | } |
| 1389 | }); |
| 1390 | |
| 1391 | const js = []; |
| 1392 | |
| 1393 | function handle(source, dynamic) { |
| 1394 | source._minifiedFiles.forEach(file => { |
| 1395 | // Remove the function name __minifyJs that was added above. |
| 1396 | if (typeof file.data === 'string') { |
| 1397 | file.data = Buffer.from( |
| 1398 | file.data |
| 1399 | .replace(/^\s*function\s+__minifyJs\s*\(/, |
| 1400 | "function("), |
| 1401 | "utf8" |
| 1402 | ); |