frontend/scripts/test.js

205 lines
6.7 KiB
JavaScript
Raw Normal View History

2021-10-26 16:08:21 +08:00
// Do this as the first thing so that any code reading it knows the right env.
2024-03-07 10:49:39 +08:00
process.env.BABEL_ENV = "test"
process.env.NODE_ENV = "test"
2024-03-11 09:21:56 +08:00
// 测试
2024-04-01 14:52:56 +08:00
// process.env.BASE_URL = "http://test.marketapi.1688sup.com"
// process.env.UNIFIED_API = "http://api.test.user.1688sup.com/v1"
// process.env.UNIFIED_URL = "http://test.user.1688sup.com/#/login"
// 镜像
2024-04-01 14:52:56 +08:00
process.env.BASE_URL = "http://pre.marketapi.1688sup.com"
process.env.UNIFIED_API = "http://api.gray.user.1688sup.com/v1"
process.env.UNIFIED_URL = "http://gray.user.1688sup.com/#/login"
2023-09-20 15:43:47 +08:00
2021-10-26 16:08:21 +08: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.
2023-09-18 10:32:31 +08:00
process.on("unhandledRejection", (err) => {
throw err
})
2021-10-26 16:08:21 +08:00
// Ensure environment variables are read.
2023-09-18 10:32:31 +08:00
require("../config/env")
const path = require("path")
const chalk = require("react-dev-utils/chalk")
const fs = require("fs-extra")
const bfj = require("bfj")
const webpack = require("webpack")
const configFactory = require("../config/webpack.config")
const paths = require("../config/paths")
const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles")
const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages")
const printHostingInstructions = require("react-dev-utils/printHostingInstructions")
const FileSizeReporter = require("react-dev-utils/FileSizeReporter")
const printBuildError = require("react-dev-utils/printBuildError")
const measureFileSizesBeforeBuild = FileSizeReporter.measureFileSizesBeforeBuild
const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild
const useYarn = fs.existsSync(paths.yarnLockFile)
2021-10-26 16:08:21 +08:00
2021-12-03 15:03:49 +08:00
// These sizes are pretty large. We'll warn for bundles exceeding them.
2023-09-18 10:32:31 +08:00
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024
2021-10-26 16:08:21 +08:00
2023-09-18 10:32:31 +08:00
const isInteractive = process.stdout.isTTY
2021-12-03 15:03:49 +08:00
// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
2023-09-18 10:32:31 +08:00
process.exit(1)
2021-10-26 16:08:21 +08:00
}
2023-09-18 10:32:31 +08:00
const argv = process.argv.slice(2)
const writeStatsJson = argv.indexOf("--stats") !== -1
2021-12-03 15:03:49 +08:00
// Generate configuration
2023-09-18 10:32:31 +08:00
const config = configFactory("production")
2021-12-03 15:03:49 +08:00
// We require that you explicitly set browsers and do not fall back to
// browserslist defaults.
2023-09-18 10:32:31 +08:00
const { checkBrowsers } = require("react-dev-utils/browsersHelper")
2021-12-03 15:03:49 +08:00
checkBrowsers(paths.appPath, isInteractive)
.then(() => {
// First, read the current file sizes in build directory.
// This lets us display how much they changed later.
2023-09-18 10:32:31 +08:00
return measureFileSizesBeforeBuild(paths.appBuild)
2021-12-03 15:03:49 +08:00
})
2023-09-18 10:32:31 +08:00
.then((previousFileSizes) => {
2021-12-03 15:03:49 +08:00
// Remove all content but keep the directory so that
// if you're in it, you don't end up in Trash
2023-09-18 10:32:31 +08:00
fs.emptyDirSync(paths.appBuild)
2021-12-03 15:03:49 +08:00
// Merge with the public folder
2023-09-18 10:32:31 +08:00
copyPublicFolder()
2021-12-03 15:03:49 +08:00
// Start the webpack build
2023-09-18 10:32:31 +08:00
return build(previousFileSizes)
2021-12-03 15:03:49 +08:00
})
.then(
({ stats, previousFileSizes, warnings }) => {
if (warnings.length) {
2023-09-18 10:32:31 +08:00
console.log(chalk.yellow("Compiled with warnings.\n"))
console.log(warnings.join("\n\n"))
2021-12-03 15:03:49 +08:00
console.log(
2023-09-18 10:32:31 +08:00
"\nSearch for the " +
chalk.underline(chalk.yellow("keywords")) +
" to learn more about each warning."
2023-09-18 10:32:31 +08:00
)
2021-12-03 15:03:49 +08:00
console.log(
2023-09-18 10:32:31 +08:00
"To ignore, add " + chalk.cyan("// eslint-disable-next-line") + " to the line before.\n"
)
2021-12-03 15:03:49 +08:00
} else {
2023-09-18 10:32:31 +08:00
console.log(chalk.green("Compiled successfully.\n"))
2021-12-03 15:03:49 +08:00
}
2023-09-18 10:32:31 +08:00
console.log("File sizes after gzip:\n")
2021-12-03 15:03:49 +08:00
printFileSizesAfterBuild(
stats,
previousFileSizes,
paths.appBuild,
WARN_AFTER_BUNDLE_GZIP_SIZE,
WARN_AFTER_CHUNK_GZIP_SIZE
2023-09-18 10:32:31 +08:00
)
console.log()
const appPackage = require(paths.appPackageJson)
const publicUrl = paths.publicUrlOrPath
const publicPath = config.output.publicPath
const buildFolder = path.relative(process.cwd(), paths.appBuild)
printHostingInstructions(appPackage, publicUrl, publicPath, buildFolder, useYarn)
2021-12-03 15:03:49 +08:00
},
2023-09-18 10:32:31 +08:00
(err) => {
const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === "true"
2021-12-03 15:03:49 +08:00
if (tscCompileOnError) {
console.log(
chalk.yellow(
2023-09-18 10:32:31 +08:00
"Compiled with the following type errors (you may want to check these before deploying your app):\n"
2021-12-03 15:03:49 +08:00
)
2023-09-18 10:32:31 +08:00
)
printBuildError(err)
2021-12-03 15:03:49 +08:00
} else {
2023-09-18 10:32:31 +08:00
console.log(chalk.red("Failed to compile.\n"))
printBuildError(err)
process.exit(1)
2021-12-03 15:03:49 +08:00
}
}
)
2023-09-18 10:32:31 +08:00
.catch((err) => {
2021-12-03 15:03:49 +08:00
if (err && err.message) {
2023-09-18 10:32:31 +08:00
console.log(err.message)
2021-12-03 15:03:49 +08:00
}
2023-09-18 10:32:31 +08:00
process.exit(1)
})
2021-12-03 15:03:49 +08:00
// Create the production build and print the deployment instructions.
function build(previousFileSizes) {
2023-09-18 10:32:31 +08:00
console.log("Creating an optimized production build...")
2021-10-26 16:08:21 +08:00
2023-09-18 10:32:31 +08:00
const compiler = webpack(config)
2021-12-03 15:03:49 +08:00
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
2023-09-18 10:32:31 +08:00
let messages
2021-12-03 15:03:49 +08:00
if (err) {
if (!err.message) {
2023-09-18 10:32:31 +08:00
return reject(err)
2021-12-03 15:03:49 +08:00
}
2023-09-18 10:32:31 +08:00
let errMessage = err.message
2021-12-03 15:03:49 +08:00
// Add additional information for postcss errors
2023-09-18 10:32:31 +08:00
if (Object.prototype.hasOwnProperty.call(err, "postcssNode")) {
errMessage += "\nCompileError: Begins at CSS selector " + err["postcssNode"].selector
2021-12-03 15:03:49 +08:00
}
messages = formatWebpackMessages({
errors: [errMessage],
2023-09-18 10:32:31 +08:00
warnings: []
})
2021-12-03 15:03:49 +08:00
} else {
2023-09-18 10:32:31 +08:00
messages = formatWebpackMessages(stats.toJson({ all: false, warnings: true, errors: true }))
2021-12-03 15:03:49 +08:00
}
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) {
2023-09-18 10:32:31 +08:00
messages.errors.length = 1
2021-12-03 15:03:49 +08:00
}
2023-09-18 10:32:31 +08:00
return reject(new Error(messages.errors.join("\n\n")))
2021-12-03 15:03:49 +08:00
}
if (
process.env.CI &&
2023-09-18 10:32:31 +08:00
(typeof process.env.CI !== "string" || process.env.CI.toLowerCase() !== "false") &&
2021-12-03 15:03:49 +08:00
messages.warnings.length
) {
console.log(
chalk.yellow(
2023-09-18 10:32:31 +08:00
"\nTreating warnings as errors because process.env.CI = true.\n" +
"Most CI servers set it automatically.\n"
2021-12-03 15:03:49 +08:00
)
2023-09-18 10:32:31 +08:00
)
return reject(new Error(messages.warnings.join("\n\n")))
2021-12-03 15:03:49 +08:00
}
const resolveArgs = {
stats,
previousFileSizes,
2023-09-18 10:32:31 +08:00
warnings: messages.warnings
}
2021-12-03 15:03:49 +08:00
if (writeStatsJson) {
return bfj
2023-09-18 10:32:31 +08:00
.write(paths.appBuild + "/bundle-stats.json", stats.toJson())
2021-12-03 15:03:49 +08:00
.then(() => resolve(resolveArgs))
2023-09-18 10:32:31 +08:00
.catch((error) => reject(new Error(error)))
2021-12-03 15:03:49 +08:00
}
2023-09-18 10:32:31 +08:00
return resolve(resolveArgs)
})
})
2021-12-03 15:03:49 +08:00
}
function copyPublicFolder() {
fs.copySync(paths.appPublic, paths.appBuild, {
dereference: true,
2023-09-18 10:32:31 +08:00
filter: (file) => file !== paths.appHtml
})
2021-12-03 15:03:49 +08:00
}