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

Convert Export to TypeScript (#8965)

This pull request converts Next.js' export CLI command to use TypeScript. This is useful for upcoming changes.
parent 9af58793
...@@ -41,7 +41,7 @@ type AmpStatus = { ...@@ -41,7 +41,7 @@ type AmpStatus = {
code: string code: string
} }
type AmpPageStatus = { export type AmpPageStatus = {
[page: string]: { errors: AmpStatus[]; warnings: AmpStatus[] } [page: string]: { errors: AmpStatus[]; warnings: AmpStatus[] }
} }
......
export default function(
dir: string,
options: any,
configuration?: any
): Promise<void>
import { cpus } from 'os'
import chalk from 'chalk' import chalk from 'chalk'
import { copyFile as copyFileOrig, existsSync, readFileSync } from 'fs'
import Worker from 'jest-worker' import Worker from 'jest-worker'
import { promisify } from 'util'
import mkdirpModule from 'mkdirp' import mkdirpModule from 'mkdirp'
import { resolve, join, dirname } from 'path' import { cpus } from 'os'
import { dirname, join, resolve } from 'path'
import { promisify } from 'util'
import { AmpPageStatus, formatAmpMessages } from '../build/output/index'
import createSpinner from '../build/spinner'
import { API_ROUTE } from '../lib/constants' import { API_ROUTE } from '../lib/constants'
import { existsSync, readFileSync, copyFile as copyFileOrig } from 'fs'
import { recursiveCopy } from '../lib/recursive-copy' import { recursiveCopy } from '../lib/recursive-copy'
import { recursiveDelete } from '../lib/recursive-delete' import { recursiveDelete } from '../lib/recursive-delete'
import { formatAmpMessages } from '../build/output/index'
import { setDistDir as setTelemetryDir } from '../telemetry/storage'
import { recordVersion } from '../telemetry/events'
import loadConfig, {
isTargetLikeServerless
} from '../next-server/server/config'
import { import {
PHASE_EXPORT,
SERVER_DIRECTORY,
PAGES_MANIFEST,
CONFIG_FILE,
BUILD_ID_FILE, BUILD_ID_FILE,
CLIENT_PUBLIC_FILES_PATH,
CLIENT_STATIC_FILES_PATH,
CONFIG_FILE,
PAGES_MANIFEST,
PHASE_EXPORT,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
CLIENT_PUBLIC_FILES_PATH,
CLIENT_STATIC_FILES_PATH
} from '../next-server/lib/constants' } from '../next-server/lib/constants'
import createSpinner from '../build/spinner' import loadConfig, {
isTargetLikeServerless,
} from '../next-server/server/config'
import { recordVersion } from '../telemetry/events'
import { setDistDir as setTelemetryDir } from '../telemetry/storage'
const mkdirp = promisify(mkdirpModule) const mkdirp = promisify(mkdirpModule)
const copyFile = promisify(copyFileOrig) const copyFile = promisify(copyFileOrig)
const createProgress = (total, label = 'Exporting') => { const createProgress = (total: number, label = 'Exporting') => {
let curProgress = 0 let curProgress = 0
let progressSpinner = createSpinner(`${label} (${curProgress}/${total})`, { let progressSpinner = createSpinner(`${label} (${curProgress}/${total})`, {
spinner: { spinner: {
...@@ -49,10 +50,10 @@ const createProgress = (total, label = 'Exporting') => { ...@@ -49,10 +50,10 @@ const createProgress = (total, label = 'Exporting') => {
'[====]', '[====]',
'[=== ]', '[=== ]',
'[== ]', '[== ]',
'[= ]' '[= ]',
], ],
interval: 80 interval: 80,
} },
}) })
return () => { return () => {
...@@ -72,9 +73,19 @@ const createProgress = (total, label = 'Exporting') => { ...@@ -72,9 +73,19 @@ const createProgress = (total, label = 'Exporting') => {
} }
} }
export default async function (dir, options, configuration) { type ExportPathMap = {
function log (message) { [page: string]: { page: string; query?: { [key: string]: string } }
if (options.silent) return }
export default async function(
dir: string,
options: any,
configuration?: any
): Promise<void> {
function log(message: string) {
if (options.silent) {
return
}
console.log(message) console.log(message)
} }
...@@ -122,7 +133,7 @@ export default async function (dir, options, configuration) { ...@@ -122,7 +133,7 @@ export default async function (dir, options, configuration) {
) )
const pages = options.pages || Object.keys(pagesManifest) const pages = options.pages || Object.keys(pagesManifest)
const defaultPathMap = {} const defaultPathMap: ExportPathMap = {}
for (const page of pages) { for (const page of pages) {
// _document and _app are not real pages // _document and _app are not real pages
...@@ -165,7 +176,7 @@ export default async function (dir, options, configuration) { ...@@ -165,7 +176,7 @@ export default async function (dir, options, configuration) {
console.log( console.log(
`> No "exportPathMap" found in "${CONFIG_FILE}". Generating map from "./pages"` `> No "exportPathMap" found in "${CONFIG_FILE}". Generating map from "./pages"`
) )
nextConfig.exportPathMap = async defaultMap => { nextConfig.exportPathMap = async (defaultMap: ExportPathMap) => {
return defaultMap return defaultMap
} }
} }
...@@ -181,18 +192,18 @@ export default async function (dir, options, configuration) { ...@@ -181,18 +192,18 @@ export default async function (dir, options, configuration) {
staticMarkup: false, staticMarkup: false,
hotReloader: null, hotReloader: null,
canonicalBase: (nextConfig.amp && nextConfig.amp.canonicalBase) || '', canonicalBase: (nextConfig.amp && nextConfig.amp.canonicalBase) || '',
isModern: nextConfig.experimental.modern isModern: nextConfig.experimental.modern,
} }
const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig
if (Object.keys(publicRuntimeConfig).length > 0) { if (Object.keys(publicRuntimeConfig).length > 0) {
renderOpts.runtimeConfig = publicRuntimeConfig ;(renderOpts as any).runtimeConfig = publicRuntimeConfig
} }
// We need this for server rendering the Link component. // We need this for server rendering the Link component.
global.__NEXT_DATA__ = { ;(global as any).__NEXT_DATA__ = {
nextExport: true nextExport: true,
} }
log(` launching ${threads} workers`) log(` launching ${threads} workers`)
...@@ -201,11 +212,11 @@ export default async function (dir, options, configuration) { ...@@ -201,11 +212,11 @@ export default async function (dir, options, configuration) {
dir, dir,
outDir, outDir,
distDir, distDir,
buildId buildId,
}) })
if (!exportPathMap['/404']) { if (!exportPathMap['/404']) {
exportPathMap['/404.html'] = exportPathMap['/404.html'] || { exportPathMap['/404.html'] = exportPathMap['/404.html'] || {
page: '/_error' page: '/_error',
} }
} }
const exportPaths = Object.keys(exportPathMap) const exportPaths = Object.keys(exportPathMap)
...@@ -227,7 +238,7 @@ export default async function (dir, options, configuration) { ...@@ -227,7 +238,7 @@ export default async function (dir, options, configuration) {
const progress = !options.silent && createProgress(filteredPaths.length) const progress = !options.silent && createProgress(filteredPaths.length)
const sprDataDir = options.buildExport ? outDir : join(outDir, '_next/data') const sprDataDir = options.buildExport ? outDir : join(outDir, '_next/data')
const ampValidations = {} const ampValidations: AmpPageStatus = {}
let hadValidationError = false let hadValidationError = false
const publicDir = join(dir, CLIENT_PUBLIC_FILES_PATH) const publicDir = join(dir, CLIENT_PUBLIC_FILES_PATH)
...@@ -240,19 +251,22 @@ export default async function (dir, options, configuration) { ...@@ -240,19 +251,22 @@ export default async function (dir, options, configuration) {
) { ) {
log(' copying "public" directory') log(' copying "public" directory')
await recursiveCopy(publicDir, outDir, { await recursiveCopy(publicDir, outDir, {
filter (path) { filter(path) {
// Exclude paths used by pages // Exclude paths used by pages
return !exportPathMap[path] return !exportPathMap[path]
} },
}) })
} }
const worker = new Worker(require.resolve('./worker'), { const worker: Worker & { default: Function } = new Worker(
maxRetries: 0, require.resolve('./worker'),
numWorkers: threads, {
enableWorkerThreads: true, maxRetries: 0,
exposedMethods: ['default'] numWorkers: threads,
}) enableWorkerThreads: true,
exposedMethods: ['default'],
}
) as any
worker.getStdout().pipe(process.stdout) worker.getStdout().pipe(process.stdout)
worker.getStderr().pipe(process.stderr) worker.getStderr().pipe(process.stderr)
...@@ -272,16 +286,17 @@ export default async function (dir, options, configuration) { ...@@ -272,16 +286,17 @@ export default async function (dir, options, configuration) {
serverRuntimeConfig, serverRuntimeConfig,
subFolders, subFolders,
buildExport: options.buildExport, buildExport: options.buildExport,
serverless: isTargetLikeServerless(nextConfig.target) serverless: isTargetLikeServerless(nextConfig.target),
}) })
for (const validation of result.ampValidations || []) { for (const validation of result.ampValidations || []) {
const { page, result } = validation const { page, result } = validation
ampValidations[page] = result ampValidations[page] = result
hadValidationError |= hadValidationError =
Array.isArray(result && result.errors) && result.errors.length > 0 hadValidationError ||
(Array.isArray(result && result.errors) && result.errors.length > 0)
} }
renderError |= result.error renderError = renderError || !!result.error
if ( if (
options.buildExport && options.buildExport &&
......
...@@ -105,7 +105,7 @@ function normalizeConfig(phase: string, config: any) { ...@@ -105,7 +105,7 @@ function normalizeConfig(phase: string, config: any) {
export default function loadConfig( export default function loadConfig(
phase: string, phase: string,
dir: string, dir: string,
customConfig: any customConfig?: object | null
) { ) {
if (customConfig) { if (customConfig) {
return assignDefaults({ configOrigin: 'server', ...customConfig }) return assignDefaults({ configOrigin: 'server', ...customConfig })
......
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