github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/shyftBlockExplorerUI/scripts/build.js (about)

     1  'use strict';
     2  
     3  // Do this as the first thing so that any code reading it knows the right env.
     4  process.env.BABEL_ENV = 'production';
     5  process.env.NODE_ENV = 'production';
     6  
     7  // Makes the script crash on unhandled rejections instead of silently
     8  // ignoring them. In the future, promise rejections that are not handled will
     9  // terminate the Node.js process with a non-zero exit code.
    10  process.on('unhandledRejection', err => {
    11    throw err;
    12  });
    13  
    14  // Ensure environment variables are read.
    15  require('../config/env');
    16  
    17  const path = require('path');
    18  const chalk = require('chalk');
    19  const fs = require('fs-extra');
    20  const webpack = require('webpack');
    21  const config = require('../config/webpack.config.prod');
    22  const paths = require('../config/paths');
    23  const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
    24  const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
    25  const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
    26  const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
    27  const printBuildError = require('react-dev-utils/printBuildError');
    28  
    29  const measureFileSizesBeforeBuild =
    30    FileSizeReporter.measureFileSizesBeforeBuild;
    31  const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
    32  const useYarn = fs.existsSync(paths.yarnLockFile);
    33  
    34  // These sizes are pretty large. We'll warn for bundles exceeding them.
    35  const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
    36  const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
    37  
    38  // Warn and crash if required files are missing
    39  if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
    40    process.exit(1);
    41  }
    42  
    43  // First, read the current file sizes in build directory.
    44  // This lets us display how much they changed later.
    45  measureFileSizesBeforeBuild(paths.appBuild)
    46    .then(previousFileSizes => {
    47      // Remove all content but keep the directory so that
    48      // if you're in it, you don't end up in Trash
    49      fs.emptyDirSync(paths.appBuild);
    50      // Merge with the public folder
    51      copyPublicFolder();
    52      // Start the webpack build
    53      return build(previousFileSizes);
    54    })
    55    .then(
    56      ({ stats, previousFileSizes, warnings }) => {
    57        if (warnings.length) {
    58          console.log(chalk.yellow('Compiled with warnings.\n'));
    59          console.log(warnings.join('\n\n'));
    60          console.log(
    61            '\nSearch for the ' +
    62              chalk.underline(chalk.yellow('keywords')) +
    63              ' to learn more about each warning.'
    64          );
    65          console.log(
    66            'To ignore, add ' +
    67              chalk.cyan('// eslint-disable-next-line') +
    68              ' to the line before.\n'
    69          );
    70        } else {
    71          console.log(chalk.green('Compiled successfully.\n'));
    72        }
    73  
    74        console.log('File sizes after gzip:\n');
    75        printFileSizesAfterBuild(
    76          stats,
    77          previousFileSizes,
    78          paths.appBuild,
    79          WARN_AFTER_BUNDLE_GZIP_SIZE,
    80          WARN_AFTER_CHUNK_GZIP_SIZE
    81        );
    82        console.log();
    83  
    84        const appPackage = require(paths.appPackageJson);
    85        const publicUrl = paths.publicUrl;
    86        const publicPath = config.output.publicPath;
    87        const buildFolder = path.relative(process.cwd(), paths.appBuild);
    88        printHostingInstructions(
    89          appPackage,
    90          publicUrl,
    91          publicPath,
    92          buildFolder,
    93          useYarn
    94        );
    95      },
    96      err => {
    97        console.log(chalk.red('Failed to compile.\n'));
    98        printBuildError(err);
    99        process.exit(1);
   100      }
   101    );
   102  
   103  // Create the production build and print the deployment instructions.
   104  function build(previousFileSizes) {
   105    console.log('Creating an optimized production build...');
   106  
   107    let compiler = webpack(config);
   108    return new Promise((resolve, reject) => {
   109      compiler.run((err, stats) => {
   110        if (err) {
   111          return reject(err);
   112        }
   113        const messages = formatWebpackMessages(stats.toJson({}, true));
   114        if (messages.errors.length) {
   115          // Only keep the first error. Others are often indicative
   116          // of the same problem, but confuse the reader with noise.
   117          if (messages.errors.length > 1) {
   118            messages.errors.length = 1;
   119          }
   120          return reject(new Error(messages.errors.join('\n\n')));
   121        }
   122        if (
   123          process.env.CI &&
   124          (typeof process.env.CI !== 'string' ||
   125            process.env.CI.toLowerCase() !== 'false') &&
   126          messages.warnings.length
   127        ) {
   128          console.log(
   129            chalk.yellow(
   130              '\nTreating warnings as errors because process.env.CI = true.\n' +
   131                'Most CI servers set it automatically.\n'
   132            )
   133          );
   134          return reject(new Error(messages.warnings.join('\n\n')));
   135        }
   136        return resolve({
   137          stats,
   138          previousFileSizes,
   139          warnings: messages.warnings,
   140        });
   141      });
   142    });
   143  }
   144  
   145  function copyPublicFolder() {
   146    fs.copySync(paths.appPublic, paths.appBuild, {
   147      dereference: true,
   148      filter: file => file !== paths.appHtml,
   149    });
   150  }