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 | const oem = require('../config/oem');
|
67 | config.resolve.alias['~customize'] = path.join(
|
68 | paths.appSrc,
|
69 | 'customize',
|
70 | oem.customizeEntry
|
71 | );
|
72 |
|
73 |
|
74 |
|
75 | const { checkBrowsers } = require('react-dev-utils/browsersHelper');
|
76 | checkBrowsers(paths.appPath, isInteractive)
|
77 | .then(() => {
|
78 |
|
79 |
|
80 | return measureFileSizesBeforeBuild(paths.appBuild);
|
81 | })
|
82 | .then(previousFileSizes => {
|
83 |
|
84 |
|
85 | fs.emptyDirSync(paths.appBuild);
|
86 |
|
87 | copyPublicFolder();
|
88 |
|
89 | return build(previousFileSizes);
|
90 | })
|
91 | .then(
|
92 | ({ stats, previousFileSizes, warnings }) => {
|
93 | if (warnings.length) {
|
94 | console.log(chalk.yellow('Compiled with warnings.\n'));
|
95 | console.log(warnings.join('\n\n'));
|
96 | console.log(
|
97 | '\nSearch for the ' +
|
98 | chalk.underline(chalk.yellow('keywords')) +
|
99 | ' to learn more about each warning.'
|
100 | );
|
101 | console.log(
|
102 | 'To ignore, add ' +
|
103 | chalk.cyan('// eslint-disable-next-line') +
|
104 | ' to the line before.\n'
|
105 | );
|
106 | } else {
|
107 | console.log(chalk.green('Compiled successfully.\n'));
|
108 | }
|
109 |
|
110 | console.log('File sizes after gzip:\n');
|
111 | printFileSizesAfterBuild(
|
112 | stats,
|
113 | previousFileSizes,
|
114 | paths.appBuild,
|
115 | WARN_AFTER_BUNDLE_GZIP_SIZE,
|
116 | WARN_AFTER_CHUNK_GZIP_SIZE
|
117 | );
|
118 | console.log();
|
119 |
|
120 | const appPackage = require(paths.appPackageJson);
|
121 | const publicUrl = paths.publicUrl;
|
122 | const publicPath = config.output.publicPath;
|
123 | const buildFolder = path.relative(process.cwd(), paths.appBuild);
|
124 | printHostingInstructions(
|
125 | appPackage,
|
126 | publicUrl,
|
127 | publicPath,
|
128 | buildFolder,
|
129 | useYarn
|
130 | );
|
131 | },
|
132 | err => {
|
133 | console.log(chalk.red('Failed to compile.\n'));
|
134 | printBuildError(err);
|
135 | process.exit(1);
|
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 | }
|