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'
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
import ChunkNamesPlugin from './webpack/plugins/chunk-names-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 AutoDllPlugin from 'autodll-webpack-plugin'
import TerserPlugin from 'terser-webpack-plugin'
......@@ -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
new ChunkNamesPlugin(),
!isServer && new ReactLoadablePlugin({
filename: REACT_LOADABLE_MANIFEST
}),
!isServer && new ReactLoadablePlugin(),
new WebpackBar({
name: isServer ? 'server' : 'client'
}),
......
......@@ -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
import url from 'url'
import path from 'path'
import {IS_BUNDLED_PAGE_REGEX} from 'next-server/constants'
function buildManifest (compiler, compilation) {
let context = compiler.options.context
let manifest = {}
export class ReactLoadablePlugin {
apply (compiler) {
compiler.hooks.emit.tapPromise('ReactLoadableManifest', async (compilation) => {
const context = compiler.options.context
const manifest = {}
compilation.chunks.forEach(chunk => {
chunk.files.forEach(file => {
for (const module of chunk.modulesIterable) {
let id = module.id
let name = typeof module.libIdent === 'function' ? module.libIdent({ context }) : null
// 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)
for (const chunk of compilation.chunks) {
for (const file of chunk.files) {
// If it doesn't end in `.js` Next.js can't handle it right now.
if (!file.match(/\.js$/) || !file.match(/^static\/chunks\//)) {
continue
}
let currentModule = module
if (module.constructor.name === 'ConcatenatedModule') {
currentModule = module.rootModule
}
if (!manifest[currentModule.rawRequest]) {
manifest[currentModule.rawRequest] = []
}
for (const module of chunk.modulesIterable) {
const id = module.id
const name = typeof module.libIdent === 'function' ? module.libIdent({ context }) : null
const publicPath = url.resolve(compilation.outputOptions.publicPath || '', file)
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 {
constructor (opts = {}) {
this.filename = opts.filename
}
for (const [, entrypoint] of compilation.entrypoints.entries()) {
if (!IS_BUNDLED_PAGE_REGEX.exec(entrypoint.name)) {
continue
}
apply (compiler) {
compiler.hooks.emit.tapAsync('ReactLoadableManifest', (compilation, callback) => {
const manifest = buildManifest(compiler, compilation)
var json = JSON.stringify(manifest, null, 2)
compilation.assets[this.filename] = {
source () {
return json
},
size () {
return json.length
const manifestFile = path.join('server', entrypoint.name.replace(/\.js$/, '-loadable.json'))
compilation.assets[manifestFile] = {
source () {
return json
},
size () {
return json.length
}
}
}
callback()
})
}
}
......@@ -2,27 +2,34 @@ import path from 'path'
import promisify from '../../lib/promisify'
import globModule from 'glob'
import {CLIENT_STATIC_FILES_PATH} from 'next-server/constants'
import {normalizePagePath} from 'next-server/dist/server/require'
const glob = promisify(globModule)
export async function getPages (dir, {nextPagesDir, dev, buildId, isServer, pageExtensions}) {
const pageFiles = await getPagePaths(dir, {dev, isServer, pageExtensions})
export async function getPages (dir, {pages, nextPagesDir, dev, buildId, isServer, pageExtensions}) {
const pageFiles = await getPagePaths(dir, {pages, dev, isServer, pageExtensions})
return getPageEntries(pageFiles, {nextPagesDir, buildId, isServer, pageExtensions})
}
export async function getPagePaths (dir, {dev, isServer, pageExtensions}) {
let pages
if (dev) {
// 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
pages = await glob(isServer ? `pages/+(_document|_app|_error).+(${pageExtensions})` : `pages/+(_app|_error).+(${pageExtensions})`, { cwd: dir })
} else {
// In production get all pages from the pages directory
pages = await glob(isServer ? `pages/**/*.+(${pageExtensions})` : `pages/**/!(_document)*.+(${pageExtensions})`, { cwd: dir })
export async function getPagePaths (dir, {pages, dev, isServer, pageExtensions}) {
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
const defaultPages = await glob(isServer ? `pages/+(_document|_app|_error).+(${pageExtensions})` : `pages/+(_app|_error).+(${pageExtensions})`, { cwd: dir })
const resolvedFiles = await Promise.all((pages || []).map(async (file) => {
const normalizedPagePath = normalizePagePath(file)
const paths = await glob(`pages/{${normalizedPagePath}/index,${normalizedPagePath}}.+(${pageExtensions})`, {cwd: dir})
if (paths.length === 0) {
return null
}
return paths[0]
}))
return [...new Set([...defaultPages, ...resolvedFiles.filter(f => f !== null)])]
}
return pages
// In production when no page files are provided get all pages from the pages directory
return glob(isServer ? `pages/**/*.+(${pageExtensions})` : `pages/**/!(_document)*.+(${pageExtensions})`, { cwd: dir })
}
// 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