Commit 62a6ece4 authored by Joe Haddad's avatar Joe Haddad Committed by JJ Kasper

Emit Static Export Intent (#9737)

* Emit Static Export Intent

* Remove the old export detail on rebuild

* Add test cases

* fix export

* Add a resolve return
parent 677a9509
...@@ -18,6 +18,8 @@ import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup' ...@@ -18,6 +18,8 @@ import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import { import {
BUILD_MANIFEST, BUILD_MANIFEST,
DEFAULT_REDIRECT_STATUS, DEFAULT_REDIRECT_STATUS,
EXPORT_DETAIL,
EXPORT_MARKER,
PAGES_MANIFEST, PAGES_MANIFEST,
PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_BUILD,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
...@@ -716,6 +718,22 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -716,6 +718,22 @@ export default async function build(dir: string, conf = null): Promise<void> {
) )
} }
await fsWriteFile(
path.join(distDir, EXPORT_MARKER),
JSON.stringify({
version: 1,
hasExportPathMap: typeof config.exportPathMap === 'function',
exportTrailingSlash: config.exportTrailingSlash === true,
}),
'utf8'
)
await fsUnlink(path.join(distDir, EXPORT_DETAIL)).catch(err => {
if (err.code === 'ENOENT') {
return Promise.resolve()
}
return Promise.reject(err)
})
staticPages.forEach(pg => allStaticPages.add(pg)) staticPages.forEach(pg => allStaticPages.add(pg))
pageInfos.forEach((info: PageInfo, key: string) => { pageInfos.forEach((info: PageInfo, key: string) => {
allPageInfos.set(key, info) allPageInfos.set(key, info)
......
import chalk from 'chalk' import chalk from 'chalk'
import { copyFile as copyFileOrig, existsSync, readFileSync } from 'fs' import findUp from 'find-up'
import {
copyFile as copyFileOrig,
existsSync,
readFileSync,
writeFileSync,
} from 'fs'
import Worker from 'jest-worker' import Worker from 'jest-worker'
import mkdirpModule from 'mkdirp' import mkdirpModule from 'mkdirp'
import { cpus } from 'os' import { cpus } from 'os'
import { dirname, join, resolve, sep } from 'path' import { dirname, join, resolve, sep } from 'path'
import { promisify } from 'util' import { promisify } from 'util'
import findUp from 'find-up'
import { AmpPageStatus, formatAmpMessages } from '../build/output/index' import { AmpPageStatus, formatAmpMessages } from '../build/output/index'
import createSpinner from '../build/spinner' import createSpinner from '../build/spinner'
import { API_ROUTE } from '../lib/constants' import { API_ROUTE } from '../lib/constants'
...@@ -16,11 +21,12 @@ import { ...@@ -16,11 +21,12 @@ import {
CLIENT_PUBLIC_FILES_PATH, CLIENT_PUBLIC_FILES_PATH,
CLIENT_STATIC_FILES_PATH, CLIENT_STATIC_FILES_PATH,
CONFIG_FILE, CONFIG_FILE,
EXPORT_DETAIL,
PAGES_MANIFEST, PAGES_MANIFEST,
PHASE_EXPORT, PHASE_EXPORT,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
} from '../next-server/lib/constants' } from '../next-server/lib/constants'
import loadConfig, { import loadConfig, {
isTargetLikeServerless, isTargetLikeServerless,
...@@ -180,6 +186,16 @@ export default async function( ...@@ -180,6 +186,16 @@ export default async function(
await recursiveDelete(join(outDir)) await recursiveDelete(join(outDir))
await mkdirp(join(outDir, '_next', buildId)) await mkdirp(join(outDir, '_next', buildId))
writeFileSync(
join(distDir, EXPORT_DETAIL),
JSON.stringify({
version: 1,
outDirectory: outDir,
success: false,
}),
'utf8'
)
// Copy static directory // Copy static directory
if (!options.buildExport && existsSync(join(dir, 'static'))) { if (!options.buildExport && existsSync(join(dir, 'static'))) {
log(' copying "static" directory') log(' copying "static" directory')
...@@ -372,6 +388,16 @@ export default async function( ...@@ -372,6 +388,16 @@ export default async function(
// Add an empty line to the console for the better readability. // Add an empty line to the console for the better readability.
log('') log('')
writeFileSync(
join(distDir, EXPORT_DETAIL),
JSON.stringify({
version: 1,
outDirectory: outDir,
success: true,
}),
'utf8'
)
if (telemetry) { if (telemetry) {
await telemetry.flush() await telemetry.flush()
} }
......
...@@ -4,6 +4,8 @@ export const PHASE_PRODUCTION_SERVER = 'phase-production-server' ...@@ -4,6 +4,8 @@ export const PHASE_PRODUCTION_SERVER = 'phase-production-server'
export const PHASE_DEVELOPMENT_SERVER = 'phase-development-server' export const PHASE_DEVELOPMENT_SERVER = 'phase-development-server'
export const PAGES_MANIFEST = 'pages-manifest.json' export const PAGES_MANIFEST = 'pages-manifest.json'
export const BUILD_MANIFEST = 'build-manifest.json' export const BUILD_MANIFEST = 'build-manifest.json'
export const EXPORT_MARKER = 'export-marker.json'
export const EXPORT_DETAIL = 'export-detail.json'
export const PRERENDER_MANIFEST = 'prerender-manifest.json' export const PRERENDER_MANIFEST = 'prerender-manifest.json'
export const ROUTES_MANIFEST = 'routes-manifest.json' export const ROUTES_MANIFEST = 'routes-manifest.json'
export const REACT_LOADABLE_MANIFEST = 'react-loadable-manifest.json' export const REACT_LOADABLE_MANIFEST = 'react-loadable-manifest.json'
......
const A = () => {
throw new Error('fail da export')
}
A.getInitialProps = () => {}
export default A
module.exports = {
exportPathMap() {
return { '/': { page: '/' } }
},
}
module.exports = {
exportTrailingSlash: true,
}
export default () => <div>Hello Export</div>
/* eslint-env jest */
/* global jasmine */
import { remove } from 'fs-extra'
import { nextBuild, nextExport, nextExportDefault } from 'next-test-utils'
import path, { join } from 'path'
import fs from 'fs'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const fixturesDir = join(__dirname, '..', 'fixtures')
describe('Application Export Intent Output', () => {
describe('Default Export', () => {
const appDir = join(fixturesDir, 'default-export')
const distDir = join(appDir, '.next')
beforeAll(async () => {
await remove(distDir)
})
it('should build and export', async () => {
await nextBuild(appDir)
await nextExportDefault(appDir)
})
it('should have the expected outputs for export', () => {
expect(
JSON.parse(fs.readFileSync(join(distDir, 'export-marker.json'), 'utf8'))
).toMatchInlineSnapshot(`
Object {
"exportTrailingSlash": false,
"hasExportPathMap": false,
"version": 1,
}
`)
const detail = JSON.parse(
fs.readFileSync(join(distDir, 'export-detail.json'), 'utf8')
)
expect({
...detail,
outDirectory: path.basename(detail.outDirectory),
}).toMatchInlineSnapshot(`
Object {
"outDirectory": "out",
"success": true,
"version": 1,
}
`)
})
})
describe('Custom Export', () => {
const appDir = join(fixturesDir, 'custom-export')
const distDir = join(appDir, '.next')
beforeAll(async () => {
await remove(distDir)
})
it('should build and export', async () => {
await nextBuild(appDir)
await nextExportDefault(appDir)
})
it('should have the expected outputs for export', () => {
expect(
JSON.parse(fs.readFileSync(join(distDir, 'export-marker.json'), 'utf8'))
).toMatchInlineSnapshot(`
Object {
"exportTrailingSlash": false,
"hasExportPathMap": true,
"version": 1,
}
`)
const detail = JSON.parse(
fs.readFileSync(join(distDir, 'export-detail.json'), 'utf8')
)
expect({
...detail,
outDirectory: path.basename(detail.outDirectory),
}).toMatchInlineSnapshot(`
Object {
"outDirectory": "out",
"success": true,
"version": 1,
}
`)
})
})
describe('Custom Out', () => {
const appDir = join(fixturesDir, 'custom-out')
const distDir = join(appDir, '.next')
beforeAll(async () => {
await remove(distDir)
})
it('should build and export', async () => {
await nextBuild(appDir)
await nextExport(appDir, { outdir: join(appDir, 'lel') })
})
it('should have the expected outputs for export', () => {
expect(
JSON.parse(fs.readFileSync(join(distDir, 'export-marker.json'), 'utf8'))
).toMatchInlineSnapshot(`
Object {
"exportTrailingSlash": true,
"hasExportPathMap": false,
"version": 1,
}
`)
const detail = JSON.parse(
fs.readFileSync(join(distDir, 'export-detail.json'), 'utf8')
)
expect({
...detail,
outDirectory: path.basename(detail.outDirectory),
}).toMatchInlineSnapshot(`
Object {
"outDirectory": "lel",
"success": true,
"version": 1,
}
`)
})
})
describe('Bad Export', () => {
const appDir = join(fixturesDir, 'bad-export')
const distDir = join(appDir, '.next')
beforeAll(async () => {
await remove(distDir)
})
it('should build and export', async () => {
await nextBuild(appDir)
await nextExportDefault(appDir)
})
it('should have the expected outputs for export', () => {
expect(
JSON.parse(fs.readFileSync(join(distDir, 'export-marker.json'), 'utf8'))
).toMatchInlineSnapshot(`
Object {
"exportTrailingSlash": false,
"hasExportPathMap": false,
"version": 1,
}
`)
const detail = JSON.parse(
fs.readFileSync(join(distDir, 'export-detail.json'), 'utf8')
)
expect({
...detail,
outDirectory: path.basename(detail.outDirectory),
}).toMatchInlineSnapshot(`
Object {
"outDirectory": "out",
"success": false,
"version": 1,
}
`)
})
})
describe('No Export', () => {
const appDir = join(fixturesDir, 'no-export')
const distDir = join(appDir, '.next')
beforeAll(async () => {
await remove(distDir)
})
it('should build and not export', async () => {
await nextBuild(appDir)
})
it('should have the expected outputs for export', () => {
expect(
JSON.parse(fs.readFileSync(join(distDir, 'export-marker.json'), 'utf8'))
).toMatchInlineSnapshot(`
Object {
"exportTrailingSlash": false,
"hasExportPathMap": false,
"version": 1,
}
`)
expect(() => {
fs.readFileSync(join(distDir, 'export-detail.json'), 'utf8')
}).toThrowError(/ENOENT/)
})
it('should export and create file', async () => {
await nextExportDefault(appDir)
expect(() => {
fs.readFileSync(join(distDir, 'export-detail.json'), 'utf8')
}).not.toThrow()
})
it('should build and clean up', async () => {
await nextBuild(appDir)
expect(() => {
fs.readFileSync(join(distDir, 'export-detail.json'), 'utf8')
}).toThrowError(/ENOENT/)
})
})
})
...@@ -192,6 +192,10 @@ export function nextExport(dir, { outdir }, opts = {}) { ...@@ -192,6 +192,10 @@ export function nextExport(dir, { outdir }, opts = {}) {
return runNextCommand(['export', dir, '--outdir', outdir], opts) return runNextCommand(['export', dir, '--outdir', outdir], opts)
} }
export function nextExportDefault(dir, opts = {}) {
return runNextCommand(['export', dir], opts)
}
export function nextStart(dir, port, opts = {}) { export function nextStart(dir, port, opts = {}) {
return runNextCommandDev(['start', '-p', port, dir], undefined, opts) return runNextCommandDev(['start', '-p', port, dir], undefined, opts)
} }
......
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