Unverified Commit c2ba7e6a authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: fix over-invalidation of node.js assets (#57240)

### What?

* no need to clear require cache when assets where not used previously
* make build status reporting more consistent
* report build status to client side for build indicator

### Why?

### How?


Closes WEB-1826
parent 6adb9716
...@@ -95,6 +95,7 @@ export default function initializeBuildWatcher( ...@@ -95,6 +95,7 @@ export default function initializeBuildWatcher(
break break
case HMR_ACTIONS_SENT_TO_BROWSER.BUILT: case HMR_ACTIONS_SENT_TO_BROWSER.BUILT:
case HMR_ACTIONS_SENT_TO_BROWSER.SYNC: case HMR_ACTIONS_SENT_TO_BROWSER.SYNC:
case HMR_ACTIONS_SENT_TO_BROWSER.FINISH_BUILDING:
hide() hide()
break break
} }
......
...@@ -17,6 +17,7 @@ export const enum HMR_ACTIONS_SENT_TO_BROWSER { ...@@ -17,6 +17,7 @@ export const enum HMR_ACTIONS_SENT_TO_BROWSER {
SYNC = 'sync', SYNC = 'sync',
BUILT = 'built', BUILT = 'built',
BUILDING = 'building', BUILDING = 'building',
FINISH_BUILDING = 'finishBuilding',
DEV_PAGES_MANIFEST_UPDATE = 'devPagesManifestUpdate', DEV_PAGES_MANIFEST_UPDATE = 'devPagesManifestUpdate',
TURBOPACK_MESSAGE = 'turbopack-message', TURBOPACK_MESSAGE = 'turbopack-message',
SERVER_ERROR = 'serverError', SERVER_ERROR = 'serverError',
...@@ -37,6 +38,10 @@ interface BuildingAction { ...@@ -37,6 +38,10 @@ interface BuildingAction {
action: HMR_ACTIONS_SENT_TO_BROWSER.BUILDING action: HMR_ACTIONS_SENT_TO_BROWSER.BUILDING
} }
interface FinishBuildingAction {
action: HMR_ACTIONS_SENT_TO_BROWSER.FINISH_BUILDING
}
interface SyncAction { interface SyncAction {
action: HMR_ACTIONS_SENT_TO_BROWSER.SYNC action: HMR_ACTIONS_SENT_TO_BROWSER.SYNC
hash: string hash: string
...@@ -95,6 +100,7 @@ export type HMR_ACTION_TYPES = ...@@ -95,6 +100,7 @@ export type HMR_ACTION_TYPES =
| TurbopackMessageAction | TurbopackMessageAction
| TurbopackConnectedAction | TurbopackConnectedAction
| BuildingAction | BuildingAction
| FinishBuildingAction
| SyncAction | SyncAction
| BuiltAction | BuiltAction
| AddedPageAction | AddedPageAction
......
...@@ -373,10 +373,22 @@ async function startWatcher(opts: SetupOpts) { ...@@ -373,10 +373,22 @@ async function startWatcher(opts: SetupOpts) {
// We ignore source maps // We ignore source maps
if (p.endsWith('.map')) continue if (p.endsWith('.map')) continue
let key = `${id}:${p}` let key = `${id}:${p}`
const previousHash = serverPathState.get(key) const localHash = serverPathState.get(key)
if (previousHash !== contentHash) { const globaHash = serverPathState.get(p)
if (
(localHash && localHash !== contentHash) ||
(globaHash && globaHash !== contentHash)
) {
hasChange = true hasChange = true
serverPathState.set(key, contentHash) serverPathState.set(key, contentHash)
serverPathState.set(p, contentHash)
} else {
if (!localHash) {
serverPathState.set(key, contentHash)
}
if (!globaHash) {
serverPathState.set(p, contentHash)
}
} }
} }
...@@ -404,6 +416,45 @@ async function startWatcher(opts: SetupOpts) { ...@@ -404,6 +416,45 @@ async function startWatcher(opts: SetupOpts) {
return result return result
} }
const buildingIds = new Set()
const readyIds = new Set()
function startBuilding(id: string, forceRebuild: boolean = false) {
if (!forceRebuild && readyIds.has(id)) {
return () => {}
}
if (buildingIds.size === 0) {
consoleStore.setState(
{
loading: true,
trigger: id,
} as OutputState,
true
)
hotReloader.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.BUILDING,
})
}
buildingIds.add(id)
return function finishBuilding() {
if (buildingIds.size === 0) {
return
}
readyIds.add(id)
buildingIds.delete(id)
if (buildingIds.size === 0) {
hotReloader.send({
action: HMR_ACTIONS_SENT_TO_BROWSER.FINISH_BUILDING,
})
consoleStore.setState(
{
loading: false,
} as OutputState,
true
)
}
}
}
let hmrHash = 0 let hmrHash = 0
const sendHmrDebounce = debounce(() => { const sendHmrDebounce = debounce(() => {
interface HmrError { interface HmrError {
...@@ -581,8 +632,6 @@ async function startWatcher(opts: SetupOpts) { ...@@ -581,8 +632,6 @@ async function startWatcher(opts: SetupOpts) {
) )
} }
const buildingReported = new Set<string>()
async function changeSubscription( async function changeSubscription(
page: string, page: string,
endpoint: Endpoint | undefined, endpoint: Endpoint | undefined,
...@@ -598,14 +647,6 @@ async function startWatcher(opts: SetupOpts) { ...@@ -598,14 +647,6 @@ async function startWatcher(opts: SetupOpts) {
const changed = await changedPromise const changed = await changedPromise
for await (const change of changed) { for await (const change of changed) {
consoleStore.setState(
{
loading: true,
trigger: page,
} as OutputState,
true
)
processIssues(page, page, change) processIssues(page, page, change)
const payload = await makePayload(page, change) const payload = await makePayload(page, change)
if (payload) sendHmr('endpoint-change', page, payload) if (payload) sendHmr('endpoint-change', page, payload)
...@@ -1029,6 +1070,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1029,6 +1070,7 @@ async function startWatcher(opts: SetupOpts) {
await processMiddleware() await processMiddleware()
changeSubscription('middleware', middleware.endpoint, async () => { changeSubscription('middleware', middleware.endpoint, async () => {
const finishBuilding = startBuilding('middleware', true)
await processMiddleware() await processMiddleware()
await propagateServerField( await propagateServerField(
'actualMiddlewareFile', 'actualMiddlewareFile',
...@@ -1037,7 +1079,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1037,7 +1079,7 @@ async function startWatcher(opts: SetupOpts) {
await propagateServerField('middleware', serverFields.middleware) await propagateServerField('middleware', serverFields.middleware)
await writeMiddlewareManifest() await writeMiddlewareManifest()
console.log('middleware changes') finishBuilding()
return { event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES } return { event: HMR_ACTIONS_SENT_TO_BROWSER.MIDDLEWARE_CHANGES }
}) })
prevMiddleware = true prevMiddleware = true
...@@ -1223,6 +1265,8 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1223,6 +1265,8 @@ async function startWatcher(opts: SetupOpts) {
let page = definition?.pathname ?? inputPage let page = definition?.pathname ?? inputPage
if (page === '/_error') { if (page === '/_error') {
let finishBuilding = startBuilding(page)
try {
if (globalEntries.app) { if (globalEntries.app) {
const writtenEndpoint = await processResult( const writtenEndpoint = await processResult(
'_app', '_app',
...@@ -1260,10 +1304,11 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1260,10 +1304,11 @@ async function startWatcher(opts: SetupOpts) {
await writePagesManifest() await writePagesManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeLoadableManifest() await writeLoadableManifest()
} finally {
finishBuilding()
}
return return
} }
await currentEntriesHandling await currentEntriesHandling
const route = const route =
curEntries.get(page) ?? curEntries.get(page) ??
...@@ -1282,8 +1327,6 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1282,8 +1327,6 @@ async function startWatcher(opts: SetupOpts) {
throw new PageNotFoundError(`route not found ${page}`) throw new PageNotFoundError(`route not found ${page}`)
} }
if (!buildingReported.has(page)) {
buildingReported.add(page)
let suffix let suffix
switch (route.type) { switch (route.type) {
case 'app-page': case 'app-page':
...@@ -1300,17 +1343,12 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1300,17 +1343,12 @@ async function startWatcher(opts: SetupOpts) {
throw new Error('Unexpected route type ' + route.type) throw new Error('Unexpected route type ' + route.type)
} }
consoleStore.setState( const buildingKey = `${page}${
{
loading: true,
trigger: `${page}${
!page.endsWith('/') && suffix.length > 0 ? '/' : '' !page.endsWith('/') && suffix.length > 0 ? '/' : ''
}${suffix}`, }${suffix}`
} as OutputState, let finishBuilding: (() => void) | undefined = undefined
true
)
}
try {
switch (route.type) { switch (route.type) {
case 'page': { case 'page': {
if (isApp) { if (isApp) {
...@@ -1319,6 +1357,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1319,6 +1357,7 @@ async function startWatcher(opts: SetupOpts) {
) )
} }
finishBuilding = startBuilding(buildingKey)
if (globalEntries.app) { if (globalEntries.app) {
const writtenEndpoint = await processResult( const writtenEndpoint = await processResult(
'_app', '_app',
...@@ -1347,7 +1386,10 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1347,7 +1386,10 @@ async function startWatcher(opts: SetupOpts) {
await route.htmlEndpoint.writeToDisk() await route.htmlEndpoint.writeToDisk()
) )
changeSubscription(page, route.dataEndpoint, (pageName, change) => { changeSubscription(
page,
route.dataEndpoint,
(pageName, change) => {
switch (change.type) { switch (change.type) {
case ServerClientChangeType.Server: case ServerClientChangeType.Server:
case ServerClientChangeType.Both: case ServerClientChangeType.Both:
...@@ -1357,7 +1399,8 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1357,7 +1399,8 @@ async function startWatcher(opts: SetupOpts) {
} }
default: default:
} }
}) }
)
const type = writtenEndpoint?.type const type = writtenEndpoint?.type
...@@ -1385,6 +1428,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1385,6 +1428,7 @@ async function startWatcher(opts: SetupOpts) {
// since this can happen when app pages make // since this can happen when app pages make
// api requests to page API routes. // api requests to page API routes.
finishBuilding = startBuilding(buildingKey)
const writtenEndpoint = await processResult( const writtenEndpoint = await processResult(
page, page,
await route.endpoint.writeToDisk() await route.endpoint.writeToDisk()
...@@ -1409,6 +1453,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1409,6 +1453,7 @@ async function startWatcher(opts: SetupOpts) {
break break
} }
case 'app-page': { case 'app-page': {
finishBuilding = startBuilding(buildingKey)
const writtenEndpoint = await processResult( const writtenEndpoint = await processResult(
page, page,
await route.htmlEndpoint.writeToDisk() await route.htmlEndpoint.writeToDisk()
...@@ -1443,6 +1488,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1443,6 +1488,7 @@ async function startWatcher(opts: SetupOpts) {
break break
} }
case 'app-route': { case 'app-route': {
finishBuilding = startBuilding(buildingKey)
const writtenEndpoint = await processResult( const writtenEndpoint = await processResult(
page, page,
await route.endpoint.writeToDisk() await route.endpoint.writeToDisk()
...@@ -1468,16 +1514,14 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1468,16 +1514,14 @@ async function startWatcher(opts: SetupOpts) {
break break
} }
default: { default: {
throw new Error(`unknown route type ${route.type} for ${page}`) throw new Error(
`unknown route type ${(route as any).type} for ${page}`
)
} }
} }
} finally {
consoleStore.setState( if (finishBuilding) finishBuilding()
{ }
loading: false,
} as OutputState,
true
)
}, },
} }
......
...@@ -20,7 +20,7 @@ const installCheckVisible = (browser) => { ...@@ -20,7 +20,7 @@ const installCheckVisible = (browser) => {
watcherDiv.querySelector('div').className.indexOf('visible') > -1 watcherDiv.querySelector('div').className.indexOf('visible') > -1
) )
if (window.showedBuilder) clearInterval(window.checkInterval) if (window.showedBuilder) clearInterval(window.checkInterval)
}, 50) }, 5)
})()`) })()`)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment