2018-05-08 21:35:56 +00:00
|
|
|
// @flow
|
2018-02-18 16:13:29 +00:00
|
|
|
"use strict";
|
2018-02-17 21:28:47 +00:00
|
|
|
|
|
|
|
// Do this as the first thing so that any code reading it knows the right env.
|
2018-02-18 16:13:29 +00:00
|
|
|
process.env.BABEL_ENV = "production";
|
|
|
|
process.env.NODE_ENV = "production";
|
2018-02-17 21:28:47 +00:00
|
|
|
|
|
|
|
// Makes the script crash on unhandled rejections instead of silently
|
|
|
|
// ignoring them. In the future, promise rejections that are not handled will
|
|
|
|
// terminate the Node.js process with a non-zero exit code.
|
2018-02-18 16:13:29 +00:00
|
|
|
process.on("unhandledRejection", (err) => {
|
2018-02-17 21:28:47 +00:00
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Ensure environment variables are read.
|
2018-02-18 16:13:29 +00:00
|
|
|
require("../config/env");
|
2018-02-17 21:28:47 +00:00
|
|
|
|
2018-02-18 16:13:29 +00:00
|
|
|
const path = require("path");
|
|
|
|
const chalk = require("chalk");
|
|
|
|
const fs = require("fs-extra");
|
|
|
|
const webpack = require("webpack");
|
|
|
|
const config = require("../config/webpack.config.prod");
|
|
|
|
const paths = require("../config/paths");
|
|
|
|
const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles");
|
|
|
|
const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages");
|
|
|
|
const FileSizeReporter = require("react-dev-utils/FileSizeReporter");
|
|
|
|
const printBuildError = require("react-dev-utils/printBuildError");
|
2018-02-17 21:28:47 +00:00
|
|
|
|
|
|
|
const measureFileSizesBeforeBuild =
|
|
|
|
FileSizeReporter.measureFileSizesBeforeBuild;
|
|
|
|
const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
|
|
|
|
const useYarn = fs.existsSync(paths.yarnLockFile);
|
|
|
|
|
|
|
|
// These sizes are pretty large. We'll warn for bundles exceeding them.
|
|
|
|
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
|
|
|
|
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
|
|
|
|
|
|
|
|
// Warn and crash if required files are missing
|
|
|
|
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, read the current file sizes in build directory.
|
|
|
|
// This lets us display how much they changed later.
|
|
|
|
measureFileSizesBeforeBuild(paths.appBuild)
|
2018-02-18 16:13:29 +00:00
|
|
|
.then((previousFileSizes) => {
|
2018-02-17 21:28:47 +00:00
|
|
|
// Remove all content but keep the directory so that
|
|
|
|
// if you're in it, you don't end up in Trash
|
|
|
|
fs.emptyDirSync(paths.appBuild);
|
|
|
|
// Merge with the public folder
|
|
|
|
copyPublicFolder();
|
|
|
|
// Start the webpack build
|
|
|
|
return build(previousFileSizes);
|
|
|
|
})
|
|
|
|
.then(
|
2018-02-18 16:13:29 +00:00
|
|
|
({stats, previousFileSizes, warnings}) => {
|
2018-02-17 21:28:47 +00:00
|
|
|
if (warnings.length) {
|
2018-02-18 16:13:29 +00:00
|
|
|
console.log(chalk.yellow("Compiled with warnings.\n"));
|
|
|
|
console.log(warnings.join("\n\n"));
|
2018-02-17 21:28:47 +00:00
|
|
|
console.log(
|
2018-02-18 16:13:29 +00:00
|
|
|
"\nSearch for the " +
|
|
|
|
chalk.underline(chalk.yellow("keywords")) +
|
|
|
|
" to learn more about each warning."
|
2018-02-17 21:28:47 +00:00
|
|
|
);
|
|
|
|
console.log(
|
2018-02-18 16:13:29 +00:00
|
|
|
"To ignore, add " +
|
|
|
|
chalk.cyan("// eslint-disable-next-line") +
|
|
|
|
" to the line before.\n"
|
2018-02-17 21:28:47 +00:00
|
|
|
);
|
|
|
|
} else {
|
2018-02-18 16:13:29 +00:00
|
|
|
console.log(chalk.green("Compiled successfully.\n"));
|
2018-02-17 21:28:47 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 16:13:29 +00:00
|
|
|
console.log("File sizes after gzip:\n");
|
2018-02-17 21:28:47 +00:00
|
|
|
printFileSizesAfterBuild(
|
|
|
|
stats,
|
|
|
|
previousFileSizes,
|
|
|
|
paths.appBuild,
|
|
|
|
WARN_AFTER_BUNDLE_GZIP_SIZE,
|
|
|
|
WARN_AFTER_CHUNK_GZIP_SIZE
|
|
|
|
);
|
|
|
|
console.log();
|
|
|
|
},
|
2018-02-18 16:13:29 +00:00
|
|
|
(err) => {
|
|
|
|
console.log(chalk.red("Failed to compile.\n"));
|
2018-02-17 21:28:47 +00:00
|
|
|
printBuildError(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create the production build and print the deployment instructions.
|
|
|
|
function build(previousFileSizes) {
|
2018-02-18 16:13:29 +00:00
|
|
|
console.log("Creating an optimized production build...");
|
2018-02-17 21:28:47 +00:00
|
|
|
|
|
|
|
let compiler = webpack(config);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
const messages = formatWebpackMessages(stats.toJson({}, true));
|
|
|
|
if (messages.errors.length) {
|
|
|
|
// Only keep the first error. Others are often indicative
|
|
|
|
// of the same problem, but confuse the reader with noise.
|
|
|
|
if (messages.errors.length > 1) {
|
|
|
|
messages.errors.length = 1;
|
|
|
|
}
|
2018-02-18 16:13:29 +00:00
|
|
|
return reject(new Error(messages.errors.join("\n\n")));
|
2018-02-17 21:28:47 +00:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
process.env.CI &&
|
2018-02-18 16:13:29 +00:00
|
|
|
(typeof process.env.CI !== "string" ||
|
|
|
|
process.env.CI.toLowerCase() !== "false") &&
|
2018-02-17 21:28:47 +00:00
|
|
|
messages.warnings.length
|
|
|
|
) {
|
|
|
|
console.log(
|
|
|
|
chalk.yellow(
|
2018-02-18 16:13:29 +00:00
|
|
|
"\nTreating warnings as errors because process.env.CI = true.\n" +
|
|
|
|
"Most CI servers set it automatically.\n"
|
2018-02-17 21:28:47 +00:00
|
|
|
)
|
|
|
|
);
|
2018-02-18 16:13:29 +00:00
|
|
|
return reject(new Error(messages.warnings.join("\n\n")));
|
2018-02-17 21:28:47 +00:00
|
|
|
}
|
|
|
|
return resolve({
|
|
|
|
stats,
|
|
|
|
previousFileSizes,
|
|
|
|
warnings: messages.warnings,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyPublicFolder() {
|
|
|
|
fs.copySync(paths.appPublic, paths.appBuild, {
|
|
|
|
dereference: true,
|
2018-02-18 16:13:29 +00:00
|
|
|
filter: (file) => file !== paths.appHtml,
|
2018-02-17 21:28:47 +00:00
|
|
|
});
|
|
|
|
}
|