|
| 1 | +const c = require('ansi-colors') |
| 2 | +const glob = require('glob') |
| 3 | +const path = require('path') |
| 4 | +const terserVersion = require('terser/package.json').version |
| 5 | +const TerserWebpackPlugin = require('terser-webpack-plugin') |
| 6 | +const webpack = require('webpack') |
| 7 | +const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin |
| 8 | +const { argv } = require('yargs') |
| 9 | + |
| 10 | +const config = require('../config') |
| 11 | + |
| 12 | +// Ensures that production settings for Babel are used |
| 13 | +process.env.NODE_ENV = 'build-es' |
| 14 | + |
| 15 | +/* eslint-disable no-await-in-loop */ |
| 16 | +/* eslint-disable no-console */ |
| 17 | +/* eslint-disable no-restricted-syntax */ |
| 18 | + |
| 19 | +// |
| 20 | +// Webpack config |
| 21 | +// |
| 22 | + |
| 23 | +const makeWebpackConfig = (entry) => ({ |
| 24 | + devtool: false, |
| 25 | + mode: 'production', |
| 26 | + name: 'client', |
| 27 | + target: 'web', |
| 28 | + |
| 29 | + entry, |
| 30 | + output: { |
| 31 | + filename: path.basename(entry), |
| 32 | + path: config.paths.base('bundle-size', 'dist'), |
| 33 | + }, |
| 34 | + |
| 35 | + module: { |
| 36 | + rules: [ |
| 37 | + { |
| 38 | + test: /\.(js|ts)$/, |
| 39 | + loader: 'babel-loader', |
| 40 | + exclude: /node_modules/, |
| 41 | + options: { |
| 42 | + cacheDirectory: true, |
| 43 | + }, |
| 44 | + }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + externals: { |
| 48 | + react: 'react', |
| 49 | + 'react-dom': 'reactDOM', |
| 50 | + }, |
| 51 | + |
| 52 | + ...(argv.debug && { |
| 53 | + optimization: { |
| 54 | + minimizer: [ |
| 55 | + new TerserWebpackPlugin({ |
| 56 | + cache: true, |
| 57 | + parallel: true, |
| 58 | + sourceMap: false, |
| 59 | + |
| 60 | + terserOptions: { |
| 61 | + mangle: false, |
| 62 | + output: { |
| 63 | + beautify: true, |
| 64 | + comments: true, |
| 65 | + preserve_annotations: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }), |
| 69 | + ], |
| 70 | + }, |
| 71 | + }), |
| 72 | + |
| 73 | + performance: { |
| 74 | + hints: false, |
| 75 | + }, |
| 76 | + plugins: [ |
| 77 | + argv.debug && |
| 78 | + new BundleAnalyzerPlugin({ |
| 79 | + analyzerMode: 'static', |
| 80 | + logLevel: 'warn', |
| 81 | + openAnalyzer: false, |
| 82 | + reportFilename: `${path.basename(entry, '.js')}.html`, |
| 83 | + }), |
| 84 | + ].filter(Boolean), |
| 85 | + resolve: { |
| 86 | + alias: { |
| 87 | + 'semantic-ui-react': config.paths.dist('es', 'index.js'), |
| 88 | + }, |
| 89 | + }, |
| 90 | +}) |
| 91 | + |
| 92 | +function webpackAsync(webpackConfig) { |
| 93 | + return new Promise((resolve, reject) => { |
| 94 | + const compiler = webpack(webpackConfig) |
| 95 | + |
| 96 | + compiler.run((err, stats) => { |
| 97 | + if (err) { |
| 98 | + reject(err) |
| 99 | + } |
| 100 | + |
| 101 | + const info = stats.toJson() |
| 102 | + |
| 103 | + if (stats.hasErrors()) { |
| 104 | + reject(new Error(info.errors.toString())) |
| 105 | + } |
| 106 | + if (stats.hasWarnings()) { |
| 107 | + reject(new Error(info.warnings.toString())) |
| 108 | + } |
| 109 | + |
| 110 | + resolve(info) |
| 111 | + }) |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +// |
| 116 | +// |
| 117 | +// |
| 118 | + |
| 119 | +;(async () => { |
| 120 | + const fixtures = glob.sync('fixtures/*.size.js', { |
| 121 | + cwd: __dirname, |
| 122 | + }) |
| 123 | + |
| 124 | + console.log(c.cyan(`ℹ Using Webpack ${webpack.version} & Terser ${terserVersion}`)) |
| 125 | + |
| 126 | + console.log(c.cyan('ℹ Running following fixtures:')) |
| 127 | + console.log(c.cyan(fixtures.map((fixture) => ` - ${fixture}`).join('\n'))) |
| 128 | + |
| 129 | + for (const fixture of fixtures) { |
| 130 | + const fixturePath = config.paths.base('bundle-size', fixture) |
| 131 | + |
| 132 | + await webpackAsync(makeWebpackConfig(fixturePath)) |
| 133 | + console.log(c.green(`✔ Completed: ${fixture}`)) |
| 134 | + } |
| 135 | +})() |
0 commit comments