UNPKG

5.71 kBJavaScriptView Raw
1'use strict';
2
3// Do this as the first thing so that any code reading it knows the right env.
4process.env.BABEL_ENV = 'production';
5process.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.
10process.on('unhandledRejection', err => {
11 throw err;
12});
13
14// Ensure environment variables are read.
15require('../config/env');
16
17const path = require('path');
18const chalk = require('chalk');
19const fs = require('fs-extra');
20const webpack = require('webpack');
21const config = require('../config/webpack.config.prod');
22const paths = require('../config/paths');
23const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
24const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
25const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
26const printBuildError = require('react-dev-utils/printBuildError');
27
28const measureFileSizesBeforeBuild =
29 FileSizeReporter.measureFileSizesBeforeBuild;
30const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
31const useYarn = fs.existsSync(paths.yarnLockFile);
32const checkPagesRequired = require('../tools').checkPagesRequired;
33
34// These sizes are pretty large. We'll warn for bundles exceeding them.
35const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
36const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
37
38// Warn and crash if required files are missing
39if (!checkPagesRequired(paths.allPages)) {
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.
45measureFileSizesBeforeBuild(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 try{
97 const endHook = require(path.resolve('config/endBuildHook'));
98 }catch(e){
99 console.log('We support endBuildHook functionality!' + '\n');
100 }
101
102
103 },
104 err => {
105 console.log(chalk.red('Failed to compile.\n'));
106 printBuildError(err);
107 process.exit(1);
108 }
109 );
110
111// Create the production build and print the deployment instructions.
112function build(previousFileSizes) {
113 console.log('Creating an optimized production build...');
114
115 let compiler = webpack(config);
116 return new Promise((resolve, reject) => {
117 compiler.run((err, stats) => {
118 if (err) {
119 return reject(err);
120 }
121 const messages = formatWebpackMessages(stats.toJson({}, true));
122 if (messages.errors.length) {
123 // Only keep the first error. Others are often indicative
124 // of the same problem, but confuse the reader with noise.
125 if (messages.errors.length > 1) {
126 messages.errors.length = 1;
127 }
128 return reject(new Error(messages.errors.join('\n\n')));
129 }
130 if (
131 process.env.CI &&
132 (typeof process.env.CI !== 'string' ||
133 process.env.CI.toLowerCase() !== 'false') &&
134 messages.warnings.length
135 ) {
136 console.log(
137 chalk.yellow(
138 '\nTreating warnings as errors because process.env.CI = true.\n' +
139 'Most CI servers set it automatically.\n'
140 )
141 );
142 return reject(new Error(messages.warnings.join('\n\n')));
143 }
144 return resolve({
145 stats,
146 previousFileSizes,
147 warnings: messages.warnings,
148 });
149 });
150 });
151}
152
153function copyPublicFolder() {
154 fs.copySync(paths.appPublic, paths.appBuild, {
155 dereference: true,
156 filter: file => file !== paths.appHtml,
157 });
158}