File size: 5,064 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
import createStore from 'next/dist/compiled/unistore'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { type Span, flushAllTraces, trace } from '../../trace'
import { teardownTraceSubscriber } from '../swc'
import * as Log from './log'
const MAX_LOG_SKIP_DURATION = 500 // 500ms
export type OutputState =
| {
bootstrap: true
appUrl: string | null
bindAddr: string | null
logging: boolean
}
| ({
bootstrap: false
appUrl: string | null
bindAddr: string | null
logging: boolean
} & (
| {
loading: true
trigger: string | undefined
url: string | undefined
}
| {
loading: false
typeChecking: boolean
totalModulesCount: number
errors: string[] | null
warnings: string[] | null
hasEdgeServer: boolean
}
))
export function formatTrigger(trigger: string) {
// Format dynamic sitemap routes to simpler file path
// e.g., /sitemap.xml[] -> /sitemap.xml
if (trigger.includes('[__metadata_id__]')) {
trigger = trigger.replace('/[__metadata_id__]', '/[id]')
}
if (trigger.length > 1 && trigger.endsWith('/')) {
trigger = trigger.slice(0, -1)
}
return trigger
}
export const store = createStore<OutputState>({
appUrl: null,
bindAddr: null,
bootstrap: true,
logging: true,
})
let lastStore: OutputState = {
appUrl: null,
bindAddr: null,
bootstrap: true,
logging: true,
}
function hasStoreChanged(nextStore: OutputState) {
if (
(
[
...new Set([...Object.keys(lastStore), ...Object.keys(nextStore)]),
] as Array<keyof OutputState>
).every((key) => Object.is(lastStore[key], nextStore[key]))
) {
return false
}
lastStore = nextStore
return true
}
let startTime = 0
let trigger = '' // default, use empty string for trigger
let triggerUrl: string | undefined = undefined
let loadingLogTimer: NodeJS.Timeout | null = null
let traceSpan: Span | null = null
let logging = true
store.subscribe((state) => {
// Update persisted logging state
if ('logging' in state) {
logging = state.logging
}
// If logging is disabled, do not log
if (!logging) {
return
}
if (!hasStoreChanged(state)) {
return
}
if (state.bootstrap) {
return
}
if (state.loading) {
if (state.trigger) {
trigger = formatTrigger(state.trigger)
triggerUrl = state.url
if (trigger !== 'initial') {
traceSpan = trace('compile-path', undefined, {
trigger: trigger,
})
if (!loadingLogTimer) {
// Only log compiling if compiled is not finished in 3 seconds
loadingLogTimer = setTimeout(() => {
if (
triggerUrl &&
triggerUrl !== trigger &&
process.env.NEXT_TRIGGER_URL
) {
Log.wait(`Compiling ${trigger} (${triggerUrl}) ...`)
} else {
Log.wait(`Compiling ${trigger} ...`)
}
}, MAX_LOG_SKIP_DURATION)
}
}
}
if (startTime === 0) {
startTime = Date.now()
}
return
}
if (state.errors) {
// Log compilation errors
Log.error(state.errors[0])
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() || '').slice(1)
console.log(
`AMP bind syntax [${prop}]='' is not supported in JSX, use 'data-amp-bind-${prop}' instead. https://nextjs.org/docs/messages/amp-bind-jsx-alt`
)
}
return
}
}
startTime = 0
// Ensure traces are flushed after each compile in development mode
flushAllTraces()
teardownTraceSubscriber()
return
}
let timeMessage = ''
if (startTime) {
const time = Date.now() - startTime
startTime = 0
timeMessage =
' ' +
(time > 2000 ? `in ${Math.round(time / 100) / 10}s` : `in ${time}ms`)
}
let modulesMessage = ''
if (state.totalModulesCount) {
modulesMessage = ` (${state.totalModulesCount} modules)`
}
if (state.warnings) {
Log.warn(state.warnings.join('\n\n'))
// Ensure traces are flushed after each compile in development mode
flushAllTraces()
teardownTraceSubscriber()
return
}
if (state.typeChecking) {
Log.info(
`bundled ${trigger}${timeMessage}${modulesMessage}, type checking...`
)
return
}
if (trigger === 'initial') {
trigger = ''
} else {
if (loadingLogTimer) {
clearTimeout(loadingLogTimer)
loadingLogTimer = null
}
if (traceSpan) {
traceSpan.stop()
traceSpan = null
}
Log.event(
`Compiled${trigger ? ' ' + trigger : ''}${timeMessage}${modulesMessage}`
)
trigger = ''
}
// Ensure traces are flushed after each compile in development mode
flushAllTraces()
teardownTraceSubscriber()
})
|