2016-12-19 21:04:38 +00:00
|
|
|
export function warn (message) {
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
console.error(message)
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 18:43:31 +00:00
|
|
|
|
2017-01-16 21:02:11 +00:00
|
|
|
export function execOnce (fn) {
|
|
|
|
let used = false
|
|
|
|
return (...args) => {
|
|
|
|
if (!used) {
|
|
|
|
used = true
|
|
|
|
fn.apply(this, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 18:43:31 +00:00
|
|
|
export function deprecated (fn, message) {
|
|
|
|
if (process.env.NODE_ENV === 'production') return fn
|
|
|
|
|
|
|
|
let warned = false
|
|
|
|
const newFn = function (...args) {
|
|
|
|
if (!warned) {
|
|
|
|
warned = true
|
|
|
|
console.error(message)
|
|
|
|
}
|
|
|
|
return fn.apply(this, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy all properties
|
|
|
|
Object.assign(newFn, fn)
|
|
|
|
|
|
|
|
return newFn
|
|
|
|
}
|
2017-01-15 12:15:06 +00:00
|
|
|
|
|
|
|
export function printAndExit (message, code = 1) {
|
|
|
|
if (code === 0) {
|
|
|
|
console.log(message)
|
|
|
|
} else {
|
|
|
|
console.error(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
process.exit(code)
|
|
|
|
}
|
2017-01-20 19:33:46 +00:00
|
|
|
|
2017-06-19 13:03:02 +00:00
|
|
|
export function getDisplayName (Component) {
|
|
|
|
return Component.displayName || Component.name || 'Unknown'
|
|
|
|
}
|
|
|
|
|
2018-01-31 13:12:21 +00:00
|
|
|
export function isResSent (res) {
|
|
|
|
return res.finished || res.headersSent
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:33:46 +00:00
|
|
|
export async function loadGetInitialProps (Component, ctx) {
|
|
|
|
if (!Component.getInitialProps) return {}
|
|
|
|
|
|
|
|
const props = await Component.getInitialProps(ctx)
|
2018-01-31 13:12:21 +00:00
|
|
|
|
|
|
|
if (ctx.res && isResSent(ctx.res)) {
|
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!props) {
|
2017-06-19 13:03:02 +00:00
|
|
|
const compName = getDisplayName(Component)
|
2017-01-20 19:33:46 +00:00
|
|
|
const message = `"${compName}.getInitialProps()" should resolve to an object. But found "${props}" instead.`
|
|
|
|
throw new Error(message)
|
|
|
|
}
|
2018-01-31 13:12:21 +00:00
|
|
|
|
2017-01-20 19:33:46 +00:00
|
|
|
return props
|
|
|
|
}
|
2017-02-15 08:31:19 +00:00
|
|
|
|
|
|
|
export function getLocationOrigin () {
|
|
|
|
const { protocol, hostname, port } = window.location
|
|
|
|
return `${protocol}//${hostname}${port ? ':' + port : ''}`
|
|
|
|
}
|
2017-02-28 17:31:17 +00:00
|
|
|
|
|
|
|
export function getURL () {
|
|
|
|
const { href } = window.location
|
|
|
|
const origin = getLocationOrigin()
|
|
|
|
return href.substring(origin.length)
|
|
|
|
}
|