Keine Beschreibung

build.lib.js 4.8KB

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