1 | 'use strict';
|
2 |
|
3 |
|
4 | process.env.BABEL_ENV = 'production';
|
5 | process.env.NODE_ENV = 'production';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | process.on('unhandledRejection', err => {
|
11 | throw err;
|
12 | });
|
13 |
|
14 |
|
15 | require('../config/env');
|
16 |
|
17 | const path = require('path');
|
18 | const chalk = require('chalk');
|
19 | const fs = require('fs-extra');
|
20 | const webpack = require('webpack');
|
21 | const config = require('../config/webpack.config.prod');
|
22 | const paths = require('../config/paths');
|
23 | const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
|
24 | const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
|
25 | const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
|
26 | const printBuildError = require('react-dev-utils/printBuildError');
|
27 |
|
28 | const measureFileSizesBeforeBuild =
|
29 | FileSizeReporter.measureFileSizesBeforeBuild;
|
30 | const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
|
31 | const useYarn = fs.existsSync(paths.yarnLockFile);
|
32 | const checkPagesRequired = require('../tools').checkPagesRequired;
|
33 |
|
34 |
|
35 | const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
|
36 | const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
|
37 |
|
38 |
|
39 | if (!checkPagesRequired(paths.allPages)) {
|
40 | process.exit(1);
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 | measureFileSizesBeforeBuild(paths.appBuild)
|
46 | .then(previousFileSizes => {
|
47 |
|
48 |
|
49 | fs.emptyDirSync(paths.appBuild);
|
50 |
|
51 | copyPublicFolder();
|
52 |
|
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 |
|
112 | function 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 |
|
124 |
|
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 |
|
153 | function copyPublicFolder() {
|
154 | fs.copySync(paths.appPublic, paths.appBuild, {
|
155 | dereference: true,
|
156 | filter: file => file !== paths.appHtml,
|
157 | });
|
158 | }
|