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

Remove CLI and negative page building (#6986)

* Remove CLI and negative page building

* Remove select all tests

* Update test of getSpecifiedPages

* Prune old tests from snapshot

* Remove old flying shuttle tests
parent 1810cf5a
...@@ -56,29 +56,16 @@ function getPossibleFiles(pageExtensions: string[], pages: string[]) { ...@@ -56,29 +56,16 @@ function getPossibleFiles(pageExtensions: string[], pages: string[]) {
export async function getSpecifiedPages( export async function getSpecifiedPages(
dir: string, dir: string,
pages: string[], pagesString: string,
pageExtensions: string[], pageExtensions: string[]
assumeAll: boolean
) { ) {
const pagesDir = path.join(dir, 'pages') const pagesDir = path.join(dir, 'pages')
const reservedPages = ['/_app', '/_document', '/_error'] const reservedPages = ['/_app', '/_document', '/_error']
pages.push(...reservedPages)
const searchAllPages = assumeAll || pages.some(p => p.includes('**'))
let pagePaths: string[]
const explodedPages = [ const explodedPages = [
...new Set( ...new Set([...pagesString.split(','), ...reservedPages]),
flatten<string>(pages.map(p => p.split(',').filter(p => p !== '**')))
),
].map(p => { ].map(p => {
let removePage = false
if (p.startsWith('-')) {
p = p.substring(1)
removePage = true
}
let resolvedPage: string | undefined let resolvedPage: string | undefined
if (path.isAbsolute(p)) { if (path.isAbsolute(p)) {
resolvedPage = getPossibleFiles(pageExtensions, [ resolvedPage = getPossibleFiles(pageExtensions, [
...@@ -91,7 +78,7 @@ export async function getSpecifiedPages( ...@@ -91,7 +78,7 @@ export async function getSpecifiedPages(
path.join(dir, p), path.join(dir, p),
]).find(f => fs.existsSync(f) && fs.lstatSync(f).isFile()) ]).find(f => fs.existsSync(f) && fs.lstatSync(f).isFile())
} }
return { original: p, resolved: resolvedPage || null, removePage } return { original: p, resolved: resolvedPage || null }
}) })
const missingPage = explodedPages.find( const missingPage = explodedPages.find(
...@@ -100,33 +87,14 @@ export async function getSpecifiedPages( ...@@ -100,33 +87,14 @@ export async function getSpecifiedPages(
if (missingPage) { if (missingPage) {
throw new Error(`Unable to identify page: ${missingPage.original}`) throw new Error(`Unable to identify page: ${missingPage.original}`)
} }
const resolvedPagePaths = explodedPages const resolvedPagePaths = explodedPages
.filter(page => page.resolved) .filter(page => page.resolved)
.map(page => ({ .map(page => '/' + path.relative(pagesDir, page.resolved!))
ignore: page.removePage, return resolvedPagePaths.sort()
pathname: '/' + path.relative(pagesDir, page.resolved!),
}))
if (searchAllPages) {
const pageSet = new Set(await collectPages(pagesDir, pageExtensions))
resolvedPagePaths
.filter(p => p.ignore)
.forEach(p => pageSet.delete(p.pathname.replace('/', path.sep)))
pagePaths = [...pageSet]
} else {
pagePaths = resolvedPagePaths.filter(p => !p.ignore).map(p => p.pathname)
}
return pagePaths.sort()
} }
export default async function build( export default async function build(dir: string, conf = null): Promise<void> {
dir: string,
conf = null,
{ pages: _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'
...@@ -151,28 +119,21 @@ export default async function build( ...@@ -151,28 +119,21 @@ export default async function build(
const distDir = path.join(dir, config.distDir) const distDir = path.join(dir, config.distDir)
const pagesDir = path.join(dir, 'pages') const pagesDir = path.join(dir, 'pages')
const pages = const selectivePageBuilding = Boolean(
Array.isArray(_pages) && _pages.length config.experimental.flyingShuttle ||
? _pages process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE
: process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE )
? [process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE]
: []
const selectivePageBuilding = pages ? Boolean(pages.length) : false
if (selectivePageBuilding && config.target !== 'serverless') { if (selectivePageBuilding && config.target !== 'serverless') {
throw new Error( throw new Error('Cannot use flying shuttle without the serverless target.')
'Cannot use selective page building without the serverless target.'
)
} }
let pagePaths let pagePaths
if (selectivePageBuilding) { if (process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE) {
pagePaths = await getSpecifiedPages( pagePaths = await getSpecifiedPages(
dir, dir,
pages, process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE!,
config.pageExtensions, config.pageExtensions
config.experimental.flyingShuttle
) )
} else { } else {
pagePaths = await collectPages(pagesDir, config.pageExtensions) pagePaths = await collectPages(pagesDir, config.pageExtensions)
...@@ -182,7 +143,7 @@ export default async function build( ...@@ -182,7 +143,7 @@ export default async function build(
mappedPages, mappedPages,
config.target, config.target,
buildId, buildId,
selectivePageBuilding, /* dynamicBuildId */ selectivePageBuilding,
config config
) )
const configs = await Promise.all([ const configs = await Promise.all([
......
...@@ -11,7 +11,6 @@ const nextBuild: cliCommand = (argv) => { ...@@ -11,7 +11,6 @@ const nextBuild: cliCommand = (argv) => {
{ {
// Types // Types
'--help': Boolean, '--help': Boolean,
'--experimental-page': [String],
// Aliases // Aliases
'-h': '--help', '-h': '--help',
}, },
...@@ -56,7 +55,7 @@ const nextBuild: cliCommand = (argv) => { ...@@ -56,7 +55,7 @@ const nextBuild: cliCommand = (argv) => {
) )
} }
build(dir, undefined, { pages: args['--experimental-page'] }).catch((err) => { build(dir).catch((err) => {
// tslint:disable-next-line // tslint:disable-next-line
console.error('> Build error occurred') console.error('> Build error occurred')
printAndExit(err) printAndExit(err)
......
module.exports = {
target: 'serverless',
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
}
}
...@@ -4,5 +4,7 @@ module.exports = { ...@@ -4,5 +4,7 @@ module.exports = {
// Make sure entries are not getting disposed. // Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60 maxInactiveAge: 1000 * 60 * 60
}, },
lambdas: true experimental: {
flyingShuttle: true
}
} }
...@@ -8,13 +8,13 @@ import { nextBuild } from 'next-test-utils' ...@@ -8,13 +8,13 @@ import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Chunk Graph on Build', () => { describe('Flying Shuttle', () => {
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir, ['--experimental-page', '/']) await nextBuild(appDir)
}) })
describe('Module collection', () => { describe('Chunk Graph Module file', () => {
it('should build a chunk graph file', () => { it('should emit a manifest file', () => {
const cgf = join(__dirname, `/../.next/${CHUNK_GRAPH_MANIFEST}`) const cgf = join(__dirname, `/../.next/${CHUNK_GRAPH_MANIFEST}`)
expect(existsSync(cgf)).toBeTruthy() expect(existsSync(cgf)).toBeTruthy()
expect( expect(
......
import fetch from 'isomorphic-unfetch'
import React from 'react'
export default class extends React.Component {
static async getInitialProps () {
try {
const res = await fetch('')
const text = await res.text()
console.log(text)
return { text }
} catch (err) {
if (err.message.includes('is not a function')) {
return { failed: true, error: err.toString() }
}
return { error: err.toString() }
}
}
render () {
const { failed, error, text } = this.props
return (
<div className='fetch-page'>
{failed ? 'failed' : ''}
{error}
<div id='text'>{text}</div>
</div>
)
}
}
import Link from 'next/link'
export default () => {
return (
<div>
Hello World
<Link href='/fetch'>
<a id='fetchlink'>fetch page</a>
</Link>
</div>
)
}
const start = require('./server')
start(3000).then(() => console.log('http://localhost:3000'))
const express = require('express')
const http = require('http')
const path = require('path')
module.exports = function start (port = 0) {
return new Promise((resolve, reject) => {
const app = express()
const nextStaticDir = path.join(__dirname, '.next', 'static')
app.use('/_next/static', express.static(nextStaticDir))
app.get('/', (req, res) => {
require('./.next/serverless/pages/index.js').render(req, res)
})
app.get('/fetch', (req, res) => {
require('./.next/serverless/pages/fetch.js').render(req, res)
})
app.get('/404', (req, res) => {
require('./.next/serverless/pages/_error.js').render(req, res)
})
const server = new http.Server(app)
server.listen(port, err => {
if (err) {
return reject(err)
}
resolve(server)
})
})
}
/* eslint-env jest */
/* global jasmine, test */
import webdriver from 'next-webdriver'
import { join } from 'path'
import { existsSync } from 'fs'
import { nextBuild, stopApp, renderViaHTTP } from 'next-test-utils'
import startServer from '../server'
import fetch from 'node-fetch'
const appDir = join(__dirname, '../')
let appPort
let server
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Serverless', () => {
beforeAll(async () => {
await nextBuild(appDir, [
'--experimental-page',
'/',
'--experimental-page',
'/fetch'
])
server = await startServer()
appPort = server.address().port
})
afterAll(() => stopApp(server))
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})
it('should render correctly when importing isomorphic-unfetch', async () => {
const url = `http://localhost:${appPort}/fetch`
const res = await fetch(url)
expect(res.status).toBe(200)
const text = await res.text()
expect(text.includes('failed')).toBe(false)
})
it('should render correctly when importing isomorphic-unfetch on the client side', async () => {
const browser = await webdriver(appPort, '/')
try {
const text = await browser
.elementByCss('a')
.click()
.waitForElementByCss('.fetch-page')
.elementByCss('#text')
.text()
expect(text).toMatch(/fetch page/)
} finally {
await browser.close()
}
})
it('should not output abc.js to serverless build', () => {
const serverlessDir = join(appDir, '.next/serverless/pages')
expect(existsSync(join(serverlessDir, 'abc.js'))).toBeFalsy()
})
})
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`getSpecifiedPages should filter pages 1`] = `
Array [
"/_app.js",
"/_document.js",
]
`;
exports[`getSpecifiedPages should filter pages 2`] = `
Array [
"/_app.js",
"/_document.js",
"/b.jsx",
"/c/index.mdx",
]
`;
exports[`getSpecifiedPages should filter pages 3`] = `
Array [
"/_app.js",
"/_document.js",
"/b.jsx",
]
`;
exports[`getSpecifiedPages should only choose selected 1`] = ` exports[`getSpecifiedPages should only choose selected 1`] = `
Array [ Array [
"/_app.js", "/_app.js",
...@@ -59,21 +35,3 @@ Array [ ...@@ -59,21 +35,3 @@ Array [
"/c/index.mdx", "/c/index.mdx",
] ]
`; `;
exports[`getSpecifiedPages should select all 1`] = `
Array [
"/_app.js",
"/_document.js",
"/a.js",
]
`;
exports[`getSpecifiedPages should select all 2`] = `
Array [
"/_app.js",
"/_document.js",
"/a.js",
"/b.jsx",
"/c/index.mdx",
]
`;
...@@ -6,61 +6,26 @@ const getSpecifiedPages = async (...args) => ...@@ -6,61 +6,26 @@ const getSpecifiedPages = async (...args) =>
describe('getSpecifiedPages', () => { describe('getSpecifiedPages', () => {
it('should only choose selected', async () => { it('should only choose selected', async () => {
expect(await getSpecifiedPages(__dirname, ['a'], ['js'])).toMatchSnapshot() expect(await getSpecifiedPages(__dirname, 'a', ['js'])).toMatchSnapshot()
let err let err
try { try {
await getSpecifiedPages(__dirname, ['a', 'b'], ['js']) await getSpecifiedPages(__dirname, 'a,b', ['js'])
} catch (e) { } catch (e) {
err = e err = e
} }
expect(err).toBeTruthy() expect(err).toBeTruthy()
expect( expect(
await getSpecifiedPages(__dirname, ['a', 'b'], ['js', 'jsx']) await getSpecifiedPages(__dirname, 'a,b', ['js', 'jsx'])
).toMatchSnapshot() ).toMatchSnapshot()
expect( expect(
await getSpecifiedPages(__dirname, ['a', 'b'], ['js', 'jsx', 'mdx']) await getSpecifiedPages(__dirname, 'a,b', ['js', 'jsx', 'mdx'])
).toMatchSnapshot() ).toMatchSnapshot()
expect( expect(
await getSpecifiedPages(__dirname, ['a', 'b', 'c'], ['js', 'jsx', 'mdx']) await getSpecifiedPages(__dirname, 'a,b,c', ['js', 'jsx', 'mdx'])
).toMatchSnapshot()
expect(
await getSpecifiedPages(__dirname, ['a', 'b', 'c'], ['js', 'jsx', 'mdx'])
).toEqual(
await getSpecifiedPages(__dirname, ['a,b,c'], ['js', 'jsx', 'mdx'])
)
expect(
await getSpecifiedPages(__dirname, ['a', 'b', 'c'], ['js', 'jsx', 'mdx'])
).toEqual(
await getSpecifiedPages(__dirname, ['a,c', 'b'], ['js', 'jsx', 'mdx'])
)
expect(
await getSpecifiedPages(__dirname, ['a', 'b', 'c'], ['js', 'jsx', 'mdx'])
).toEqual(
await getSpecifiedPages(__dirname, ['a', 'c,b'], ['js', 'jsx', 'mdx'])
)
})
it('should select all', async () => {
expect(await getSpecifiedPages(__dirname, ['**'], ['js'])).toMatchSnapshot()
expect(
await getSpecifiedPages(__dirname, ['**'], ['js', 'mdx', 'jsx'])
).toMatchSnapshot()
})
it('should filter pages', async () => {
expect(
await getSpecifiedPages(__dirname, ['**', '-a'], ['js'])
).toMatchSnapshot()
expect(
await getSpecifiedPages(__dirname, ['**', '-a'], ['js', 'mdx', 'jsx'])
).toMatchSnapshot()
expect(
await getSpecifiedPages(__dirname, ['**,-a', '-c'], ['js', 'mdx', 'jsx'])
).toMatchSnapshot() ).toMatchSnapshot()
}) })
}) })
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