|
1
|
|
|
'use strict'; |
|
2
|
|
|
|
|
3
|
|
|
// Do this as the first thing so that any code reading it knows the right env. |
|
4
|
|
|
process.env.BABEL_ENV = 'production'; |
|
5
|
|
|
process.env.NODE_ENV = 'production'; |
|
6
|
|
|
|
|
7
|
|
|
// Makes the script crash on unhandled rejections instead of silently |
|
8
|
|
|
// ignoring them. In the future, promise rejections that are not handled will |
|
9
|
|
|
// terminate the Node.js process with a non-zero exit code. |
|
10
|
|
|
process.on('unhandledRejection', err => { |
|
11
|
|
|
throw err; |
|
12
|
|
|
}); |
|
13
|
|
|
|
|
14
|
|
|
// Ensure environment variables are read. |
|
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 checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); |
|
24
|
|
|
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages'); |
|
25
|
|
|
const printHostingInstructions = require('react-dev-utils/printHostingInstructions'); |
|
26
|
|
|
const FileSizeReporter = require('react-dev-utils/FileSizeReporter'); |
|
27
|
|
|
|
|
28
|
|
|
const measureFileSizesBeforeBuild = |
|
29
|
|
|
FileSizeReporter.measureFileSizesBeforeBuild; |
|
30
|
|
|
const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild; |
|
31
|
|
|
const useYarn = fs.existsSync(paths.yarnLockFile); |
|
32
|
|
|
|
|
33
|
|
|
// These sizes are pretty large. We'll warn for bundles exceeding them. |
|
34
|
|
|
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024; |
|
35
|
|
|
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024; |
|
36
|
|
|
|
|
37
|
|
|
// Warn and crash if required files are missing |
|
38
|
|
|
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { |
|
39
|
|
|
process.exit(1); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// First, read the current file sizes in build directory. |
|
43
|
|
|
// This lets us display how much they changed later. |
|
44
|
|
|
measureFileSizesBeforeBuild(paths.appBuild) |
|
45
|
|
|
.then(previousFileSizes => { |
|
46
|
|
|
// Remove all content but keep the directory so that |
|
47
|
|
|
// if you're in it, you don't end up in Trash |
|
48
|
|
|
fs.emptyDirSync(paths.appBuild); |
|
49
|
|
|
// Merge with the public folder |
|
50
|
|
|
copyPublicFolder(); |
|
51
|
|
|
// Start the webpack build |
|
52
|
|
|
return build(previousFileSizes); |
|
53
|
|
|
}) |
|
54
|
|
|
.then( |
|
55
|
|
|
({ stats, previousFileSizes, warnings }) => { |
|
56
|
|
|
if (warnings.length) { |
|
57
|
|
|
console.log(chalk.yellow('Compiled with warnings.\n')); |
|
|
|
|
|
|
58
|
|
|
console.log(warnings.join('\n\n')); |
|
59
|
|
|
console.log( |
|
60
|
|
|
'\nSearch for the ' + |
|
61
|
|
|
chalk.underline(chalk.yellow('keywords')) + |
|
62
|
|
|
' to learn more about each warning.' |
|
63
|
|
|
); |
|
64
|
|
|
console.log( |
|
65
|
|
|
'To ignore, add ' + |
|
66
|
|
|
chalk.cyan('// eslint-disable-next-line') + |
|
67
|
|
|
' to the line before.\n' |
|
68
|
|
|
); |
|
69
|
|
|
} else { |
|
70
|
|
|
console.log(chalk.green('Compiled successfully.\n')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
console.log('File sizes after gzip:\n'); |
|
74
|
|
|
printFileSizesAfterBuild( |
|
75
|
|
|
stats, |
|
76
|
|
|
previousFileSizes, |
|
77
|
|
|
paths.appBuild, |
|
78
|
|
|
WARN_AFTER_BUNDLE_GZIP_SIZE, |
|
79
|
|
|
WARN_AFTER_CHUNK_GZIP_SIZE |
|
80
|
|
|
); |
|
81
|
|
|
console.log(); |
|
82
|
|
|
|
|
83
|
|
|
const appPackage = require(paths.appPackageJson); |
|
84
|
|
|
const publicUrl = paths.publicUrl; |
|
85
|
|
|
const publicPath = config.output.publicPath; |
|
86
|
|
|
const buildFolder = path.relative(process.cwd(), paths.appBuild); |
|
87
|
|
|
printHostingInstructions( |
|
88
|
|
|
appPackage, |
|
89
|
|
|
publicUrl, |
|
90
|
|
|
publicPath, |
|
91
|
|
|
buildFolder, |
|
92
|
|
|
useYarn |
|
93
|
|
|
); |
|
94
|
|
|
}, |
|
95
|
|
|
err => { |
|
96
|
|
|
console.log(chalk.red('Failed to compile.\n')); |
|
|
|
|
|
|
97
|
|
|
console.log((err.message || err) + '\n'); |
|
98
|
|
|
process.exit(1); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
// Create the production build and print the deployment instructions. |
|
103
|
|
|
function build(previousFileSizes) { |
|
104
|
|
|
console.log('Creating an optimized production build...'); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
let compiler = webpack(config); |
|
107
|
|
|
return new Promise((resolve, reject) => { |
|
108
|
|
|
compiler.run((err, stats) => { |
|
109
|
|
|
if (err) { |
|
110
|
|
|
return reject(err); |
|
111
|
|
|
} |
|
112
|
|
|
const messages = formatWebpackMessages(stats.toJson({}, true)); |
|
113
|
|
|
if (messages.errors.length) { |
|
114
|
|
|
return reject(new Error(messages.errors.join('\n\n'))); |
|
115
|
|
|
} |
|
116
|
|
|
if ( |
|
117
|
|
|
process.env.CI && |
|
118
|
|
|
(typeof process.env.CI !== 'string' || |
|
119
|
|
|
process.env.CI.toLowerCase() !== 'false') && |
|
120
|
|
|
messages.warnings.length |
|
121
|
|
|
) { |
|
122
|
|
|
console.log( |
|
|
|
|
|
|
123
|
|
|
chalk.yellow( |
|
124
|
|
|
'\nTreating warnings as errors because process.env.CI = true.\n' + |
|
125
|
|
|
'Most CI servers set it automatically.\n' |
|
126
|
|
|
) |
|
127
|
|
|
); |
|
128
|
|
|
return reject(new Error(messages.warnings.join('\n\n'))); |
|
129
|
|
|
} |
|
130
|
|
|
return resolve({ |
|
131
|
|
|
stats, |
|
132
|
|
|
previousFileSizes, |
|
133
|
|
|
warnings: messages.warnings, |
|
134
|
|
|
}); |
|
135
|
|
|
}); |
|
136
|
|
|
}); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
function copyPublicFolder() { |
|
140
|
|
|
fs.copySync(paths.appPublic, paths.appBuild, { |
|
141
|
|
|
dereference: true, |
|
142
|
|
|
filter: file => file !== paths.appHtml, |
|
143
|
|
|
}); |
|
144
|
|
|
} |
|
145
|
|
|
|