mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Allow next export to build html pages.
This commit is contained in:
parent
be2e5a8c23
commit
dcc3228429
|
@ -71,7 +71,7 @@ export class Head extends Component {
|
|||
|
||||
return <head>
|
||||
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${realPathname}`} as='script' />
|
||||
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error`} as='script' />
|
||||
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error.js`} as='script' />
|
||||
{this.getPreloadMainLinks()}
|
||||
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
||||
{styles || null}
|
||||
|
@ -148,7 +148,7 @@ export class NextScript extends Component {
|
|||
`
|
||||
}} />}
|
||||
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${realPathname}`} />
|
||||
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error`} />
|
||||
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error.js`} />
|
||||
{staticMarkup ? null : this.getScripts()}
|
||||
</div>
|
||||
}
|
||||
|
|
|
@ -1,18 +1,23 @@
|
|||
import del from 'del'
|
||||
import cp from 'recursive-copy'
|
||||
import mkdirp from 'mkdirp-then'
|
||||
import { resolve, join } from 'path'
|
||||
import { existsSync, readFileSync } from 'fs'
|
||||
import { resolve, join, dirname, sep } from 'path'
|
||||
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
||||
import getConfig from './config'
|
||||
import { renderToHTML } from './render'
|
||||
import { printAndExit } from '../lib/utils'
|
||||
|
||||
export default async function (dir) {
|
||||
const outDir = resolve(dir, '.out')
|
||||
const nextDir = resolve(dir, '.next')
|
||||
dir = resolve(dir)
|
||||
const outDir = join(dir, '.out')
|
||||
const nextDir = join(dir, '.next')
|
||||
|
||||
if (!existsSync(nextDir)) {
|
||||
console.error('Build your with "next build" before running "next start".')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const config = getConfig(dir)
|
||||
const buildId = readFileSync(join(nextDir, 'BUILD_ID'), 'utf8')
|
||||
const buildStats = require(join(nextDir, 'build-stats.json'))
|
||||
|
||||
|
@ -31,4 +36,41 @@ export default async function (dir) {
|
|||
join(nextDir, 'bundles', 'pages'),
|
||||
join(outDir, '_next', buildId, 'page')
|
||||
)
|
||||
|
||||
// Start the rendering process
|
||||
const renderOpts = {
|
||||
dir,
|
||||
buildStats,
|
||||
buildId,
|
||||
assetPrefix: config.assetPrefix.replace(/\/$/, ''),
|
||||
dev: false,
|
||||
staticMarkup: false,
|
||||
hotReloader: null
|
||||
}
|
||||
|
||||
// Get the exportPathMap from the `next.config.js`
|
||||
if (typeof config.exportPathMap !== 'function') {
|
||||
printAndExit(
|
||||
'> Could not found "exportPathMap" function inside "next.config.js"\n' +
|
||||
'> "next export" uses that function build html pages.'
|
||||
)
|
||||
}
|
||||
|
||||
const exportPathMap = await config.exportPathMap()
|
||||
const exportPaths = Object.keys(exportPathMap)
|
||||
|
||||
for (const path of exportPaths) {
|
||||
const { page, query } = exportPathMap[path]
|
||||
const req = { url: path }
|
||||
const res = {}
|
||||
|
||||
const htmlFilename = page === '/' ? 'index.html' : `${page}${sep}index.html`
|
||||
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')
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue