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('chalk');
|
36 | const fs = require('fs-extra');
|
37 | const webpack = require('webpack');
|
38 | const bfj = require('bfj');
|
39 | const config = require('../config/webpack.config.prod');
|
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 |
|
64 | const argv = process.argv.slice(2);
|
65 | const writeStatsJson = argv.indexOf('--stats') !== -1;
|
66 |
|
67 |
|
68 |
|
69 | const { checkBrowsers } = require('react-dev-utils/browsersHelper');
|
70 | checkBrowsers(paths.appPath, isInteractive)
|
71 | .then(() => {
|
72 |
|
73 |
|
74 | return measureFileSizesBeforeBuild(paths.appBuild);
|
75 | })
|
76 | .then(previousFileSizes => {
|
77 |
|
78 |
|
79 | fs.emptyDirSync(paths.appBuild);
|
80 |
|
81 | copyPublicFolder();
|
82 |
|
83 | return build(previousFileSizes);
|
84 | })
|
85 | .then(
|
86 | ({ stats, previousFileSizes, warnings }) => {
|
87 | if (warnings.length) {
|
88 | console.log(chalk.yellow('Compiled with warnings.\n'));
|
89 | console.log(warnings.join('\n\n'));
|
90 | console.log(
|
91 | '\nSearch for the ' +
|
92 | chalk.underline(chalk.yellow('keywords')) +
|
93 | ' to learn more about each warning.'
|
94 | );
|
95 | console.log(
|
96 | 'To ignore, add ' +
|
97 | chalk.cyan('// eslint-disable-next-line') +
|
98 | ' to the line before.\n'
|
99 | );
|
100 | } else {
|
101 | console.log(chalk.green('Compiled successfully.\n'));
|
102 | }
|
103 |
|
104 | console.log('File sizes after gzip:\n');
|
105 | printFileSizesAfterBuild(
|
106 | stats,
|
107 | previousFileSizes,
|
108 | paths.appBuild,
|
109 | WARN_AFTER_BUNDLE_GZIP_SIZE,
|
110 | WARN_AFTER_CHUNK_GZIP_SIZE
|
111 | );
|
112 | console.log();
|
113 |
|
114 | const appPackage = require(paths.appPackageJson);
|
115 | const publicUrl = paths.publicUrl;
|
116 | const publicPath = config.output.publicPath;
|
117 | const buildFolder = path.relative(process.cwd(), paths.appBuild);
|
118 | printHostingInstructions(
|
119 | appPackage,
|
120 | publicUrl,
|
121 | publicPath,
|
122 | buildFolder,
|
123 | useYarn
|
124 | );
|
125 | },
|
126 | err => {
|
127 | console.log(chalk.red('Failed to compile.\n'));
|
128 | printBuildError(err);
|
129 | process.exit(1);
|
130 | }
|
131 | )
|
132 | .catch(err => {
|
133 | if (err && err.message) {
|
134 | console.log(err.message);
|
135 | }
|
136 | process.exit(1);
|
137 | });
|
138 |
|
139 |
|
140 | function build(previousFileSizes) {
|
141 | console.log('Creating an optimized production build...');
|
142 |
|
143 | let compiler = webpack(config);
|
144 | return new Promise((resolve, reject) => {
|
145 | compiler.run((err, stats) => {
|
146 | let messages;
|
147 | if (err) {
|
148 | if (!err.message) {
|
149 | return reject(err);
|
150 | }
|
151 | messages = formatWebpackMessages({
|
152 | errors: [err.message],
|
153 | warnings: [],
|
154 | });
|
155 | } else {
|
156 | messages = formatWebpackMessages(
|
157 | stats.toJson({ all: false, warnings: true, errors: true })
|
158 | );
|
159 | }
|
160 | if (messages.errors.length) {
|
161 |
|
162 |
|
163 | if (messages.errors.length > 1) {
|
164 | messages.errors.length = 1;
|
165 | }
|
166 | return reject(new Error(messages.errors.join('\n\n')));
|
167 | }
|
168 | if (
|
169 | process.env.CI &&
|
170 | (typeof process.env.CI !== 'string' ||
|
171 | process.env.CI.toLowerCase() !== 'false') &&
|
172 | messages.warnings.length
|
173 | ) {
|
174 | console.log(
|
175 | chalk.yellow(
|
176 | '\nTreating warnings as errors because process.env.CI = true.\n' +
|
177 | 'Most CI servers set it automatically.\n'
|
178 | )
|
179 | );
|
180 | return reject(new Error(messages.warnings.join('\n\n')));
|
181 | }
|
182 |
|
183 | const resolveArgs = {
|
184 | stats,
|
185 | previousFileSizes,
|
186 | warnings: messages.warnings,
|
187 | };
|
188 | if (writeStatsJson) {
|
189 | return bfj
|
190 | .write(paths.appBuild + '/bundle-stats.json', stats.toJson())
|
191 | .then(() => resolve(resolveArgs))
|
192 | .catch(error => reject(new Error(error)));
|
193 | }
|
194 |
|
195 | return resolve(resolveArgs);
|
196 | });
|
197 | });
|
198 | }
|
199 |
|
200 | function copyPublicFolder() {
|
201 | fs.copySync(paths.appPublic, paths.appBuild, {
|
202 | dereference: true,
|
203 | filter: file => file !== paths.appHtml,
|
204 | });
|
205 | }
|