@cisdi/pangu
Version:
develop tool for ui-engine project
99 lines (94 loc) • 2.81 kB
JavaScript
const errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware')
const evalSourceMapMiddleware = require('react-dev-utils/evalSourceMapMiddleware')
const noopServiceWorkerMiddleware = require('react-dev-utils/noopServiceWorkerMiddleware')
const ignoredFiles = require('react-dev-utils/ignoredFiles')
const fs = require('fs')
const {
srcDir,
publicDir,
publicPath,
serverConfigPath,
pkgPath,
} = require('../utils/paths')
const { readJSONFileSync } = require('../utils/read-json')
let cache = null
const getConfig = (host, port) => {
const baseConfig = {
port,
host,
publicPath,
quiet: true,
compress: true,
noInfo: true,
hot: true,
overlay: false,
disableHostCheck: true,
clientLogLevel: 'none',
contentBase: publicDir,
// By default files from `contentBase` will not trigger a page reload.
watchContentBase: true,
// useLocalIp: true,
watchOptions: {
ignored: ignoredFiles(srcDir),
},
// Use 'ws' instead of 'sockjs-node' on server since we're using native
// websockets in `webpackHotDevClient`.
transportMode: 'ws',
// Prevent a WS client from getting injected as we're already including
// `webpackHotDevClient`.
injectClient: false,
historyApiFallback: true,
stats: {
colors: true,
chunks: false,
chunkModules: false,
modules: false,
entrypoints: false,
children: false,
version: false,
assets: false,
},
before(app, server) {
// This lets us fetch source contents from webpack for the error overlay
app.use(evalSourceMapMiddleware(server))
// This lets us open files from the runtime error overlay.
app.use(errorOverlayMiddleware())
},
after(app) {
// This service worker file is effectively a 'no-op' that will reset any
// previous service worker registered for the same host:port combination.
// We do this in development to avoid hitting the production cache if
// it used the same host and port.
// https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
app.use(noopServiceWorkerMiddleware(publicPath))
},
}
if (fs.existsSync(serverConfigPath)) {
const appConfig = require(serverConfigPath)
if (typeof appConfig === 'function') {
return appConfig(baseConfig)
} else if (typeof appConfig === 'object') {
return {
...baseConfig,
...appConfig,
}
}
} else {
const pkg = readJSONFileSync(pkgPath)
if (pkg.server) {
return {
...baseConfig,
...pkg.server,
}
}
}
return baseConfig
}
module.exports = (host, port) => {
if (cache) {
return cache
}
const config = getConfig()
cache = config
return cache
}