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>
|
return <head>
|
||||||
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${realPathname}`} as='script' />
|
<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()}
|
{this.getPreloadMainLinks()}
|
||||||
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
|
||||||
{styles || null}
|
{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${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()}
|
{staticMarkup ? null : this.getScripts()}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,23 @@
|
||||||
import del from 'del'
|
import del from 'del'
|
||||||
import cp from 'recursive-copy'
|
import cp from 'recursive-copy'
|
||||||
import mkdirp from 'mkdirp-then'
|
import mkdirp from 'mkdirp-then'
|
||||||
import { resolve, join } from 'path'
|
import { resolve, join, dirname, sep } from 'path'
|
||||||
import { existsSync, readFileSync } from 'fs'
|
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
||||||
|
import getConfig from './config'
|
||||||
|
import { renderToHTML } from './render'
|
||||||
|
import { printAndExit } from '../lib/utils'
|
||||||
|
|
||||||
export default async function (dir) {
|
export default async function (dir) {
|
||||||
const outDir = resolve(dir, '.out')
|
dir = resolve(dir)
|
||||||
const nextDir = resolve(dir, '.next')
|
const outDir = join(dir, '.out')
|
||||||
|
const nextDir = join(dir, '.next')
|
||||||
|
|
||||||
if (!existsSync(nextDir)) {
|
if (!existsSync(nextDir)) {
|
||||||
console.error('Build your with "next build" before running "next start".')
|
console.error('Build your with "next build" before running "next start".')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const config = getConfig(dir)
|
||||||
const buildId = readFileSync(join(nextDir, 'BUILD_ID'), 'utf8')
|
const buildId = readFileSync(join(nextDir, 'BUILD_ID'), 'utf8')
|
||||||
const buildStats = require(join(nextDir, 'build-stats.json'))
|
const buildStats = require(join(nextDir, 'build-stats.json'))
|
||||||
|
|
||||||
|
@ -31,4 +36,41 @@ export default async function (dir) {
|
||||||
join(nextDir, 'bundles', 'pages'),
|
join(nextDir, 'bundles', 'pages'),
|
||||||
join(outDir, '_next', buildId, 'page')
|
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