-
Notifications
You must be signed in to change notification settings - Fork 26.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add flow, pages-manifest.json, defaultPathMap for export (minor) #4066
Changes from 9 commits
fc09580
af12570
f60a4fa
d232646
c4d2af6
917d29c
6b61954
9ad271c
ca5d934
370de74
13f08d3
0a3c0d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"presets": [ | ||
"env", | ||
"react" | ||
"react", | ||
"flow" | ||
], | ||
"plugins": [ | ||
"transform-object-rest-spread", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ignore] | ||
<PROJECT_ROOT>/examples/.* | ||
<PROJECT_ROOT>/examples/.* | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// @flow | ||
import { RawSource } from 'webpack-sources' | ||
import { MATCH_ROUTE_NAME } from '../../utils' | ||
import {PAGES_MANIFEST} from '../../../lib/constants' | ||
|
||
export default class PagesManifestPlugin { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we add a comment on what this plugin is about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
apply (compiler: any) { | ||
compiler.plugin('emit', (compilation, callback) => { | ||
const {entries} = compilation | ||
const pages = {} | ||
|
||
for (const entry of entries) { | ||
const pagePath = MATCH_ROUTE_NAME.exec(entry.name)[1] | ||
|
||
if (!pagePath) { | ||
continue | ||
} | ||
|
||
const {name} = entry | ||
pages[`/${pagePath}`] = name | ||
} | ||
|
||
compilation.assets[PAGES_MANIFEST] = new RawSource(JSON.stringify(pages)) | ||
callback() | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,9 @@ import walk from 'walk' | |
import { extname, resolve, join, dirname, sep } from 'path' | ||
import { existsSync, readFileSync, writeFileSync } from 'fs' | ||
import getConfig from './config' | ||
import {PHASE_EXPORT} from '../lib/constants' | ||
import {PHASE_EXPORT, PAGES_MANIFEST} from '../lib/constants' | ||
import { renderToHTML } from './render' | ||
import { getAvailableChunks } from './utils' | ||
import { printAndExit } from '../lib/utils' | ||
import { setAssetPrefix } from '../lib/asset' | ||
import * as envConfig from '../lib/runtime-config' | ||
|
||
|
@@ -17,7 +16,7 @@ export default async function (dir, options, configuration) { | |
const nextConfig = configuration || getConfig(PHASE_EXPORT, dir) | ||
const nextDir = join(dir, nextConfig.distDir) | ||
|
||
log(` using build directory: ${nextDir}`) | ||
log(`> using build directory: ${nextDir}`) | ||
|
||
if (!existsSync(nextDir)) { | ||
console.error( | ||
|
@@ -27,6 +26,17 @@ export default async function (dir, options, configuration) { | |
} | ||
|
||
const buildId = readFileSync(join(nextDir, 'BUILD_ID'), 'utf8') | ||
const pagesManifest = require(join(nextDir, 'dist', PAGES_MANIFEST)) | ||
|
||
const pages = Object.keys(pagesManifest) | ||
const defaultPathMap = {} | ||
|
||
for (const page of pages) { | ||
if (page === '/_document') { | ||
continue | ||
} | ||
defaultPathMap[page] = { page } | ||
} | ||
|
||
// Initialize the output directory | ||
const outDir = options.outdir | ||
|
@@ -73,13 +83,13 @@ export default async function (dir, options, configuration) { | |
|
||
// Get the exportPathMap from the `next.config.js` | ||
if (typeof nextConfig.exportPathMap !== 'function') { | ||
printAndExit( | ||
'> Could not find "exportPathMap" function inside "next.config.js"\n' + | ||
'> "next export" uses that function to build html pages.' | ||
) | ||
console.log('> No "exportPathMap" found in "next.config.js". Generating map from "./pages"') | ||
nextConfig.exportPathMap = async (defaultMap) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log a message on this is exporting all the pages or something similar. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the |
||
return defaultMap | ||
} | ||
} | ||
|
||
const exportPathMap = await nextConfig.exportPathMap() | ||
const exportPathMap = await nextConfig.exportPathMap(defaultPathMap) | ||
const exportPaths = Object.keys(exportPathMap) | ||
|
||
// Start the rendering process | ||
|
@@ -115,7 +125,7 @@ export default async function (dir, options, configuration) { | |
} | ||
|
||
for (const path of exportPaths) { | ||
log(` exporting path: ${path}`) | ||
log(`> exporting path: ${path}`) | ||
if (!path.startsWith('/')) { | ||
throw new Error(`path "${path}" doesn't start with a backslash`) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import {join, parse, normalize, sep} from 'path' | ||
import fs from 'mz/fs' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's still used, but there's another PR to remove it. |
||
import {join, posix} from 'path' | ||
import {PAGES_MANIFEST} from '../lib/constants' | ||
|
||
export function pageNotFoundError (page) { | ||
const err = new Error(`Cannot find module for page: ${page}`) | ||
|
@@ -18,13 +18,8 @@ export function normalizePagePath (page) { | |
page = `/${page}` | ||
} | ||
|
||
// Windows compatibility | ||
if (sep !== '/') { | ||
page = page.replace(/\//g, sep) | ||
} | ||
|
||
// Throw when using ../ etc in the pathname | ||
const resolvedPage = normalize(page) | ||
const resolvedPage = posix.normalize(page) | ||
if (page !== resolvedPage) { | ||
throw new Error('Requested and resolved page mismatch') | ||
} | ||
|
@@ -33,7 +28,8 @@ export function normalizePagePath (page) { | |
} | ||
|
||
export function getPagePath (page, {dir, dist}) { | ||
const pageBundlesPath = join(dir, dist, 'dist', 'bundles', 'pages') | ||
const serverBuildPath = join(dir, dist, 'dist') | ||
const pagesManifest = require(join(serverBuildPath, PAGES_MANIFEST)) | ||
|
||
try { | ||
page = normalizePagePath(page) | ||
|
@@ -42,24 +38,14 @@ export function getPagePath (page, {dir, dist}) { | |
throw pageNotFoundError(page) | ||
} | ||
|
||
const pagePath = join(pageBundlesPath, page) // Path to the page that is to be loaded | ||
|
||
// Don't allow wandering outside of the bundles directory | ||
const pathDir = parse(pagePath).dir | ||
if (pathDir.indexOf(pageBundlesPath) !== 0) { | ||
console.error('Resolved page path goes outside of bundles path') | ||
if (!pagesManifest[page]) { | ||
throw pageNotFoundError(page) | ||
} | ||
|
||
return pagePath | ||
return join(serverBuildPath, pagesManifest[page]) | ||
} | ||
|
||
export default async function requirePage (page, {dir, dist}) { | ||
const pagePath = getPagePath(page, {dir, dist}) + '.js' | ||
const fileExists = await fs.exists(pagePath) | ||
if (!fileExists) { | ||
throw pageNotFoundError(page) | ||
} | ||
|
||
const pagePath = getPagePath(page, {dir, dist}) | ||
return require(pagePath) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
test: 'error' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"/index": "bundles/pages/index.js", | ||
"/world": "bundles/pages/world.js", | ||
"/_error": "bundles/pages/_error.js", | ||
"/non-existent-child": "bundles/pages/non-existent-child.js" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why two instances of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed