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 webpack = require('webpack');
|
38 | const configFactory = require('../config/webpack.config');
|
39 | const paths = require('../config/paths');
|
40 | const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
|
41 | const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
|
42 | const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
|
43 | const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
|
44 | const printBuildError = require('react-dev-utils/printBuildError');
|
45 |
|
46 | const measureFileSizesBeforeBuild =
|
47 | FileSizeReporter.measureFileSizesBeforeBuild;
|
48 | const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
|
49 | const useYarn = fs.existsSync(paths.yarnLockFile);
|
50 |
|
51 |
|
52 | const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
|
53 | const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
|
54 |
|
55 | const isInteractive = process.stdout.isTTY;
|
56 |
|
57 |
|
58 | if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
|
59 | process.exit(1);
|
60 | }
|
61 |
|
62 |
|
63 | const config = configFactory('production');
|
64 |
|
65 |
|
66 |
|
67 | const { checkBrowsers } = require('react-dev-utils/browsersHelper');
|
68 | checkBrowsers(paths.appPath, isInteractive)
|
69 | .then(() => {
|
70 |
|
71 |
|
72 | return measureFileSizesBeforeBuild(paths.appBuild);
|
73 | })
|
74 | .then(previousFileSizes => {
|
75 |
|
76 |
|
77 | fs.emptyDirSync(paths.appBuild);
|
78 |
|
79 | copyPublicFolder();
|
80 |
|
81 | return build(previousFileSizes);
|
82 | })
|
83 | .then(
|
84 | ({ stats, previousFileSizes, warnings }) => {
|
85 | if (warnings.length) {
|
86 | console.log(chalk.yellow('Compiled with warnings.\n'));
|
87 | console.log(warnings.join('\n\n'));
|
88 | console.log(
|
89 | '\nSearch for the ' +
|
90 | chalk.underline(chalk.yellow('keywords')) +
|
91 | ' to learn more about each warning.'
|
92 | );
|
93 | console.log(
|
94 | 'To ignore, add ' +
|
95 | chalk.cyan('// eslint-disable-next-line') +
|
96 | ' to the line before.\n'
|
97 | );
|
98 | } else {
|
99 | console.log(chalk.green('Compiled successfully.\n'));
|
100 | }
|
101 |
|
102 | console.log('File sizes after gzip:\n');
|
103 | printFileSizesAfterBuild(
|
104 | stats,
|
105 | previousFileSizes,
|
106 | paths.appBuild,
|
107 | WARN_AFTER_BUNDLE_GZIP_SIZE,
|
108 | WARN_AFTER_CHUNK_GZIP_SIZE
|
109 | );
|
110 | console.log();
|
111 |
|
112 | const appPackage = require(paths.appPackageJson);
|
113 | const publicUrl = paths.publicUrl;
|
114 | const publicPath = config.output.publicPath;
|
115 | const buildFolder = path.relative(process.cwd(), paths.appBuild);
|
116 | printHostingInstructions(
|
117 | appPackage,
|
118 | publicUrl,
|
119 | publicPath,
|
120 | buildFolder,
|
121 | useYarn
|
122 | );
|
123 | },
|
124 | err => {
|
125 | const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
|
126 | if (tscCompileOnError) {
|
127 | console.log(chalk.yellow(
|
128 | 'Compiled with the following type errors (you may want to check these before deploying your app):\n'
|
129 | ));
|
130 | printBuildError(err);
|
131 | } else {
|
132 | console.log(chalk.red('Failed to compile.\n'));
|
133 | printBuildError(err);
|
134 | process.exit(1);
|
135 | }
|
136 | }
|
137 | )
|
138 | .catch(err => {
|
139 | if (err && err.message) {
|
140 | console.log(err.message);
|
141 | }
|
142 | process.exit(1);
|
143 | });
|
144 |
|
145 |
|
146 | function build(previousFileSizes) {
|
147 |
|
148 |
|
149 |
|
150 | if (process.env.NODE_PATH) {
|
151 | console.log(
|
152 | chalk.yellow(
|
153 | 'Setting NODE_PATH to resolve modules absolutely has been deprecated in favor of setting baseUrl in jsconfig.json (or tsconfig.json if you are using TypeScript) and will be removed in a future major release of create-react-app.'
|
154 | )
|
155 | );
|
156 | console.log();
|
157 | }
|
158 |
|
159 | console.log('Creating an optimized production build...');
|
160 |
|
161 | const compiler = webpack(config);
|
162 | return new Promise((resolve, reject) => {
|
163 | compiler.run((err, stats) => {
|
164 | let messages;
|
165 | if (err) {
|
166 | if (!err.message) {
|
167 | return reject(err);
|
168 | }
|
169 | messages = formatWebpackMessages({
|
170 | errors: [err.message],
|
171 | warnings: [],
|
172 | });
|
173 | } else {
|
174 | messages = formatWebpackMessages(
|
175 | stats.toJson({ all: false, warnings: true, errors: true })
|
176 | );
|
177 | }
|
178 | if (messages.errors.length) {
|
179 |
|
180 |
|
181 | if (messages.errors.length > 1) {
|
182 | messages.errors.length = 1;
|
183 | }
|
184 | return reject(new Error(messages.errors.join('\n\n')));
|
185 | }
|
186 | if (
|
187 | process.env.CI &&
|
188 | (typeof process.env.CI !== 'string' ||
|
189 | process.env.CI.toLowerCase() !== 'false') &&
|
190 | messages.warnings.length
|
191 | ) {
|
192 | console.log(
|
193 | chalk.yellow(
|
194 | '\nTreating warnings as errors because process.env.CI = true.\n' +
|
195 | 'Most CI servers set it automatically.\n'
|
196 | )
|
197 | );
|
198 | return reject(new Error(messages.warnings.join('\n\n')));
|
199 | }
|
200 |
|
201 | return resolve({
|
202 | stats,
|
203 | previousFileSizes,
|
204 | warnings: messages.warnings,
|
205 | });
|
206 | });
|
207 | });
|
208 | }
|
209 |
|
210 | function copyPublicFolder() {
|
211 | fs.copySync(paths.appPublic, paths.appBuild, {
|
212 | dereference: true,
|
213 | filter: file => file !== paths.appHtml,
|
214 | });
|
215 | }
|