|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +const chalk = require('chalk'); |
| 4 | +const gzipSize = require('gzip-size'); |
| 5 | +const fs = require('fs-extra'); |
| 6 | +const path = require('path'); |
| 7 | +const { minify } = require('terser'); |
| 8 | +const webpack = require('webpack'); |
| 9 | + |
| 10 | +const { hrToSeconds } = require('./helpers'); |
| 11 | + |
| 12 | +/** |
| 13 | + * @param {string} fixturePath |
| 14 | + * @param {string} outputPath |
| 15 | + * |
| 16 | + * @return {import("webpack").Configuration} |
| 17 | + */ |
| 18 | +function createWebpackConfig(fixturePath, outputPath) { |
| 19 | + return { |
| 20 | + name: 'client', |
| 21 | + target: 'web', |
| 22 | + mode: 'production', |
| 23 | + |
| 24 | + cache: { |
| 25 | + type: 'memory', |
| 26 | + }, |
| 27 | + externals: { |
| 28 | + react: 'React', |
| 29 | + 'react-dom': 'ReactDOM', |
| 30 | + }, |
| 31 | + |
| 32 | + entry: fixturePath, |
| 33 | + output: { |
| 34 | + filename: path.basename(outputPath), |
| 35 | + path: path.dirname(outputPath), |
| 36 | + |
| 37 | + pathinfo: true, |
| 38 | + }, |
| 39 | + performance: { |
| 40 | + hints: false, |
| 41 | + }, |
| 42 | + optimization: { |
| 43 | + minimize: false, |
| 44 | + }, |
| 45 | + stats: { |
| 46 | + optimizationBailout: true, |
| 47 | + }, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * @param {import("webpack").Configuration} webpackConfig |
| 53 | + * @return {Promise<null>} |
| 54 | + */ |
| 55 | +function webpackAsync(webpackConfig) { |
| 56 | + return new Promise((resolve, reject) => { |
| 57 | + const compiler = webpack(webpackConfig); |
| 58 | + |
| 59 | + compiler.run(err => { |
| 60 | + if (err) { |
| 61 | + reject(err); |
| 62 | + } |
| 63 | + |
| 64 | + resolve(null); |
| 65 | + }); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +// --- |
| 70 | + |
| 71 | +/** @typedef {{ name: string, path: string, minifiedSize: number, gzippedSize: number }} BuildResult */ |
| 72 | + |
| 73 | +/** |
| 74 | + * Builds a fixture with Webpack and then minifies it with Terser. Produces two files as artifacts: |
| 75 | + * - partially minified file (.output.js) for debugging |
| 76 | + * - fully minified file (.min.js) |
| 77 | + * |
| 78 | + * @param {import('./prepareFixture').PreparedFixture} preparedFixture |
| 79 | + * @param {boolean} verbose |
| 80 | + * |
| 81 | + * @return {Promise<BuildResult>} |
| 82 | + */ |
| 83 | +module.exports = async function buildFixture(preparedFixture, verbose) { |
| 84 | + const webpackStartTime = process.hrtime(); |
| 85 | + |
| 86 | + const webpackOutputPath = preparedFixture.absolutePath.replace(/.fixture.js$/, '.output.js'); |
| 87 | + const config = createWebpackConfig(preparedFixture.absolutePath, webpackOutputPath); |
| 88 | + |
| 89 | + await webpackAsync(config); |
| 90 | + |
| 91 | + if (verbose) { |
| 92 | + console.log( |
| 93 | + [ |
| 94 | + chalk.blue('[i]'), |
| 95 | + `"${path.basename(preparedFixture.relativePath)}": Webpack in ${hrToSeconds(process.hrtime(webpackStartTime))}`, |
| 96 | + ].join(' '), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + // --- |
| 101 | + |
| 102 | + const terserStartTime = process.hrtime(); |
| 103 | + const terserOutputPath = preparedFixture.absolutePath.replace(/.fixture.js$/, '.min.js'); |
| 104 | + |
| 105 | + const webpackOutput = (await fs.promises.readFile(webpackOutputPath)).toString(); |
| 106 | + |
| 107 | + const [terserOutput, terserOutputMinified] = await Promise.all([ |
| 108 | + // Performs only dead-code elimination |
| 109 | + /* eslint-disable @typescript-eslint/naming-convention */ |
| 110 | + minify(webpackOutput, { |
| 111 | + mangle: false, |
| 112 | + output: { |
| 113 | + beautify: true, |
| 114 | + comments: true, |
| 115 | + preserve_annotations: true, |
| 116 | + }, |
| 117 | + }), |
| 118 | + minify(webpackOutput, { |
| 119 | + output: { |
| 120 | + comments: false, |
| 121 | + }, |
| 122 | + }), |
| 123 | + /* eslint-enable @typescript-eslint/naming-convention */ |
| 124 | + ]); |
| 125 | + |
| 126 | + await fs.promises.writeFile(webpackOutputPath, terserOutput.code); |
| 127 | + await fs.promises.writeFile(terserOutputPath, terserOutputMinified.code); |
| 128 | + |
| 129 | + if (verbose) { |
| 130 | + console.log( |
| 131 | + [ |
| 132 | + chalk.blue('[i]'), |
| 133 | + `"${path.basename(preparedFixture.relativePath)}": Terser in ${hrToSeconds(process.hrtime(terserStartTime))}`, |
| 134 | + ].join(' '), |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + const minifiedSize = (await fs.promises.stat(terserOutputPath)).size; |
| 139 | + const gzippedSize = await gzipSize.file(terserOutputPath); |
| 140 | + |
| 141 | + return { |
| 142 | + name: preparedFixture.name, |
| 143 | + path: preparedFixture.relativePath, |
| 144 | + |
| 145 | + minifiedSize, |
| 146 | + gzippedSize, |
| 147 | + }; |
| 148 | +}; |
0 commit comments