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

Specified page builds (#6796)

* [wip] individual page builds

* Make flag experimental and remove from main bin

* Do not split chunks when using shared runtime
parent 02966afd
import { join } from 'path'
import {
join as pathJoin,
relative as pathRelative,
isAbsolute as pathIsAbsolute,
} from 'path'
import nanoid from 'next/dist/compiled/nanoid/index.js'
import loadConfig from 'next-server/next-config'
import { PHASE_PRODUCTION_BUILD } from 'next-server/constants'
......@@ -7,7 +11,7 @@ import { generateBuildId } from './generate-build-id'
import { writeBuildId } from './write-build-id'
import { isWriteable } from './is-writeable'
import { runCompiler, CompilerResult } from './compiler'
import {recursiveReadDir} from '../lib/recursive-readdir'
import { recursiveReadDir } from '../lib/recursive-readdir'
import { createPagesMapping, createEntrypoints } from './entries'
import formatWebpackMessages from '../client/dev-error-overlay/format-webpack-messages'
import chalk from 'chalk'
......@@ -16,7 +20,10 @@ function collectPages(
directory: string,
pageExtensions: string[]
): Promise<string[]> {
return recursiveReadDir(directory, new RegExp(`\\.(?:${pageExtensions.join('|')})$`))
return recursiveReadDir(
directory,
new RegExp(`\\.(?:${pageExtensions.join('|')})$`)
)
}
function printTreeView(list: string[]) {
......@@ -37,7 +44,11 @@ function printTreeView(list: string[]) {
console.log()
}
export default async function build(dir: string, conf = null): Promise<void> {
export default async function build(
dir: string,
conf = null,
{ pages = [] as string[] } = {}
): Promise<void> {
if (!(await isWriteable(dir))) {
throw new Error(
'> Build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable'
......@@ -49,12 +60,25 @@ export default async function build(dir: string, conf = null): Promise<void> {
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const buildId = await generateBuildId(config.generateBuildId, nanoid)
const distDir = join(dir, config.distDir)
const pagesDir = join(dir, 'pages')
const distDir = pathJoin(dir, config.distDir)
const pagesDir = pathJoin(dir, 'pages')
const pagePaths = await collectPages(pagesDir, config.pageExtensions)
const pages = createPagesMapping(pagePaths, config.pageExtensions)
const entrypoints = createEntrypoints(pages, config.target, buildId, config)
let pagePaths
if (pages && pages.length) {
// TODO: smart resolve these pages and check for them
pagePaths = pages.map(page =>
pathIsAbsolute(page) ? pathRelative(pagesDir, page) : page
)
} else {
pagePaths = await collectPages(pagesDir, config.pageExtensions)
}
const mappedPages = createPagesMapping(pagePaths, config.pageExtensions)
const entrypoints = createEntrypoints(
mappedPages,
config.target,
buildId,
config
)
const configs = await Promise.all([
getBaseWebpackConfig(dir, {
buildId,
......@@ -112,7 +136,9 @@ export default async function build(dir: string, conf = null): Promise<void> {
console.error()
if (error.indexOf('private-next-pages') > -1) {
throw new Error('> webpack config.resolve.alias was incorrectly overriden. https://err.sh/zeit/next.js/invalid-resolve-alias')
throw new Error(
'> webpack config.resolve.alias was incorrectly overriden. https://err.sh/zeit/next.js/invalid-resolve-alias'
)
}
throw new Error('> Build failed because of webpack errors')
} else if (result.warnings.length > 0) {
......@@ -123,7 +149,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
console.log(chalk.green('Compiled successfully.\n'))
}
printTreeView(Object.keys(pages))
printTreeView(Object.keys(mappedPages))
await writeBuildId(distDir, buildId)
}
......@@ -151,6 +151,16 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
default: false,
vendors: false
}
} : sharedRuntime ? {
cacheGroups: {
default: false,
vendors: false,
react: {
name: 'commons',
chunks: 'all',
test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/
}
}
} : {
chunks: 'all',
cacheGroups: {
......
......@@ -7,15 +7,20 @@ import { printAndExit } from '../server/lib/utils'
import { cliCommand } from '../bin/next'
const nextBuild: cliCommand = (argv) => {
const args = arg({
// Types
'--help': Boolean,
// Aliases
'-h': '--help',
}, { argv })
const args = arg(
{
// Types
'--help': Boolean,
'--experimental-page': [String],
// Aliases
'-h': '--help',
},
{ argv },
)
if (args['--help']) {
printAndExit(`
printAndExit(
`
Description
Compiles the application for production deployment
......@@ -25,7 +30,9 @@ const nextBuild: cliCommand = (argv) => {
<dir> represents where the compiled dist folder should go.
If no directory is provided, the dist folder will be created in the current directory.
You can set a custom folder in config https://github.com/zeit/next.js#custom-configuration, otherwise it will be created inside '.next'
`, 0)
`,
0,
)
}
const dir = resolve(args._[0] || '.')
......@@ -39,18 +46,21 @@ const nextBuild: cliCommand = (argv) => {
if (!existsSync(join(dir, 'pages'))) {
// Check one level down the tree to see if the pages directory might be there
if (existsSync(join(dir, '..', 'pages'))) {
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
printAndExit(
'> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?',
)
}
printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
printAndExit(
"> Couldn't find a `pages` directory. Please create one under the project root",
)
}
build(dir)
.catch((err) => {
// tslint:disable-next-line
console.error('> Build error occurred')
printAndExit(err)
})
build(dir, undefined, { pages: args['--experimental-page'] }).catch((err) => {
// tslint:disable-next-line
console.error('> Build error occurred')
printAndExit(err)
})
}
export { nextBuild }
......@@ -122,6 +122,7 @@ export default async function (task) {
await task.watch('export/**/*.+(js|ts|tsx)', 'nextexport')
await task.watch('client/**/*.+(js|ts|tsx)', 'client')
await task.watch('lib/**/*.+(js|ts|tsx)', 'lib')
await task.watch('cli/**/*.+(js|ts|tsx)', 'cli')
}
export async function release (task) {
......
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