2019-02-27 15:12:40 +01:00
|
|
|
import createStore from 'next/dist/compiled/unistore'
|
2019-04-03 00:32:07 +02:00
|
|
|
import stripAnsi from 'strip-ansi'
|
2019-02-16 17:09:49 +01:00
|
|
|
|
2019-04-09 17:52:03 +02:00
|
|
|
import * as Log from './log'
|
|
|
|
|
2019-02-16 17:09:49 +01:00
|
|
|
export type OutputState =
|
|
|
|
| { bootstrap: true; appUrl: string | null }
|
|
|
|
| ({ bootstrap: false; appUrl: string | null } & (
|
|
|
|
| { loading: true }
|
|
|
|
| {
|
|
|
|
loading: false
|
2019-06-26 20:54:23 +02:00
|
|
|
typeChecking: boolean
|
2019-02-16 17:09:49 +01:00
|
|
|
errors: string[] | null
|
|
|
|
warnings: string[] | null
|
2019-05-29 13:57:26 +02:00
|
|
|
}
|
|
|
|
))
|
2019-02-16 17:09:49 +01:00
|
|
|
|
|
|
|
export const store = createStore<OutputState>({ appUrl: null, bootstrap: true })
|
|
|
|
|
2019-04-09 17:52:03 +02:00
|
|
|
let lastStore: OutputState = {} as any
|
|
|
|
function hasStoreChanged(nextStore: OutputState) {
|
|
|
|
if (
|
|
|
|
[...new Set([...Object.keys(lastStore), ...Object.keys(nextStore)])].every(
|
|
|
|
key => Object.is((lastStore as any)[key], (nextStore as any)[key])
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
lastStore = nextStore
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-02-16 17:09:49 +01:00
|
|
|
store.subscribe(state => {
|
2019-04-09 17:52:03 +02:00
|
|
|
if (!hasStoreChanged(state)) {
|
|
|
|
return
|
2019-03-19 23:16:02 +01:00
|
|
|
}
|
2019-02-16 17:09:49 +01:00
|
|
|
|
|
|
|
if (state.bootstrap) {
|
2019-04-09 17:52:03 +02:00
|
|
|
Log.wait('starting the development server ...')
|
2019-05-06 15:22:54 +02:00
|
|
|
if (state.appUrl) {
|
|
|
|
Log.info(`waiting on ${state.appUrl} ...`)
|
|
|
|
}
|
2019-02-16 17:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.loading) {
|
2019-04-09 17:52:03 +02:00
|
|
|
Log.wait('compiling ...')
|
2019-02-16 17:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 17:52:03 +02:00
|
|
|
|
2019-02-16 17:09:49 +01:00
|
|
|
if (state.errors) {
|
2019-04-09 17:52:03 +02:00
|
|
|
Log.error(state.errors[0])
|
|
|
|
|
2019-04-03 00:32:07 +02:00
|
|
|
const cleanError = stripAnsi(state.errors[0])
|
|
|
|
if (cleanError.indexOf('SyntaxError') > -1) {
|
|
|
|
const matches = cleanError.match(/\[.*\]=/)
|
|
|
|
if (matches) {
|
|
|
|
for (const match of matches) {
|
|
|
|
const prop = (match.split(']').shift() || '').substr(1)
|
2019-04-09 17:52:03 +02:00
|
|
|
console.log(
|
|
|
|
`AMP bind syntax [${prop}]='' is not supported in JSX, use 'data-amp-bind-${prop}' instead. https://err.sh/zeit/next.js/amp-bind-jsx-alt`
|
|
|
|
)
|
2019-04-03 00:32:07 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 17:52:03 +02:00
|
|
|
|
2019-02-16 17:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.warnings) {
|
2019-04-09 17:52:03 +02:00
|
|
|
Log.warn(state.warnings.join('\n\n'))
|
2019-05-06 15:22:54 +02:00
|
|
|
if (state.appUrl) {
|
|
|
|
Log.info(`ready on ${state.appUrl}`)
|
|
|
|
}
|
2019-02-16 17:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:54:23 +02:00
|
|
|
if (state.typeChecking) {
|
|
|
|
Log.info('bundled successfully, waiting for typecheck results ...')
|
|
|
|
return
|
2019-02-16 17:09:49 +01:00
|
|
|
}
|
2019-06-26 20:54:23 +02:00
|
|
|
|
|
|
|
Log.ready(
|
|
|
|
'compiled successfully' +
|
2019-07-10 02:34:20 +02:00
|
|
|
(state.appUrl ? ` - ready on ${state.appUrl}` : '')
|
2019-06-26 20:54:23 +02:00
|
|
|
)
|
2019-02-16 17:09:49 +01:00
|
|
|
})
|