UNPKG

@procore/core-scripts

Version:

A CLI to enhance your development experience

167 lines 7.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const command_1 = require("@oclif/command"); const cli_highlight_1 = require("cli-highlight"); const core_1 = require("@oclif/core"); const fsExtra = tslib_1.__importStar(require("fs-extra")); const path_1 = tslib_1.__importDefault(require("path")); const FileSizeReporter_1 = tslib_1.__importDefault(require("react-dev-utils/FileSizeReporter")); const formatWebpackMessages_1 = tslib_1.__importDefault(require("react-dev-utils/formatWebpackMessages")); const printHostingInstructions_1 = tslib_1.__importDefault(require("react-dev-utils/printHostingInstructions")); const webpack_1 = tslib_1.__importDefault(require("webpack")); const BaseCommand_1 = require("../../BaseCommand"); const paths_1 = require("../../paths"); const app_1 = require("../../webpack/app"); const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024; const WARN_AFTER_CHUNK_GZIP_SIZE = 3024 * 1024; // Should we use 1024 * 1024? class AppBuildCommand extends BaseCommand_1.BaseCommand { constructor() { super(...arguments); this.env = 'production'; } async run() { // eslint-disable-next-line no-shadow const { flags } = this.parse(AppBuildCommand); const publicPath = this.getPublicPath(flags); const config = (0, app_1.configFactory)({ env: 'production', workspace: this.workspace, publicPath, railsMode: flags.railsMode, analyze: flags.analyze, sourceMapPublicPath: flags.sourceMapPublicPath, }); if (flags.inspect) { // @ts-ignore Fix: type spec const output = config.toString({ verbose: false }); this.log((0, cli_highlight_1.highlight)(output, { language: 'js' })); this.exit(0); } const webpackConfig = config.toConfig(); const previousFileSizes = await this.measureFileSizesBeforeBuild(); await this.emptyBuildFolder(); this.copyPublicFolder(); const { stats } = await this.build(webpackConfig); this.printFileSizesAfterBuild(stats, previousFileSizes); this.showHostingInstructions(webpackConfig); } getPublicPath(opts) { const publicPath = (() => { const { ASSET_PATH } = process.env; if (ASSET_PATH) { return ASSET_PATH; } if (opts.publicPath) { return opts.publicPath; } const pkg = this.workspace.packageJson; if (pkg.homepage) { return pkg.homepage; } return '/'; })(); return (0, paths_1.withSlash)(publicPath); } measureFileSizesBeforeBuild() { this.debug('Measure file sizes'); return FileSizeReporter_1.default.measureFileSizesBeforeBuild(this.workspace.resolve('build')); } async emptyBuildFolder() { this.debug(`Emptying ${this.workspace.resolve('build')}`); fsExtra.emptyDirSync(this.workspace.resolve('build')); } copyPublicFolder() { if (!this.workspace.existsSync('public')) { return; } this.debug(`Copying ${this.workspace.resolve('public')} into ${this.workspace.resolve('build')}`); fsExtra.copySync(this.workspace.resolve('public'), this.workspace.resolve('build'), { dereference: true, filter: (file) => file !== this.workspace.resolve('public', 'index.html'), }); } printFileSizesAfterBuild(stats, previousFileSizes) { this.log('\nFile sizes after gzip:\n'); FileSizeReporter_1.default.printFileSizesAfterBuild(stats, previousFileSizes, this.workspace.resolve('build'), WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE); this.newline(); } showHostingInstructions(webpackConfig) { const appPackage = this.workspace.packageJson; const publicPath = String(webpackConfig.output.publicPath); const publicUrl = (0, paths_1.withoutSlash)(publicPath); (0, printHostingInstructions_1.default)(appPackage, publicUrl, publicPath, path_1.default.relative(this.workspace.context, this.workspace.resolve('build')), true); } async build(webpackConfig) { core_1.CliUx.ux.action.start('Compiling'); const compiler = (0, webpack_1.default)(webpackConfig); return new Promise((resolve, reject) => { compiler.run((err, result) => { let messages; if (err) { if (!err.message) { core_1.CliUx.ux.action.stop('Compilation Failed'); reject(err); return; } messages = (0, formatWebpackMessages_1.default)({ errors: [err.message], warnings: [], }); } else { try { messages = (0, formatWebpackMessages_1.default)(result === null || result === void 0 ? void 0 : result.toJson({ all: false, warnings: true, errors: true })); } catch (e) { messages = { errors: [], 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; } core_1.CliUx.ux.action.stop('Compilation Failed'); reject(new Error(messages.errors.join('\n\n'))); return; } const params = compiler.newCompilationParams(); const compilation = new webpack_1.default.Compilation(compiler, params); const resolveArgs = { stats: result || new webpack_1.default.Stats(compilation), warnings: messages.warnings, }; core_1.CliUx.ux.action.stop('Compiled successfully'); resolve(resolveArgs); }); }); } } AppBuildCommand.description = 'Compiles the application.'; AppBuildCommand.flags = { analyze: command_1.flags.boolean({ default: false, description: 'Run webpack bundle analyzer.', }), railsMode: command_1.flags.boolean({ default: false, description: 'Enables Procore Rails integration.', }), inspect: command_1.flags.boolean({ default: false, description: 'Prints the Webpack configuration.', }), sourceMapPublicPath: command_1.flags.string({ description: 'Base path for all the sourcemap.', }), publicPath: command_1.flags.string({ description: [ 'Allows you to specify the base path for all the assets within', 'your application.', ].join(' '), }), }; exports.default = AppBuildCommand; //# sourceMappingURL=build.js.map