Unverified Commit a63a4933 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Refine selective page building (#6866)

parent 7c57d2f5
...@@ -30,8 +30,7 @@ const defaultConfig = { ...@@ -30,8 +30,7 @@ const defaultConfig = {
), ),
ampBindInitData: false, ampBindInitData: false,
exportTrailingSlash: true, exportTrailingSlash: true,
profiling: false, profiling: false
sharedRuntime: false
} }
} }
......
import { import path from 'path'
join as pathJoin, import fs from 'fs'
relative as pathRelative,
isAbsolute as pathIsAbsolute,
} from 'path'
import nanoid from 'next/dist/compiled/nanoid/index.js' import nanoid from 'next/dist/compiled/nanoid/index.js'
import loadConfig from 'next-server/next-config' import loadConfig from 'next-server/next-config'
import { PHASE_PRODUCTION_BUILD } from 'next-server/constants' import { PHASE_PRODUCTION_BUILD } from 'next-server/constants'
...@@ -44,10 +41,21 @@ function printTreeView(list: string[]) { ...@@ -44,10 +41,21 @@ function printTreeView(list: string[]) {
console.log() console.log()
} }
function flatten<T>(arr: T[][]): T[] {
return arr.reduce((acc, val) => acc.concat(val), [] as T[])
}
function getPossibleFiles(pageExtensions: string[], pages: string[]) {
const res = pages.map(page =>
[page].concat(pageExtensions.map(e => `${page}.${e}`))
)
return flatten<string>(res)
}
export default async function build( export default async function build(
dir: string, dir: string,
conf = null, conf = null,
{ pages = [] as string[] } = {} { pages: _pages = [] as string[] } = {}
): Promise<void> { ): Promise<void> {
if (!(await isWriteable(dir))) { if (!(await isWriteable(dir))) {
throw new Error( throw new Error(
...@@ -60,14 +68,48 @@ export default async function build( ...@@ -60,14 +68,48 @@ export default async function build(
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf) const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const buildId = await generateBuildId(config.generateBuildId, nanoid) const buildId = await generateBuildId(config.generateBuildId, nanoid)
const distDir = pathJoin(dir, config.distDir) const distDir = path.join(dir, config.distDir)
const pagesDir = pathJoin(dir, 'pages') const pagesDir = path.join(dir, 'pages')
const pages =
Array.isArray(_pages) && _pages.length
? _pages
: process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE
? [process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE]
: []
let pagePaths let pagePaths
if (pages && pages.length) { if (pages && pages.length) {
// TODO: smart resolve these pages and check for them if (config.target !== 'serverless') {
pagePaths = pages.map(page => throw new Error(
'/'.concat(pathIsAbsolute(page) ? pathRelative(pagesDir, page) : page) 'Cannot use selective page building without the serverless target.'
)
}
const explodedPages = flatten<string>(pages.map(p => p.split(','))).map(
p => {
let resolvedPage: string | undefined
if (path.isAbsolute(p)) {
resolvedPage = getPossibleFiles(config.pageExtensions, [
path.join(pagesDir, p),
p,
]).find(f => fs.existsSync(f))
} else {
resolvedPage = getPossibleFiles(config.pageExtensions, [
path.join(pagesDir, p),
path.join(dir, p),
]).find(f => fs.existsSync(f))
}
return { original: p, resolved: resolvedPage || null }
}
)
const missingPage = explodedPages.find(({ resolved }) => !resolved)
if (missingPage) {
throw new Error(`Unable to identify page: ${missingPage.original}`)
}
pagePaths = explodedPages.map(
page => '/' + path.relative(pagesDir, page.resolved!)
) )
} else { } else {
pagePaths = await collectPages(pagesDir, config.pageExtensions) pagePaths = await collectPages(pagesDir, config.pageExtensions)
...@@ -86,6 +128,7 @@ export default async function build( ...@@ -86,6 +128,7 @@ export default async function build(
config, config,
target: config.target, target: config.target,
entrypoints: entrypoints.client, entrypoints: entrypoints.client,
__selectivePageBuilding: pages && Boolean(pages.length),
}), }),
getBaseWebpackConfig(dir, { getBaseWebpackConfig(dir, {
buildId, buildId,
...@@ -93,6 +136,7 @@ export default async function build( ...@@ -93,6 +136,7 @@ export default async function build(
config, config,
target: config.target, target: config.target,
entrypoints: entrypoints.server, entrypoints: entrypoints.server,
__selectivePageBuilding: pages && Boolean(pages.length),
}), }),
]) ])
......
...@@ -18,7 +18,7 @@ import { ChunkGraphPlugin } from './webpack/plugins/chunk-graph-plugin' ...@@ -18,7 +18,7 @@ import { ChunkGraphPlugin } from './webpack/plugins/chunk-graph-plugin'
import { WebpackEntrypoints } from './entries' import { WebpackEntrypoints } from './entries'
type ExcludesFalse = <T>(x: T | false) => x is T type ExcludesFalse = <T>(x: T | false) => x is T
export default function getBaseWebpackConfig (dir: string, {dev = false, isServer = false, buildId, config, target = 'server', entrypoints}: {dev?: boolean, isServer?: boolean, buildId: string, config: any, target?: string, entrypoints: WebpackEntrypoints}): webpack.Configuration { export default function getBaseWebpackConfig (dir: string, {dev = false, isServer = false, buildId, config, target = 'server', entrypoints, __selectivePageBuilding = false}: {dev?: boolean, isServer?: boolean, buildId: string, config: any, target?: string, entrypoints: WebpackEntrypoints, __selectivePageBuilding?: boolean}): webpack.Configuration {
const defaultLoaders = { const defaultLoaders = {
babel: { babel: {
loader: 'next-babel-loader', loader: 'next-babel-loader',
...@@ -75,8 +75,6 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe ...@@ -75,8 +75,6 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
cpus: config.experimental.cpus, cpus: config.experimental.cpus,
} }
const sharedRuntime = config.experimental.sharedRuntime && target === 'serverless'
let webpackConfig: webpack.Configuration = { let webpackConfig: webpack.Configuration = {
mode: webpackMode, mode: webpackMode,
devtool: dev ? 'cheap-module-source-map' : false, devtool: dev ? 'cheap-module-source-map' : false,
...@@ -145,15 +143,15 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe ...@@ -145,15 +143,15 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
}) })
] : undefined ] : undefined
} : { } : {
runtimeChunk: (!sharedRuntime || dev) ? { runtimeChunk: __selectivePageBuilding ? false : {
name: CLIENT_STATIC_FILES_RUNTIME_WEBPACK name: CLIENT_STATIC_FILES_RUNTIME_WEBPACK
} : false, },
splitChunks: dev ? { splitChunks: dev ? {
cacheGroups: { cacheGroups: {
default: false, default: false,
vendors: false vendors: false
} }
} : sharedRuntime ? { } : __selectivePageBuilding ? {
cacheGroups: { cacheGroups: {
default: false, default: false,
vendors: false, vendors: false,
...@@ -318,7 +316,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe ...@@ -318,7 +316,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
// bundle churn // bundle churn
!dev && new HashedChunkIdsPlugin(buildId), !dev && new HashedChunkIdsPlugin(buildId),
// On the client we want to share the same runtime cache // On the client we want to share the same runtime cache
!dev && !isServer && sharedRuntime && new SharedRuntimePlugin(), !isServer && __selectivePageBuilding && new SharedRuntimePlugin(),
!dev && new webpack.IgnorePlugin({ !dev && new webpack.IgnorePlugin({
checkResource: (resource: string) => { checkResource: (resource: string) => {
return /react-is/.test(resource) return /react-is/.test(resource)
......
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