Commit e76529a4 authored by Lukáš Huvar's avatar Lukáš Huvar Committed by Tim Neutkens

Bundle only server code for API routes (#7443)

parent eee19788
...@@ -52,6 +52,9 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, { ...@@ -52,6 +52,9 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
invalidator.startBuilding() invalidator.startBuilding()
const allEntries = Object.keys(entries).map(async (page) => { const allEntries = Object.keys(entries).map(async (page) => {
if (compiler.name === 'client' && page.startsWith('/api')) {
return
}
const { name, absolutePagePath } = entries[page] const { name, absolutePagePath } = entries[page]
const pageExists = await isWriteable(absolutePagePath) const pageExists = await isWriteable(absolutePagePath)
if (!pageExists) { if (!pageExists) {
...@@ -69,11 +72,8 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, { ...@@ -69,11 +72,8 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
}) })
} }
multiCompiler.hooks.done.tap('NextJsOnDemandEntries', (multiStats) => { function findHardFailedPages (errors) {
const clientStats = multiStats.stats[0] return errors.filter(e => {
const { compilation } = clientStats
const hardFailedPages = compilation.errors
.filter(e => {
// Make sure to only pick errors which marked with missing modules // Make sure to only pick errors which marked with missing modules
const hasNoModuleFoundError = /ENOENT/.test(e.message) || /Module not found/.test(e.message) const hasNoModuleFoundError = /ENOENT/.test(e.message) || /Module not found/.test(e.message)
if (!hasNoModuleFoundError) return false if (!hasNoModuleFoundError) return false
...@@ -91,9 +91,11 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, { ...@@ -91,9 +91,11 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
const pageName = ROUTE_NAME_REGEX.exec(c.name)[1] const pageName = ROUTE_NAME_REGEX.exec(c.name)[1]
return normalizePage(`/${pageName}`) return normalizePage(`/${pageName}`)
}) })
}
// compilation.entrypoints is a Map object, so iterating over it 0 is the key and 1 is the value function getPagePathsFromEntrypoints (entrypoints) {
for (const [, entrypoint] of compilation.entrypoints.entries()) { const pagePaths = []
for (const [, entrypoint] of entrypoints.entries()) {
const result = ROUTE_NAME_REGEX.exec(entrypoint.name) const result = ROUTE_NAME_REGEX.exec(entrypoint.name)
if (!result) { if (!result) {
continue continue
...@@ -105,6 +107,19 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, { ...@@ -105,6 +107,19 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
continue continue
} }
pagePaths.push(pagePath)
}
return pagePaths
}
multiCompiler.hooks.done.tap('NextJsOnDemandEntries', (multiStats) => {
const [clientStats, serverStats] = multiStats.stats
const hardFailedPages = [...new Set([...findHardFailedPages(clientStats.compilation.errors), ...findHardFailedPages(serverStats.compilation.errors)])]
const pagePaths = new Set([...getPagePathsFromEntrypoints(clientStats.compilation.entrypoints), ...(getPagePathsFromEntrypoints(serverStats.compilation.entrypoints))])
// compilation.entrypoints is a Map object, so iterating over it 0 is the key and 1 is the value
for (const pagePath of pagePaths) {
const page = normalizePage('/' + pagePath) const page = normalizePage('/' + pagePath)
const entry = entries[page] const entry = entries[page]
......
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