Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

206 строки
6.4 KiB

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