Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/scripts/build.js
'use strict'; | |||||
// Do this as the first thing so that any code reading it knows the right env. | // Do this as the first thing so that any code reading it knows the right env. | ||||
process.env.BABEL_ENV = 'production'; | process.env.BABEL_ENV = 'production'; | ||||
process.env.NODE_ENV = 'production'; | process.env.NODE_ENV = 'production'; | ||||
// Makes the script crash on unhandled rejections instead of silently | // Makes the script crash on unhandled rejections instead of silently | ||||
// ignoring them. In the future, promise rejections that are not handled will | // ignoring them. In the future, promise rejections that are not handled will | ||||
// terminate the Node.js process with a non-zero exit code. | // terminate the Node.js process with a non-zero exit code. | ||||
process.on('unhandledRejection', err => { | process.on('unhandledRejection', err => { | ||||
throw err; | throw err; | ||||
}); | }); | ||||
// Ensure environment variables are read. | // Ensure environment variables are read. | ||||
require('../config/env'); | require('../config/env'); | ||||
const path = require('path'); | const path = require('path'); | ||||
const chalk = require('react-dev-utils/chalk'); | const chalk = require('react-dev-utils/chalk'); | ||||
const fs = require('fs-extra'); | const fs = require('fs-extra'); | ||||
const bfj = require('bfj'); | |||||
const webpack = require('webpack'); | const webpack = require('webpack'); | ||||
const configFactory = require('../config/webpack.config'); | const configFactory = require('../config/webpack.config'); | ||||
const paths = require('../config/paths'); | const paths = require('../config/paths'); | ||||
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); | const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); | ||||
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages'); | const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages'); | ||||
const printHostingInstructions = require('react-dev-utils/printHostingInstructions'); | const printHostingInstructions = require('react-dev-utils/printHostingInstructions'); | ||||
const FileSizeReporter = require('react-dev-utils/FileSizeReporter'); | const FileSizeReporter = require('react-dev-utils/FileSizeReporter'); | ||||
const printBuildError = require('react-dev-utils/printBuildError'); | const printBuildError = require('react-dev-utils/printBuildError'); | ||||
Show All 9 Lines | |||||
const isInteractive = process.stdout.isTTY; | const isInteractive = process.stdout.isTTY; | ||||
// Warn and crash if required files are missing | // Warn and crash if required files are missing | ||||
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { | if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { | ||||
process.exit(1); | process.exit(1); | ||||
} | } | ||||
const argv = process.argv.slice(2); | |||||
const writeStatsJson = argv.indexOf('--stats') !== -1; | |||||
// Generate configuration | // Generate configuration | ||||
const config = configFactory('production'); | const config = configFactory('production'); | ||||
// We require that you explicitly set browsers and do not fall back to | // We require that you explicitly set browsers and do not fall back to | ||||
// browserslist defaults. | // browserslist defaults. | ||||
const { checkBrowsers } = require('react-dev-utils/browsersHelper'); | const { checkBrowsers } = require('react-dev-utils/browsersHelper'); | ||||
checkBrowsers(paths.appPath, isInteractive) | checkBrowsers(paths.appPath, isInteractive) | ||||
.then(() => { | .then(() => { | ||||
Show All 35 Lines | .then( | ||||
previousFileSizes, | previousFileSizes, | ||||
paths.appBuild, | paths.appBuild, | ||||
WARN_AFTER_BUNDLE_GZIP_SIZE, | WARN_AFTER_BUNDLE_GZIP_SIZE, | ||||
WARN_AFTER_CHUNK_GZIP_SIZE, | WARN_AFTER_CHUNK_GZIP_SIZE, | ||||
); | ); | ||||
console.log(); | console.log(); | ||||
const appPackage = require(paths.appPackageJson); | const appPackage = require(paths.appPackageJson); | ||||
const publicUrl = paths.publicUrl; | const publicUrl = paths.publicUrlOrPath; | ||||
const publicPath = config.output.publicPath; | const publicPath = config.output.publicPath; | ||||
const buildFolder = path.relative(process.cwd(), paths.appBuild); | const buildFolder = path.relative(process.cwd(), paths.appBuild); | ||||
printHostingInstructions( | printHostingInstructions( | ||||
appPackage, | appPackage, | ||||
publicUrl, | publicUrl, | ||||
publicPath, | publicPath, | ||||
buildFolder, | buildFolder, | ||||
useYarn, | useYarn, | ||||
); | ); | ||||
}, | }, | ||||
err => { | err => { | ||||
const tscCompileOnError = | |||||
process.env.TSC_COMPILE_ON_ERROR === 'true'; | |||||
if (tscCompileOnError) { | |||||
console.log( | |||||
chalk.yellow( | |||||
'Compiled with the following type errors (you may want to check these before deploying your app):\n', | |||||
), | |||||
); | |||||
printBuildError(err); | |||||
} else { | |||||
console.log(chalk.red('Failed to compile.\n')); | console.log(chalk.red('Failed to compile.\n')); | ||||
printBuildError(err); | printBuildError(err); | ||||
process.exit(1); | process.exit(1); | ||||
} | |||||
}, | }, | ||||
) | ) | ||||
.catch(err => { | .catch(err => { | ||||
if (err && err.message) { | if (err && err.message) { | ||||
console.log(err.message); | console.log(err.message); | ||||
} | } | ||||
process.exit(1); | process.exit(1); | ||||
}); | }); | ||||
// Create the production build and print the deployment instructions. | // Create the production build and print the deployment instructions. | ||||
function build(previousFileSizes) { | function build(previousFileSizes) { | ||||
// We used to support resolving modules according to `NODE_PATH`. | |||||
// This now has been deprecated in favor of jsconfig/tsconfig.json | |||||
// This lets you use absolute paths in imports inside large monorepos: | |||||
if (process.env.NODE_PATH) { | |||||
console.log( | |||||
chalk.yellow( | |||||
'Setting NODE_PATH to resolve modules absolutely has been deprecated in favor of setting baseUrl in jsconfig.json (or tsconfig.json if you are using TypeScript) and will be removed in a future major release of create-react-app.', | |||||
), | |||||
); | |||||
console.log(); | |||||
} | |||||
console.log('Creating an optimized production build...'); | console.log('Creating an optimized production build...'); | ||||
const compiler = webpack(config); | const compiler = webpack(config); | ||||
return new Promise((resolve, reject) => { | return new Promise((resolve, reject) => { | ||||
compiler.run((err, stats) => { | compiler.run((err, stats) => { | ||||
let messages; | let messages; | ||||
if (err) { | if (err) { | ||||
if (!err.message) { | if (!err.message) { | ||||
return reject(err); | return reject(err); | ||||
} | } | ||||
let errMessage = err.message; | |||||
// Add additional information for postcss errors | |||||
if (Object.prototype.hasOwnProperty.call(err, 'postcssNode')) { | |||||
errMessage += | |||||
'\nCompileError: Begins at CSS selector ' + | |||||
err['postcssNode'].selector; | |||||
} | |||||
messages = formatWebpackMessages({ | messages = formatWebpackMessages({ | ||||
errors: [err.message], | errors: [errMessage], | ||||
warnings: [], | warnings: [], | ||||
}); | }); | ||||
} else { | } else { | ||||
messages = formatWebpackMessages( | messages = formatWebpackMessages( | ||||
stats.toJson({ all: false, warnings: true, errors: true }), | stats.toJson({ all: false, warnings: true, errors: true }), | ||||
); | ); | ||||
} | } | ||||
if (messages.errors.length) { | if (messages.errors.length) { | ||||
// Only keep the first error. Others are often indicative | // Only keep the first error. Others are often indicative | ||||
// of the same problem, but confuse the reader with noise. | // of the same problem, but confuse the reader with noise. | ||||
if (messages.errors.length > 1) { | if (messages.errors.length > 1) { | ||||
messages.errors.length = 1; | messages.errors.length = 1; | ||||
} | } | ||||
return reject(new Error(messages.errors.join('\n\n'))); | return reject(new Error(messages.errors.join('\n\n'))); | ||||
} | } | ||||
if ( | if ( | ||||
process.env.CI && | process.env.CI && | ||||
(typeof process.env.CI !== 'string' || | (typeof process.env.CI !== 'string' || | ||||
process.env.CI.toLowerCase() !== 'false') && | process.env.CI.toLowerCase() !== 'false') && | ||||
messages.warnings.length | messages.warnings.length | ||||
) { | ) { | ||||
// Ignore sourcemap warnings in CI builds. See #8227 for more info. | |||||
const filteredWarnings = messages.warnings.filter( | |||||
w => !/Failed to parse source map/.test(w), | |||||
); | |||||
if (filteredWarnings.length) { | |||||
console.log( | console.log( | ||||
chalk.yellow( | chalk.yellow( | ||||
'\nTreating warnings as errors because process.env.CI = true.\n' + | '\nTreating warnings as errors because process.env.CI = true.\n' + | ||||
'Most CI servers set it automatically.\n', | 'Most CI servers set it automatically.\n', | ||||
), | ), | ||||
); | ); | ||||
return reject(new Error(messages.warnings.join('\n\n'))); | return reject(new Error(filteredWarnings.join('\n\n'))); | ||||
} | |||||
} | } | ||||
return resolve({ | const resolveArgs = { | ||||
stats, | stats, | ||||
previousFileSizes, | previousFileSizes, | ||||
warnings: messages.warnings, | warnings: messages.warnings, | ||||
}); | }; | ||||
if (writeStatsJson) { | |||||
return bfj | |||||
.write( | |||||
paths.appBuild + '/bundle-stats.json', | |||||
stats.toJson(), | |||||
) | |||||
.then(() => resolve(resolveArgs)) | |||||
.catch(error => reject(new Error(error))); | |||||
} | |||||
return resolve(resolveArgs); | |||||
}); | }); | ||||
}); | }); | ||||
} | } | ||||
function copyPublicFolder() { | function copyPublicFolder() { | ||||
fs.copySync(paths.appPublic, paths.appBuild, { | fs.copySync(paths.appPublic, paths.appBuild, { | ||||
dereference: true, | dereference: true, | ||||
filter: file => file !== paths.appHtml, | filter: file => file !== paths.appHtml, | ||||
}); | }); | ||||
} | } |