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
This commit is contained in:
Joe Haddad 2019-04-09 22:57:46 -04:00 committed by GitHub
parent 1810cf5abe
commit b543071440
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 30 additions and 285 deletions

View file

@ -56,29 +56,16 @@ function getPossibleFiles(pageExtensions: string[], pages: string[]) {
export async function getSpecifiedPages(
dir: string,
pages: string[],
pageExtensions: string[],
assumeAll: boolean
pagesString: string,
pageExtensions: string[]
) {
const pagesDir = path.join(dir, 'pages')
const reservedPages = ['/_app', '/_document', '/_error']
pages.push(...reservedPages)
const searchAllPages = assumeAll || pages.some(p => p.includes('**'))
let pagePaths: string[]
const explodedPages = [
...new Set(
flatten<string>(pages.map(p => p.split(',').filter(p => p !== '**')))
),
...new Set([...pagesString.split(','), ...reservedPages]),
].map(p => {
let removePage = false
if (p.startsWith('-')) {
p = p.substring(1)
removePage = true
}
let resolvedPage: string | undefined
if (path.isAbsolute(p)) {
resolvedPage = getPossibleFiles(pageExtensions, [
@ -91,7 +78,7 @@ export async function getSpecifiedPages(
path.join(dir, p),
]).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(
@ -100,33 +87,14 @@ export async function getSpecifiedPages(
if (missingPage) {
throw new Error(`Unable to identify page: ${missingPage.original}`)
}
const resolvedPagePaths = explodedPages
.filter(page => page.resolved)
.map(page => ({
ignore: page.removePage,
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()
.map(page => '/' + path.relative(pagesDir, page.resolved!))
return resolvedPagePaths.sort()
}
export default async function build(
dir: string,
conf = null,
{ pages: _pages = [] as string[] } = {}
): Promise<void> {
export default async function build(dir: string, conf = null): Promise<void> {
if (!(await isWriteable(dir))) {
throw new Error(
'> Build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable'
@ -151,28 +119,21 @@ export default async function build(
const distDir = path.join(dir, config.distDir)
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]
: []
const selectivePageBuilding = pages ? Boolean(pages.length) : false
const selectivePageBuilding = Boolean(
config.experimental.flyingShuttle ||
process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE
)
if (selectivePageBuilding && config.target !== 'serverless') {
throw new Error(
'Cannot use selective page building without the serverless target.'
)
throw new Error('Cannot use flying shuttle without the serverless target.')
}
let pagePaths
if (selectivePageBuilding) {
if (process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE) {
pagePaths = await getSpecifiedPages(
dir,
pages,
config.pageExtensions,
config.experimental.flyingShuttle
process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE!,
config.pageExtensions
)
} else {
pagePaths = await collectPages(pagesDir, config.pageExtensions)
@ -182,7 +143,7 @@ export default async function build(
mappedPages,
config.target,
buildId,
selectivePageBuilding,
/* dynamicBuildId */ selectivePageBuilding,
config
)
const configs = await Promise.all([

View file

@ -11,7 +11,6 @@ const nextBuild: cliCommand = (argv) => {
{
// Types
'--help': Boolean,
'--experimental-page': [String],
// Aliases
'-h': '--help',
},
@ -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
console.error('> Build error occurred')
printAndExit(err)

View file

@ -1,7 +0,0 @@
module.exports = {
target: 'serverless',
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
}
}

View file

@ -4,5 +4,7 @@ module.exports = {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
},
lambdas: true
experimental: {
flyingShuttle: true
}
}

View file

@ -8,13 +8,13 @@ import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '../')
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Chunk Graph on Build', () => {
describe('Flying Shuttle', () => {
beforeAll(async () => {
await nextBuild(appDir, ['--experimental-page', '/'])
await nextBuild(appDir)
})
describe('Module collection', () => {
it('should build a chunk graph file', () => {
describe('Chunk Graph Module file', () => {
it('should emit a manifest file', () => {
const cgf = join(__dirname, `/../.next/${CHUNK_GRAPH_MANIFEST}`)
expect(existsSync(cgf)).toBeTruthy()
expect(

View file

@ -1 +0,0 @@
export default () => <div>test</div>

View file

@ -1,29 +0,0 @@
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>
)
}
}

View file

@ -1,11 +0,0 @@
import Link from 'next/link'
export default () => {
return (
<div>
Hello World
<Link href='/fetch'>
<a id='fetchlink'>fetch page</a>
</Link>
</div>
)
}

View file

@ -1,2 +0,0 @@
const start = require('./server')
start(3000).then(() => console.log('http://localhost:3000'))

View file

@ -1,29 +0,0 @@
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)
})
})
}

View file

@ -1,61 +0,0 @@
/* 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()
})
})

View file

@ -1,29 +1,5 @@
// 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`] = `
Array [
"/_app.js",
@ -59,21 +35,3 @@ Array [
"/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",
]
`;

View file

@ -6,61 +6,26 @@ const getSpecifiedPages = async (...args) =>
describe('getSpecifiedPages', () => {
it('should only choose selected', async () => {
expect(await getSpecifiedPages(__dirname, ['a'], ['js'])).toMatchSnapshot()
expect(await getSpecifiedPages(__dirname, 'a', ['js'])).toMatchSnapshot()
let err
try {
await getSpecifiedPages(__dirname, ['a', 'b'], ['js'])
await getSpecifiedPages(__dirname, 'a,b', ['js'])
} catch (e) {
err = e
}
expect(err).toBeTruthy()
expect(
await getSpecifiedPages(__dirname, ['a', 'b'], ['js', 'jsx'])
await getSpecifiedPages(__dirname, 'a,b', ['js', 'jsx'])
).toMatchSnapshot()
expect(
await getSpecifiedPages(__dirname, ['a', 'b'], ['js', 'jsx', 'mdx'])
await getSpecifiedPages(__dirname, 'a,b', ['js', 'jsx', 'mdx'])
).toMatchSnapshot()
expect(
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'])
await getSpecifiedPages(__dirname, 'a,b,c', ['js', 'jsx', 'mdx'])
).toMatchSnapshot()
})
})