Commit 084919f3 authored by Tim Neutkens's avatar Tim Neutkens

Add loadable manifest per page

parent 821bedd9
...@@ -218,7 +218,39 @@ async function clientBundler({Bundler, dir, buildId, config}) { ...@@ -218,7 +218,39 @@ async function clientBundler({Bundler, dir, buildId, config}) {
// Run the bundler, this returns the main bundle // Run the bundler, this returns the main bundle
// Use the events if you're using watch mode as this promise will only trigger once and not for every rebuild // Use the events if you're using watch mode as this promise will only trigger once and not for every rebuild
const bundle = await bundler.bundle(); const mainBundle = await bundler.bundle()
const manifests = new Set()
function getDynamicBundlesForAsset(entryAsset, entryName) {
const modulesMapping = {}
for(const [dep, asset] of entryAsset.depAssets) {
if(!dep.dynamic) {
continue
}
if(!modulesMapping[dep.name]) {
modulesMapping[dep.name] = []
}
for(const bundle of asset.bundles) {
modulesMapping[dep.name].push(path.relative(path.dirname(entryName), bundle.name))
}
}
return modulesMapping
}
const writePromises = []
for(const [, entryBundle] of mainBundle.childBundles.entries()) {
if(!ROUTE_NAME_REGEX.exec(entryBundle.name)) {
continue
}
const dynamicBundles = getDynamicBundlesForAsset(entryBundle.entryAsset, entryBundle.name)
const manifestName = entryBundle.name.replace(options.outDir, path.join(dir, '.next', 'server')).replace(/\.js$/, '-loadable.json')
writePromises.push(writeFile(manifestName, JSON.stringify(dynamicBundles), 'utf8'))
}
await Promise.all(writePromises)
} }
async function serverBundler({Bundler, dir, buildId, config}) { async function serverBundler({Bundler, dir, buildId, config}) {
......
...@@ -25,7 +25,7 @@ import React from 'react' ...@@ -25,7 +25,7 @@ import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
const ALL_INITIALIZERS = [] const ALL_INITIALIZERS = []
const READY_INITIALIZERS = new Map() let READY_INITIALIZERS = {}
let initialized = false let initialized = false
function load (loader) { function load (loader) {
...@@ -136,10 +136,12 @@ function createLoadableComponent (loadFn, options) { ...@@ -136,10 +136,12 @@ function createLoadableComponent (loadFn, options) {
} }
// Client only // Client only
if (!initialized && typeof window !== 'undefined' && typeof opts.webpack === 'function') { if (!initialized && typeof window !== 'undefined' && opts.modules) {
const moduleIds = opts.webpack() for (const moduleId of opts.modules) {
for (const moduleId of moduleIds) { if(!READY_INITIALIZERS[moduleId]) {
READY_INITIALIZERS.set(moduleId, () => { READY_INITIALIZERS[moduleId] = []
}
READY_INITIALIZERS[moduleId].push(() => {
return init() return init()
}) })
} }
...@@ -297,18 +299,16 @@ Loadable.preloadAll = () => { ...@@ -297,18 +299,16 @@ Loadable.preloadAll = () => {
Loadable.preloadReady = (webpackIds) => { Loadable.preloadReady = (webpackIds) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const initializers = webpackIds.reduce((allInitalizers, moduleId) => { const initializers = webpackIds.reduce((allInitalizers, moduleId) => {
const initializer = READY_INITIALIZERS.get(moduleId) const initializer = READY_INITIALIZERS[moduleId]
if (!initializer) { if (!initializer) {
return allInitalizers return allInitalizers
} }
return allInitalizers.concat(initializer)
allInitalizers.push(initializer)
return allInitalizers
}, []) }, [])
initialized = true initialized = true
// Make sure the object is cleared // Make sure the object is cleared
READY_INITIALIZERS.clear() READY_INITIALIZERS = {}
// We always will resolve, errors should be handled within loading UIs. // We always will resolve, errors should be handled within loading UIs.
flushInitializers(initializers).then(resolve, resolve) flushInitializers(initializers).then(resolve, resolve)
......
...@@ -95,8 +95,7 @@ async function doRender (req, res, pathname, query, { ...@@ -95,8 +95,7 @@ async function doRender (req, res, pathname, query, {
App = App.default || App App = App.default || App
Component = Component.default || Component Component = Component.default || Component
// const reactLoadableManifest = require(pagePath + '-loadable.json') const reactLoadableManifest = require(pagePath + '-loadable.json')
// const buildManifest = require(pagePath + '-assets.json')
if (typeof Component !== 'function') { if (typeof Component !== 'function') {
throw new Error(`The default export is not a React Component in page: "${page}"`) throw new Error(`The default export is not a React Component in page: "${page}"`)
...@@ -106,7 +105,6 @@ async function doRender (req, res, pathname, query, { ...@@ -106,7 +105,6 @@ async function doRender (req, res, pathname, query, {
const ctx = { err, req, res, pathname, query, asPath } const ctx = { err, req, res, pathname, query, asPath }
const router = new Router(pathname, query, asPath) const router = new Router(pathname, query, asPath)
const props = await loadGetInitialProps(App, {Component, router, ctx}) const props = await loadGetInitialProps(App, {Component, router, ctx})
// const files = buildManifest.assets
const files = [`static/${buildId}/main.js`] const files = [`static/${buildId}/main.js`]
// the response might be finshed on the getinitialprops call // the response might be finshed on the getinitialprops call
...@@ -154,7 +152,7 @@ async function doRender (req, res, pathname, query, { ...@@ -154,7 +152,7 @@ async function doRender (req, res, pathname, query, {
head = Head.rewind() || defaultHead() head = Head.rewind() || defaultHead()
} }
return { html, head, /* buildManifest */ } return { html, head }
} }
await Loadable.preloadAll() // Make sure all dynamic imports are loaded await Loadable.preloadAll() // Make sure all dynamic imports are loaded
...@@ -162,7 +160,7 @@ async function doRender (req, res, pathname, query, { ...@@ -162,7 +160,7 @@ async function doRender (req, res, pathname, query, {
const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage }) const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage })
// const dynamicImports = [...(new Set(getDynamicImportBundles(reactLoadableManifest, reactLoadableModules)))] // const dynamicImports = [...(new Set(getDynamicImportBundles(reactLoadableManifest, reactLoadableModules)))]
const dynamicImports = [] const dynamicImports = []
const dynamicImportsIds = dynamicImports.map((bundle) => bundle.id) const dynamicImportsIds = reactLoadableModules
if (isResSent(res)) return if (isResSent(res)) return
......
...@@ -112,23 +112,6 @@ export default function ({ types: t, template }) { ...@@ -112,23 +112,6 @@ export default function ({ types: t, template }) {
t.objectProperty( t.objectProperty(
t.identifier('loadableGenerated'), t.identifier('loadableGenerated'),
t.objectExpression([ t.objectExpression([
t.objectProperty(
t.identifier('webpack'),
t.arrowFunctionExpression(
[],
t.arrayExpression(
dynamicImports.map(dynamicImport => {
return t.callExpression(
t.memberExpression(
t.identifier('require'),
t.identifier('resolveWeak')
),
[dynamicImport.get('arguments')[0].node]
)
})
)
)
),
t.objectProperty( t.objectProperty(
t.identifier('modules'), t.identifier('modules'),
t.arrayExpression( t.arrayExpression(
......
...@@ -46,7 +46,7 @@ module.exports = (context, opts = {}) => ({ ...@@ -46,7 +46,7 @@ module.exports = (context, opts = {}) => ({
plugins: [ plugins: [
require('babel-plugin-react-require'), require('babel-plugin-react-require'),
require('@babel/plugin-syntax-dynamic-import'), require('@babel/plugin-syntax-dynamic-import'),
// require('./plugins/react-loadable-plugin'), require('./plugins/react-loadable-plugin'),
require('./plugins/next-to-next-server'), require('./plugins/next-to-next-server'),
[require('@babel/plugin-proposal-class-properties'), opts['class-properties'] || {}], [require('@babel/plugin-proposal-class-properties'), opts['class-properties'] || {}],
require('@babel/plugin-proposal-object-rest-spread'), require('@babel/plugin-proposal-object-rest-spread'),
......
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