frontend/scripts/start.js

152 lines
5.2 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.
2023-09-18 10:32:31 +08:00
process.env.BABEL_ENV = "development"
process.env.NODE_ENV = "development"
2024-03-03 15:45:00 +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-02-29 09:13:39 +08:00
2024-03-03 15:45:00 +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-18 10:32:31 +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 fs = require("fs")
const chalk = require("react-dev-utils/chalk")
const webpack = require("webpack")
const WebpackDevServer = require("webpack-dev-server")
const clearConsole = require("react-dev-utils/clearConsole")
const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles")
2021-10-26 16:08:21 +08:00
const {
choosePort,
createCompiler,
prepareProxy,
2023-09-18 10:32:31 +08:00
prepareUrls
} = require("react-dev-utils/WebpackDevServerUtils")
const openBrowser = require("react-dev-utils/openBrowser")
const semver = require("semver")
const paths = require("../config/paths")
const configFactory = require("../config/webpack.config")
const createDevServerConfig = require("../config/webpackDevServer.config")
const getClientEnvironment = require("../config/env")
const react = require(require.resolve("react", { paths: [paths.appPath] }))
const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1))
const useYarn = fs.existsSync(paths.yarnLockFile)
const isInteractive = process.stdout.isTTY
2021-10-26 16:08:21 +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
}
// Tools like Cloud9 rely on this.
2023-09-18 10:32:31 +08:00
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000
const HOST = process.env.HOST || "0.0.0.0"
2021-10-26 16:08:21 +08:00
if (process.env.HOST) {
console.log(
chalk.cyan(
`Attempting to bind to HOST environment variable: ${chalk.yellow(
chalk.bold(process.env.HOST)
)}`
)
2023-09-18 10:32:31 +08:00
)
console.log(`If this was unintentional, check that you haven't mistakenly set it in your shell.`)
console.log(`Learn more here: ${chalk.yellow("https://cra.link/advanced-config")}`)
console.log()
2021-10-26 16:08:21 +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-10-26 16:08:21 +08:00
checkBrowsers(paths.appPath, isInteractive)
.then(() => {
// We attempt to use the default port but if it is busy, we offer the user to
// run on a different port. `choosePort()` Promise resolves to the next free port.
2023-09-18 10:32:31 +08:00
return choosePort(HOST, DEFAULT_PORT)
2021-10-26 16:08:21 +08:00
})
2023-09-18 10:32:31 +08:00
.then((port) => {
2021-10-26 16:08:21 +08:00
if (port == null) {
// We have not found a port.
2023-09-18 10:32:31 +08:00
return
2021-10-26 16:08:21 +08:00
}
2023-09-18 10:32:31 +08:00
const config = configFactory("development")
const protocol = process.env.HTTPS === "true" ? "https" : "http"
const appName = require(paths.appPackageJson).name
2021-10-26 16:08:21 +08:00
2023-09-18 10:32:31 +08:00
const useTypeScript = fs.existsSync(paths.appTsConfig)
const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === "true"
const urls = prepareUrls(protocol, HOST, port, paths.publicUrlOrPath.slice(0, -1))
2021-10-26 16:08:21 +08:00
const devSocket = {
2023-09-18 10:32:31 +08:00
warnings: (warnings) => devServer.sockWrite(devServer.sockets, "warnings", warnings),
errors: (errors) => devServer.sockWrite(devServer.sockets, "errors", errors)
}
2021-10-26 16:08:21 +08:00
// Create a webpack compiler that is configured with custom messages.
const compiler = createCompiler({
appName,
config,
devSocket,
urls,
useYarn,
useTypeScript,
tscCompileOnError,
2023-09-18 10:32:31 +08:00
webpack
})
2021-10-26 16:08:21 +08:00
// Load proxy config
2023-09-18 10:32:31 +08:00
const proxySetting = require(paths.appPackageJson).proxy
const proxyConfig = prepareProxy(proxySetting, paths.appPublic, paths.publicUrlOrPath)
2021-10-26 16:08:21 +08:00
// Serve webpack assets generated by the compiler over a web server.
2023-09-18 10:32:31 +08:00
const serverConfig = createDevServerConfig(proxyConfig, urls.lanUrlForConfig)
const devServer = new WebpackDevServer(compiler, serverConfig)
2021-10-26 16:08:21 +08:00
// Launch WebpackDevServer.
2023-09-18 10:32:31 +08:00
devServer.listen(port, HOST, (err) => {
2021-10-26 16:08:21 +08:00
if (err) {
2023-09-18 10:32:31 +08:00
return console.log(err)
2021-10-26 16:08:21 +08:00
}
if (isInteractive) {
2023-09-18 10:32:31 +08:00
clearConsole()
2021-10-26 16:08:21 +08:00
}
2023-09-18 10:32:31 +08:00
if (env.raw.FAST_REFRESH && semver.lt(react.version, "16.10.0")) {
2021-10-26 16:08:21 +08:00
console.log(
chalk.yellow(
`Fast Refresh requires React 16.10 or higher. You are using React ${react.version}.`
)
2023-09-18 10:32:31 +08:00
)
2021-10-26 16:08:21 +08:00
}
2023-09-18 10:32:31 +08:00
console.log(chalk.cyan("Starting the development server...\n"))
openBrowser(urls.localUrlForBrowser)
})
;["SIGINT", "SIGTERM"].forEach(function (sig) {
2021-10-26 16:08:21 +08:00
process.on(sig, function () {
2023-09-18 10:32:31 +08:00
devServer.close()
process.exit()
})
})
2021-10-26 16:08:21 +08:00
2023-09-18 10:32:31 +08:00
if (process.env.CI !== "true") {
2021-10-26 16:08:21 +08:00
// Gracefully exit when stdin ends
2023-09-18 10:32:31 +08:00
process.stdin.on("end", function () {
devServer.close()
process.exit()
})
2021-10-26 16:08:21 +08:00
}
})
2023-09-18 10:32:31 +08:00
.catch((err) => {
2021-10-26 16:08:21 +08:00
if (err && err.message) {
2023-09-18 10:32:31 +08:00
console.log(err.message)
2021-10-26 16:08:21 +08:00
}
2023-09-18 10:32:31 +08:00
process.exit(1)
})