casterly
Version:
CLI for Casterly
168 lines (167 loc) • 7.15 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Ensure environment variables are read.
require("../config/env");
const path_1 = require("path");
const util = __importStar(require("util"));
const bfj_1 = __importDefault(require("bfj"));
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs-extra"));
const nanoid_1 = require("nanoid");
const webpack_1 = require("webpack");
const fileSizeReporter_1 = require("../build/fileSizeReporter");
const formatWebpackMessages_1 = require("../build/formatWebpackMessages");
const utils_1 = require("../build/utils");
const constants_1 = require("../config/constants");
const createWebpackConfig_1 = __importDefault(require("../config/createWebpackConfig"));
const paths_1 = __importDefault(require("../config/paths"));
const userConfig_1 = __importDefault(require("../config/userConfig"));
const Log = __importStar(require("../output/log"));
// 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.
process.on('unhandledRejection', (err) => {
throw err;
});
// Create the production build and print the deployment instructions.
async function build(previousFileSizes, writeStatsJson = false, profile = false) {
Log.info('Creating an optimized production build...');
const webpackConfigFn = userConfig_1.default.loadWebpackConfig();
const compiler = webpack_1.webpack([
await createWebpackConfig_1.default({
profile,
configFn: webpackConfigFn,
}),
await createWebpackConfig_1.default({
isServer: true,
profile,
configFn: webpackConfigFn,
}),
]);
const run = util.promisify(compiler.run);
let multiStats;
let messages;
try {
multiStats = await run.call(compiler);
messages = formatWebpackMessages_1.formatWebpackMessages(multiStats === null || multiStats === void 0 ? void 0 : multiStats.toJson({ all: false, warnings: true, errors: true }));
}
catch (err) {
if (!err.message) {
throw err;
}
messages = formatWebpackMessages_1.formatWebpackMessages({
errors: [{ message: err.message }],
warnings: [],
});
}
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;
}
throw new Error(messages.errors.join('\n\n'));
}
if (process.env.CI &&
(typeof process.env.CI !== 'string' ||
process.env.CI.toLowerCase() !== 'false') &&
messages.warnings.length) {
Log.warn(chalk_1.default.yellow('\nTreating warnings as errors because process.env.CI = true.\n' +
'Most CI servers set it automatically.\n'));
throw new Error(messages.warnings.join('\n\n'));
}
if (writeStatsJson) {
await bfj_1.default.write(paths_1.default.appBuildFolder + '/bundle-stats.json', multiStats === null || multiStats === void 0 ? void 0 : multiStats.toJson());
}
return {
stats: multiStats,
previousFileSizes,
warnings: messages.warnings,
};
}
function copyPublicFolder() {
if (!fs.existsSync(paths_1.default.appPublic)) {
return;
}
fs.copySync(paths_1.default.appPublic, paths_1.default.appPublicBuildFolder, {
dereference: true,
});
}
function startBuild() {
// 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 (!utils_1.checkRequiredFiles([paths_1.default.appRoutesJs])) {
process.exit(1);
}
// Process CLI arguments
const argv = process.argv.slice(3);
const writeStatsJson = argv.indexOf('--stats') !== -1;
const profile = argv.indexOf('--profile') !== -1;
// First, read the current file sizes in build directory.
// This lets us display how much they changed later.
fileSizeReporter_1.measureFileSizesBeforeBuild(paths_1.default.appBuildFolder)
.then((previousFileSizes) => {
// Remove all content but keep the directory so that
// if you're in it, you don't end up in Trash
fs.emptyDirSync(paths_1.default.appBuildFolder);
// Merge with the public folder
copyPublicFolder();
// Start the webpack build
return build(previousFileSizes, writeStatsJson, profile);
})
.then(({ stats, previousFileSizes, warnings }) => {
if (warnings.length) {
Log.warn(chalk_1.default.yellow('Compiled with warnings.\n'));
console.log(warnings.join('\n\n'));
console.log('\nSearch for the ' +
chalk_1.default.underline(chalk_1.default.yellow('keywords')) +
' to learn more about each warning.');
console.log('To ignore, add ' +
chalk_1.default.cyan('// eslint-disable-next-line') +
' to the line before.\n');
}
else {
Log.ready('Compiled successfully.\n');
}
const buildId = nanoid_1.nanoid();
fs.writeFileSync(path_1.join(paths_1.default.appBuildFolder, constants_1.BUILD_ID_FILE), buildId);
const [clientStats] = stats.stats;
console.log('File sizes after gzip:\n');
fileSizeReporter_1.printFileSizesAfterBuild(clientStats, previousFileSizes, paths_1.default.appBuildFolder, WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE);
}, (err) => {
Log.error('Failed to compile.\n');
utils_1.printBuildError(err);
process.exit(1);
})
.catch((err) => {
if (err === null || err === void 0 ? void 0 : err.message) {
console.log(err.message);
}
process.exit(1);
});
}
exports.default = startBuild;
;