2019-02-27 15:12:40 +01:00
|
|
|
import createStore from 'next/dist/compiled/unistore'
|
2020-03-29 05:05:26 +02:00
|
|
|
import stripAnsi from 'next/dist/compiled/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 })
|
|
|
|
|
2020-04-06 17:59:36 +02:00
|
|
|
let lastStore: OutputState = { appUrl: null, bootstrap: true }
|
2019-04-09 17:52:03 +02:00
|
|
|
function hasStoreChanged(nextStore: OutputState) {
|
|
|
|
if (
|
2020-04-06 17:59:36 +02:00
|
|
|
([
|
2019-11-11 04:24:53 +01:00
|
|
|
...new Set([...Object.keys(lastStore), ...Object.keys(nextStore)]),
|
2020-04-06 17:59:36 +02:00
|
|
|
] as Array<keyof OutputState>).every(key =>
|
|
|
|
Object.is(lastStore[key], nextStore[key])
|
|
|
|
)
|
2019-04-09 17:52:03 +02:00
|
|
|
) {
|
|
|
|
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) {
|
2019-10-28 19:02:03 +01:00
|
|
|
Log.info('bundled successfully, waiting for typecheck results...')
|
2019-06-26 20:54:23 +02:00
|
|
|
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
|
|
|
})
|