説明なし

build.js 4.8KB

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