Commit 38d315f6 authored by Tim Neutkens's avatar Tim Neutkens

Change loadable to export per-page

parent 7a3b9685
...@@ -17,7 +17,7 @@ import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin' ...@@ -17,7 +17,7 @@ import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin'
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin' import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin' import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin'
import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin' import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin'
import {SERVER_DIRECTORY, REACT_LOADABLE_MANIFEST, CLIENT_STATIC_FILES_RUNTIME_WEBPACK, CLIENT_STATIC_FILES_RUNTIME_MAIN} from 'next-server/constants' import {SERVER_DIRECTORY, CLIENT_STATIC_FILES_RUNTIME_WEBPACK, CLIENT_STATIC_FILES_RUNTIME_MAIN} from 'next-server/constants'
import {NEXT_PROJECT_ROOT, NEXT_PROJECT_ROOT_NODE_MODULES, NEXT_PROJECT_ROOT_DIST, DEFAULT_PAGES_DIR} from '../lib/constants' import {NEXT_PROJECT_ROOT, NEXT_PROJECT_ROOT_NODE_MODULES, NEXT_PROJECT_ROOT_DIST, DEFAULT_PAGES_DIR} from '../lib/constants'
import AutoDllPlugin from 'autodll-webpack-plugin' import AutoDllPlugin from 'autodll-webpack-plugin'
import TerserPlugin from 'terser-webpack-plugin' import TerserPlugin from 'terser-webpack-plugin'
...@@ -251,9 +251,7 @@ export default async function getBaseWebpackConfig (dir: string, {dev = false, i ...@@ -251,9 +251,7 @@ export default async function getBaseWebpackConfig (dir: string, {dev = false, i
}), }),
// This plugin makes sure `output.filename` is used for entry chunks // This plugin makes sure `output.filename` is used for entry chunks
new ChunkNamesPlugin(), new ChunkNamesPlugin(),
!isServer && new ReactLoadablePlugin({ !isServer && new ReactLoadablePlugin(),
filename: REACT_LOADABLE_MANIFEST
}),
new WebpackBar({ new WebpackBar({
name: isServer ? 'server' : 'client' name: isServer ? 'server' : 'client'
}), }),
......
...@@ -22,56 +22,58 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR ...@@ -22,56 +22,58 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
// Modified to strip out unneeded results for Next's specific use case // Modified to strip out unneeded results for Next's specific use case
import url from 'url' import url from 'url'
import path from 'path'
import {IS_BUNDLED_PAGE_REGEX} from 'next-server/constants'
function buildManifest (compiler, compilation) { export class ReactLoadablePlugin {
let context = compiler.options.context apply (compiler) {
let manifest = {} compiler.hooks.emit.tapPromise('ReactLoadableManifest', async (compilation) => {
const context = compiler.options.context
const manifest = {}
compilation.chunks.forEach(chunk => { for (const chunk of compilation.chunks) {
chunk.files.forEach(file => { for (const file of chunk.files) {
for (const module of chunk.modulesIterable) { // If it doesn't end in `.js` Next.js can't handle it right now.
let id = module.id if (!file.match(/\.js$/) || !file.match(/^static\/chunks\//)) {
let name = typeof module.libIdent === 'function' ? module.libIdent({ context }) : null continue
// If it doesn't end in `.js` Next.js can't handle it right now. }
if (!file.match(/\.js$/) || !file.match(/^static\/chunks\//)) {
return
}
let publicPath = url.resolve(compilation.outputOptions.publicPath || '', file)
let currentModule = module for (const module of chunk.modulesIterable) {
if (module.constructor.name === 'ConcatenatedModule') { const id = module.id
currentModule = module.rootModule const name = typeof module.libIdent === 'function' ? module.libIdent({ context }) : null
} const publicPath = url.resolve(compilation.outputOptions.publicPath || '', file)
if (!manifest[currentModule.rawRequest]) {
manifest[currentModule.rawRequest] = [] let currentModule = module
} if (module.constructor.name === 'ConcatenatedModule') {
currentModule = module.rootModule
}
if (!manifest[currentModule.rawRequest]) {
manifest[currentModule.rawRequest] = []
}
manifest[currentModule.rawRequest].push({ id, name, file, publicPath }) manifest[currentModule.rawRequest].push({ id, name, file, publicPath })
}
}
} }
})
})
return manifest const json = JSON.stringify(manifest)
}
export class ReactLoadablePlugin { for (const [, entrypoint] of compilation.entrypoints.entries()) {
constructor (opts = {}) { if (!IS_BUNDLED_PAGE_REGEX.exec(entrypoint.name)) {
this.filename = opts.filename continue
} }
apply (compiler) { const manifestFile = path.join('server', entrypoint.name.replace(/\.js$/, '-loadable.json'))
compiler.hooks.emit.tapAsync('ReactLoadableManifest', (compilation, callback) => {
const manifest = buildManifest(compiler, compilation) compilation.assets[manifestFile] = {
var json = JSON.stringify(manifest, null, 2) source () {
compilation.assets[this.filename] = { return json
source () { },
return json size () {
}, return json.length
size () { }
return json.length
} }
} }
callback()
}) })
} }
} }
...@@ -2,27 +2,34 @@ import path from 'path' ...@@ -2,27 +2,34 @@ import path from 'path'
import promisify from '../../lib/promisify' import promisify from '../../lib/promisify'
import globModule from 'glob' import globModule from 'glob'
import {CLIENT_STATIC_FILES_PATH} from 'next-server/constants' import {CLIENT_STATIC_FILES_PATH} from 'next-server/constants'
import {normalizePagePath} from 'next-server/dist/server/require'
const glob = promisify(globModule) const glob = promisify(globModule)
export async function getPages (dir, {nextPagesDir, dev, buildId, isServer, pageExtensions}) { export async function getPages (dir, {pages, nextPagesDir, dev, buildId, isServer, pageExtensions}) {
const pageFiles = await getPagePaths(dir, {dev, isServer, pageExtensions}) const pageFiles = await getPagePaths(dir, {pages, dev, isServer, pageExtensions})
return getPageEntries(pageFiles, {nextPagesDir, buildId, isServer, pageExtensions}) return getPageEntries(pageFiles, {nextPagesDir, buildId, isServer, pageExtensions})
} }
export async function getPagePaths (dir, {dev, isServer, pageExtensions}) { export async function getPagePaths (dir, {pages, dev, isServer, pageExtensions}) {
let pages if (dev || pages) {
// In development or when page files are provided we only compile _document.js, _error.js and _app.js when starting, since they're always needed. All other pages are compiled with on demand entries
if (dev) { const defaultPages = await glob(isServer ? `pages/+(_document|_app|_error).+(${pageExtensions})` : `pages/+(_app|_error).+(${pageExtensions})`, { cwd: dir })
// In development we only compile _document.js, _error.js and _app.js when starting, since they're always needed. All other pages are compiled with on demand entries const resolvedFiles = await Promise.all((pages || []).map(async (file) => {
pages = await glob(isServer ? `pages/+(_document|_app|_error).+(${pageExtensions})` : `pages/+(_app|_error).+(${pageExtensions})`, { cwd: dir }) const normalizedPagePath = normalizePagePath(file)
} else { const paths = await glob(`pages/{${normalizedPagePath}/index,${normalizedPagePath}}.+(${pageExtensions})`, {cwd: dir})
// In production get all pages from the pages directory if (paths.length === 0) {
pages = await glob(isServer ? `pages/**/*.+(${pageExtensions})` : `pages/**/!(_document)*.+(${pageExtensions})`, { cwd: dir }) return null
}
return paths[0]
}))
return [...new Set([...defaultPages, ...resolvedFiles.filter(f => f !== null)])]
} }
// In production when no page files are provided get all pages from the pages directory
return pages return glob(isServer ? `pages/**/*.+(${pageExtensions})` : `pages/**/!(_document)*.+(${pageExtensions})`, { cwd: dir })
} }
// Convert page path into single entry // Convert page path into single entry
......
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