1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 |
|
12 | process.env.BABEL_ENV = 'production';
|
13 | process.env.NODE_ENV = 'production';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | process.on('unhandledRejection', err => {
|
19 | throw err;
|
20 | });
|
21 |
|
22 |
|
23 | require('../config/env');
|
24 |
|
25 |
|
26 | const verifyPackageTree = require('./utils/verifyPackageTree');
|
27 | if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
|
28 | verifyPackageTree();
|
29 | }
|
30 | const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
|
31 | verifyTypeScriptSetup();
|
32 |
|
33 |
|
34 | const path = require('path');
|
35 | const chalk = require('react-dev-utils/chalk');
|
36 | const fs = require('fs-extra');
|
37 | const bfj = require('bfj');
|
38 | const webpack = require('webpack');
|
39 | const configFactory = require('../config/webpack.config');
|
40 | const paths = require('../config/paths');
|
41 | const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
|
42 | const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
|
43 | const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
|
44 | const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
|
45 | const printBuildError = require('react-dev-utils/printBuildError');
|
46 |
|
47 | const measureFileSizesBeforeBuild =
|
48 | FileSizeReporter.measureFileSizesBeforeBuild;
|
49 | const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
|
50 | const useYarn = fs.existsSync(paths.yarnLockFile);
|
51 |
|
52 |
|
53 | const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
|
54 | const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
|
55 |
|
56 | const isInteractive = process.stdout.isTTY;
|
57 |
|
58 |
|
59 | if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
|
60 | process.exit(1);
|
61 | }
|
62 |
|
63 | const argv = process.argv.slice(2);
|
64 | const writeStatsJson = argv.indexOf('--stats') !== -1;
|
65 |
|
66 |
|
67 | const config = configFactory('production');
|
68 |
|
69 |
|
70 |
|
71 | const { checkBrowsers } = require('react-dev-utils/browsersHelper');
|
72 | checkBrowsers(paths.appPath, isInteractive)
|
73 | .then(() => {
|
74 |
|
75 |
|
76 | return measureFileSizesBeforeBuild(paths.appBuild);
|
77 | })
|
78 | .then(previousFileSizes => {
|
79 |
|
80 |
|
81 | fs.emptyDirSync(paths.appBuild);
|
82 |
|
83 | copyPublicFolder();
|
84 |
|
85 | return build(previousFileSizes);
|
86 | })
|
87 | .then(
|
88 | ({ stats, previousFileSizes, warnings }) => {
|
89 | if (warnings.length) {
|
90 | console.log(chalk.yellow('Compiled with warnings.\n'));
|
91 | console.log(warnings.join('\n\n'));
|
92 | console.log(
|
93 | '\nSearch for the ' +
|
94 | chalk.underline(chalk.yellow('keywords')) +
|
95 | ' to learn more about each warning.'
|
96 | );
|
97 | console.log(
|
98 | 'To ignore, add ' +
|
99 | chalk.cyan('// eslint-disable-next-line') +
|
100 | ' to the line before.\n'
|
101 | );
|
102 | } else {
|
103 | console.log(chalk.green('Compiled successfully.\n'));
|
104 | }
|
105 |
|
106 | console.log('File sizes after gzip:\n');
|
107 | printFileSizesAfterBuild(
|
108 | stats,
|
109 | previousFileSizes,
|
110 | paths.appBuild,
|
111 | WARN_AFTER_BUNDLE_GZIP_SIZE,
|
112 | WARN_AFTER_CHUNK_GZIP_SIZE
|
113 | );
|
114 | console.log();
|
115 |
|
116 | const appPackage = require(paths.appPackageJson);
|
117 | const publicUrl = paths.publicUrlOrPath;
|
118 | const publicPath = config.output.publicPath;
|
119 | const buildFolder = path.relative(process.cwd(), paths.appBuild);
|
120 | printHostingInstructions(
|
121 | appPackage,
|
122 | publicUrl,
|
123 | publicPath,
|
124 | buildFolder,
|
125 | useYarn
|
126 | );
|
127 | },
|
128 | err => {
|
129 | const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
|
130 | if (tscCompileOnError) {
|
131 | console.log(
|
132 | chalk.yellow(
|
133 | 'Compiled with the following type errors (you may want to check these before deploying your app):\n'
|
134 | )
|
135 | );
|
136 | printBuildError(err);
|
137 | } else {
|
138 | console.log(chalk.red('Failed to compile.\n'));
|
139 | printBuildError(err);
|
140 | process.exit(1);
|
141 | }
|
142 | }
|
143 | )
|
144 | .catch(err => {
|
145 | if (err && err.message) {
|
146 | console.log(err.message);
|
147 | }
|
148 | process.exit(1);
|
149 | });
|
150 |
|
151 |
|
152 | function build(previousFileSizes) {
|
153 | console.log('Creating an optimized production build...');
|
154 |
|
155 | const compiler = webpack(config);
|
156 | return new Promise((resolve, reject) => {
|
157 | compiler.run((err, stats) => {
|
158 | let messages;
|
159 | if (err) {
|
160 | if (!err.message) {
|
161 | return reject(err);
|
162 | }
|
163 |
|
164 | let errMessage = err.message;
|
165 |
|
166 |
|
167 | if (Object.prototype.hasOwnProperty.call(err, 'postcssNode')) {
|
168 | errMessage +=
|
169 | '\nCompileError: Begins at CSS selector ' +
|
170 | err['postcssNode'].selector;
|
171 | }
|
172 |
|
173 | messages = formatWebpackMessages({
|
174 | errors: [errMessage],
|
175 | warnings: [],
|
176 | });
|
177 | } else {
|
178 | messages = formatWebpackMessages(
|
179 | stats.toJson({ all: false, warnings: true, errors: true })
|
180 | );
|
181 | }
|
182 | if (messages.errors.length) {
|
183 |
|
184 |
|
185 | if (messages.errors.length > 1) {
|
186 | messages.errors.length = 1;
|
187 | }
|
188 | return reject(new Error(messages.errors.join('\n\n')));
|
189 | }
|
190 | if (
|
191 | process.env.CI &&
|
192 | (typeof process.env.CI !== 'string' ||
|
193 | process.env.CI.toLowerCase() !== 'false') &&
|
194 | messages.warnings.length
|
195 | ) {
|
196 | console.log(
|
197 | chalk.yellow(
|
198 | '\nTreating warnings as errors because process.env.CI = true.\n' +
|
199 | 'Most CI servers set it automatically.\n'
|
200 | )
|
201 | );
|
202 | return reject(new Error(messages.warnings.join('\n\n')));
|
203 | }
|
204 |
|
205 | const resolveArgs = {
|
206 | stats,
|
207 | previousFileSizes,
|
208 | warnings: messages.warnings,
|
209 | };
|
210 |
|
211 | if (writeStatsJson) {
|
212 | return bfj
|
213 | .write(paths.appBuild + '/bundle-stats.json', stats.toJson())
|
214 | .then(() => resolve(resolveArgs))
|
215 | .catch(error => reject(new Error(error)));
|
216 | }
|
217 |
|
218 | return resolve(resolveArgs);
|
219 | });
|
220 | });
|
221 | }
|
222 |
|
223 | function copyPublicFolder() {
|
224 | fs.copySync(paths.appPublic, paths.appBuild, {
|
225 | dereference: true,
|
226 | filter: file => file !== paths.appHtml,
|
227 | });
|
228 | }
|