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'
|
2021-09-13 15:49:29 +02:00
|
|
|
import { flushAllTraces } from '../../trace'
|
2021-11-02 22:03:29 +01:00
|
|
|
import { getUnresolvedModuleFromError } from '../utils'
|
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 =
|
2021-01-25 16:13:12 +01:00
|
|
|
| { bootstrap: true; appUrl: string | null; bindAddr: string | null }
|
|
|
|
| ({ bootstrap: false; appUrl: string | null; bindAddr: string | null } & (
|
2021-10-09 11:51:37 +02:00
|
|
|
| {
|
|
|
|
loading: true
|
|
|
|
trigger: string | undefined
|
|
|
|
}
|
2019-02-16 17:09:49 +01:00
|
|
|
| {
|
|
|
|
loading: false
|
2019-06-26 20:54:23 +02:00
|
|
|
typeChecking: boolean
|
2021-11-16 16:57:30 +01:00
|
|
|
partial: 'client and server' | undefined
|
2021-10-09 11:51:37 +02:00
|
|
|
modules: number
|
2019-02-16 17:09:49 +01:00
|
|
|
errors: string[] | null
|
|
|
|
warnings: string[] | null
|
2022-02-08 14:16:46 +01:00
|
|
|
hasEdgeServer: boolean
|
2019-05-29 13:57:26 +02:00
|
|
|
}
|
|
|
|
))
|
2019-02-16 17:09:49 +01:00
|
|
|
|
2021-01-25 16:13:12 +01:00
|
|
|
export const store = createStore<OutputState>({
|
|
|
|
appUrl: null,
|
|
|
|
bindAddr: null,
|
|
|
|
bootstrap: true,
|
|
|
|
})
|
2019-02-16 17:09:49 +01:00
|
|
|
|
2021-01-25 16:13:12 +01:00
|
|
|
let lastStore: OutputState = { appUrl: null, bindAddr: null, bootstrap: true }
|
2019-04-09 17:52:03 +02:00
|
|
|
function hasStoreChanged(nextStore: OutputState) {
|
|
|
|
if (
|
2021-08-17 09:18:08 +02:00
|
|
|
(
|
|
|
|
[
|
|
|
|
...new Set([...Object.keys(lastStore), ...Object.keys(nextStore)]),
|
|
|
|
] 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
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:52:26 +02:00
|
|
|
let startTime = 0
|
|
|
|
|
2020-05-18 21:24:37 +02: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-05-06 15:22:54 +02:00
|
|
|
if (state.appUrl) {
|
2021-01-25 16:13:12 +01:00
|
|
|
Log.ready(`started server on ${state.bindAddr}, url: ${state.appUrl}`)
|
2019-05-06 15:22:54 +02:00
|
|
|
}
|
2019-02-16 17:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.loading) {
|
2021-10-09 11:51:37 +02:00
|
|
|
if (state.trigger) {
|
2021-11-23 18:47:36 +01:00
|
|
|
if (state.trigger !== 'initial') {
|
|
|
|
Log.wait(`compiling ${state.trigger}...`)
|
|
|
|
}
|
2021-10-09 11:51:37 +02:00
|
|
|
} else {
|
|
|
|
Log.wait('compiling...')
|
|
|
|
}
|
2021-11-23 18:47:36 +01:00
|
|
|
if (startTime === 0) {
|
|
|
|
startTime = Date.now()
|
|
|
|
}
|
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) {
|
2022-03-24 22:49:38 +01:00
|
|
|
const prop = (match.split(']').shift() || '').slice(1)
|
2019-04-09 17:52:03 +02:00
|
|
|
console.log(
|
2021-03-29 10:25:00 +02:00
|
|
|
`AMP bind syntax [${prop}]='' is not supported in JSX, use 'data-amp-bind-${prop}' instead. https://nextjs.org/docs/messages/amp-bind-jsx-alt`
|
2019-04-09 17:52:03 +02:00
|
|
|
)
|
2019-04-03 00:32:07 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 17:52:03 +02:00
|
|
|
|
2021-11-02 22:03:29 +01:00
|
|
|
const moduleName = getUnresolvedModuleFromError(cleanError)
|
2022-02-08 14:16:46 +01:00
|
|
|
if (state.hasEdgeServer && moduleName) {
|
2021-11-01 12:40:21 +01:00
|
|
|
console.error(
|
2022-02-08 14:16:46 +01:00
|
|
|
`Native Node.js APIs are not supported in the Edge Runtime. Found \`${moduleName}\` imported.\n`
|
2021-11-01 12:40:21 +01:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-16 19:43:28 +02:00
|
|
|
// Ensure traces are flushed after each compile in development mode
|
|
|
|
flushAllTraces()
|
2019-02-16 17:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:52:26 +02:00
|
|
|
let timeMessage = ''
|
|
|
|
if (startTime) {
|
|
|
|
const time = Date.now() - startTime
|
|
|
|
startTime = 0
|
|
|
|
|
|
|
|
timeMessage =
|
2021-10-11 10:14:52 +02:00
|
|
|
time > 2000 ? ` in ${Math.round(time / 100) / 10}s` : ` in ${time} ms`
|
2021-09-30 15:52:26 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 11:51:37 +02:00
|
|
|
let modulesMessage = ''
|
|
|
|
if (state.modules) {
|
|
|
|
modulesMessage = ` (${state.modules} modules)`
|
|
|
|
}
|
|
|
|
|
2021-10-11 10:14:52 +02:00
|
|
|
let partialMessage = ''
|
|
|
|
if (state.partial) {
|
|
|
|
partialMessage = ` ${state.partial}`
|
|
|
|
}
|
|
|
|
|
2019-02-16 17:09:49 +01:00
|
|
|
if (state.warnings) {
|
2019-04-09 17:52:03 +02:00
|
|
|
Log.warn(state.warnings.join('\n\n'))
|
2021-09-16 19:43:28 +02:00
|
|
|
// Ensure traces are flushed after each compile in development mode
|
|
|
|
flushAllTraces()
|
2019-02-16 17:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:54:23 +02:00
|
|
|
if (state.typeChecking) {
|
2021-09-30 15:52:26 +02:00
|
|
|
Log.info(
|
2021-10-11 10:14:52 +02:00
|
|
|
`bundled${partialMessage} successfully${timeMessage}${modulesMessage}, waiting for typecheck results...`
|
2021-09-30 15:52:26 +02:00
|
|
|
)
|
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
|
|
|
|
2021-10-11 10:14:52 +02:00
|
|
|
Log.event(
|
|
|
|
`compiled${partialMessage} successfully${timeMessage}${modulesMessage}`
|
|
|
|
)
|
2021-09-13 15:49:29 +02:00
|
|
|
// Ensure traces are flushed after each compile in development mode
|
|
|
|
flushAllTraces()
|
2019-02-16 17:09:49 +01:00
|
|
|
})
|