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 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'
...@@ -7,7 +11,7 @@ import { generateBuildId } from './generate-build-id' ...@@ -7,7 +11,7 @@ import { generateBuildId } from './generate-build-id'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
import { isWriteable } from './is-writeable' import { isWriteable } from './is-writeable'
import { runCompiler, CompilerResult } from './compiler' import { runCompiler, CompilerResult } from './compiler'
import {recursiveReadDir} from '../lib/recursive-readdir' import { recursiveReadDir } from '../lib/recursive-readdir'
import { createPagesMapping, createEntrypoints } from './entries' import { createPagesMapping, createEntrypoints } from './entries'
import formatWebpackMessages from '../client/dev-error-overlay/format-webpack-messages' import formatWebpackMessages from '../client/dev-error-overlay/format-webpack-messages'
import chalk from 'chalk' import chalk from 'chalk'
...@@ -16,7 +20,10 @@ function collectPages( ...@@ -16,7 +20,10 @@ function collectPages(
directory: string, directory: string,
pageExtensions: string[] pageExtensions: string[]
): Promise<string[]> { ): Promise<string[]> {
return recursiveReadDir(directory, new RegExp(`\\.(?:${pageExtensions.join('|')})$`)) return recursiveReadDir(
directory,
new RegExp(`\\.(?:${pageExtensions.join('|')})$`)
)
} }
function printTreeView(list: string[]) { function printTreeView(list: string[]) {
...@@ -37,7 +44,11 @@ function printTreeView(list: string[]) { ...@@ -37,7 +44,11 @@ function printTreeView(list: string[]) {
console.log() 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))) { if (!(await isWriteable(dir))) {
throw new Error( throw new Error(
'> Build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable' '> 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> { ...@@ -49,12 +60,25 @@ export default async function build(dir: string, conf = null): Promise<void> {
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 = join(dir, config.distDir) const distDir = pathJoin(dir, config.distDir)
const pagesDir = join(dir, 'pages') const pagesDir = pathJoin(dir, 'pages')
const pagePaths = await collectPages(pagesDir, config.pageExtensions) let pagePaths
const pages = createPagesMapping(pagePaths, config.pageExtensions) if (pages && pages.length) {
const entrypoints = createEntrypoints(pages, config.target, buildId, config) // 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([ const configs = await Promise.all([
getBaseWebpackConfig(dir, { getBaseWebpackConfig(dir, {
buildId, buildId,
...@@ -112,7 +136,9 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -112,7 +136,9 @@ export default async function build(dir: string, conf = null): Promise<void> {
console.error() console.error()
if (error.indexOf('private-next-pages') > -1) { 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') throw new Error('> Build failed because of webpack errors')
} else if (result.warnings.length > 0) { } else if (result.warnings.length > 0) {
...@@ -123,7 +149,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -123,7 +149,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
console.log(chalk.green('Compiled successfully.\n')) console.log(chalk.green('Compiled successfully.\n'))
} }
printTreeView(Object.keys(pages)) printTreeView(Object.keys(mappedPages))
await writeBuildId(distDir, buildId) await writeBuildId(distDir, buildId)
} }
...@@ -151,6 +151,16 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe ...@@ -151,6 +151,16 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
default: false, default: false,
vendors: false vendors: false
} }
} : sharedRuntime ? {
cacheGroups: {
default: false,
vendors: false,
react: {
name: 'commons',
chunks: 'all',
test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/
}
}
} : { } : {
chunks: 'all', chunks: 'all',
cacheGroups: { cacheGroups: {
......
...@@ -7,15 +7,20 @@ import { printAndExit } from '../server/lib/utils' ...@@ -7,15 +7,20 @@ import { printAndExit } from '../server/lib/utils'
import { cliCommand } from '../bin/next' import { cliCommand } from '../bin/next'
const nextBuild: cliCommand = (argv) => { const nextBuild: cliCommand = (argv) => {
const args = arg({ const args = arg(
// Types {
'--help': Boolean, // Types
// Aliases '--help': Boolean,
'-h': '--help', '--experimental-page': [String],
}, { argv }) // Aliases
'-h': '--help',
},
{ argv },
)
if (args['--help']) { if (args['--help']) {
printAndExit(` printAndExit(
`
Description Description
Compiles the application for production deployment Compiles the application for production deployment
...@@ -25,7 +30,9 @@ const nextBuild: cliCommand = (argv) => { ...@@ -25,7 +30,9 @@ const nextBuild: cliCommand = (argv) => {
<dir> represents where the compiled dist folder should go. <dir> represents where the compiled dist folder should go.
If no directory is provided, the dist folder will be created in the current directory. 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' 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] || '.') const dir = resolve(args._[0] || '.')
...@@ -39,18 +46,21 @@ const nextBuild: cliCommand = (argv) => { ...@@ -39,18 +46,21 @@ const nextBuild: cliCommand = (argv) => {
if (!existsSync(join(dir, 'pages'))) { if (!existsSync(join(dir, 'pages'))) {
// Check one level down the tree to see if the pages directory might be there // Check one level down the tree to see if the pages directory might be there
if (existsSync(join(dir, '..', 'pages'))) { 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) build(dir, undefined, { pages: args['--experimental-page'] }).catch((err) => {
.catch((err) => { // tslint:disable-next-line
// tslint:disable-next-line console.error('> Build error occurred')
console.error('> Build error occurred') printAndExit(err)
printAndExit(err) })
})
} }
export { nextBuild } export { nextBuild }
...@@ -122,6 +122,7 @@ export default async function (task) { ...@@ -122,6 +122,7 @@ export default async function (task) {
await task.watch('export/**/*.+(js|ts|tsx)', 'nextexport') await task.watch('export/**/*.+(js|ts|tsx)', 'nextexport')
await task.watch('client/**/*.+(js|ts|tsx)', 'client') await task.watch('client/**/*.+(js|ts|tsx)', 'client')
await task.watch('lib/**/*.+(js|ts|tsx)', 'lib') await task.watch('lib/**/*.+(js|ts|tsx)', 'lib')
await task.watch('cli/**/*.+(js|ts|tsx)', 'cli')
} }
export async function release (task) { 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