1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/packages/next/export/index.js

144 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-05-07 22:47:40 +00:00
import del from 'del'
import cp from 'recursive-copy'
import mkdirp from 'mkdirp-then'
import { extname, resolve, join, dirname, sep } from 'path'
2017-05-08 06:10:26 +00:00
import { existsSync, readFileSync, writeFileSync } from 'fs'
2018-10-01 22:55:31 +00:00
import loadConfig from 'next-server/next-config'
import {PHASE_EXPORT, SERVER_DIRECTORY, PAGES_MANIFEST, CONFIG_FILE, BUILD_ID_FILE, CLIENT_STATIC_FILES_PATH} from 'next-server/constants'
import { renderToHTML } from 'next-server/dist/server/render'
import { setAssetPrefix } from 'next-server/asset'
import * as envConfig from 'next-server/config'
2017-05-07 22:47:40 +00:00
export default async function (dir, options, configuration) {
function log (message) {
if (options.silent) return
console.log(message)
}
2017-05-08 06:10:26 +00:00
dir = resolve(dir)
const nextConfig = configuration || loadConfig(PHASE_EXPORT, dir)
const distDir = join(dir, nextConfig.distDir)
2017-05-07 22:47:40 +00:00
log(`> using build directory: ${distDir}`)
if (!existsSync(distDir)) {
throw new Error(`Build directory ${distDir} does not exist. Make sure you run "next build" before running "next start" or "next export".`)
2017-05-07 22:47:40 +00:00
}
const buildId = readFileSync(join(distDir, BUILD_ID_FILE), 'utf8')
const pagesManifest = require(join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST))
const pages = Object.keys(pagesManifest)
const defaultPathMap = {}
for (const page of pages) {
// _document and _app are not real pages.
if (page === '/_document' || page === '/_app') {
continue
}
if (page === '/_error') {
defaultPathMap['/404.html'] = { page }
continue
}
defaultPathMap[page] = { page }
}
2017-05-07 22:47:40 +00:00
// Initialize the output directory
const outDir = options.outdir
2017-05-18 04:28:22 +00:00
await del(join(outDir, '*'))
2017-05-07 22:47:40 +00:00
await mkdirp(join(outDir, '_next', buildId))
// Copy static directory
if (existsSync(join(dir, 'static'))) {
log(' copying "static" directory')
await cp(
join(dir, 'static'),
join(outDir, 'static'),
{ expand: true }
)
}
// Copy .next/static directory
if (existsSync(join(distDir, CLIENT_STATIC_FILES_PATH))) {
log(' copying "static build" directory')
await cp(
join(distDir, CLIENT_STATIC_FILES_PATH),
join(outDir, '_next', CLIENT_STATIC_FILES_PATH)
)
}
// Get the exportPathMap from the config file
if (typeof nextConfig.exportPathMap !== 'function') {
console.log(`> No "exportPathMap" found in "${CONFIG_FILE}". Generating map from "./pages"`)
nextConfig.exportPathMap = async (defaultMap) => {
return defaultMap
}
}
2017-05-08 06:10:26 +00:00
// Start the rendering process
const renderOpts = {
dir,
buildId,
nextExport: true,
assetPrefix: nextConfig.assetPrefix.replace(/\/$/, ''),
distDir,
2017-05-08 06:10:26 +00:00
dev: false,
staticMarkup: false,
hotReloader: null
}
2018-02-27 16:50:14 +00:00
const {serverRuntimeConfig, publicRuntimeConfig} = nextConfig
if (publicRuntimeConfig) {
renderOpts.runtimeConfig = publicRuntimeConfig
2017-05-08 06:10:26 +00:00
}
2018-02-27 16:50:14 +00:00
envConfig.setConfig({
serverRuntimeConfig,
publicRuntimeConfig
})
// set the assetPrefix to use for 'next/asset'
setAssetPrefix(renderOpts.assetPrefix)
// We need this for server rendering the Link component.
global.__NEXT_DATA__ = {
nextExport: true
2017-05-08 06:10:26 +00:00
}
const exportPathMap = await nextConfig.exportPathMap(defaultPathMap, {dev: false, dir, outDir, distDir, buildId})
const exportPaths = Object.keys(exportPathMap)
2017-05-08 06:10:26 +00:00
for (const path of exportPaths) {
log(`> exporting path: ${path}`)
if (!path.startsWith('/')) {
throw new Error(`path "${path}" doesn't start with a backslash`)
}
2017-05-09 01:53:08 +00:00
const { page, query = {} } = exportPathMap[path]
2017-05-08 06:10:26 +00:00
const req = { url: path }
const res = {}
let htmlFilename = `${path}${sep}index.html`
if (extname(path) !== '') {
// If the path has an extension, use that as the filename instead
htmlFilename = path
} else if (path === '/') {
// If the path is the root, just use index.html
htmlFilename = 'index.html'
}
2017-05-08 06:10:26 +00:00
const baseDir = join(outDir, dirname(htmlFilename))
const htmlFilepath = join(outDir, htmlFilename)
await mkdirp(baseDir)
const html = await renderToHTML(req, res, page, query, renderOpts)
writeFileSync(htmlFilepath, html, 'utf8')
}
// Add an empty line to the console for the better readability.
log('')
2017-05-07 22:47:40 +00:00
}