Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
084919f3
Commit
084919f3
authored
Oct 22, 2018
by
Tim Neutkens
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add loadable manifest per page
parent
821bedd9
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
47 additions
and
34 deletions
+47
-34
index.js
packages/next-build/index.js
+33
-1
loadable.js
packages/next-server/lib/loadable.js
+10
-10
render.js
packages/next-server/server/render.js
+3
-5
react-loadable-plugin.js
packages/next/build/babel/plugins/react-loadable-plugin.js
+0
-17
preset.js
packages/next/build/babel/preset.js
+1
-1
No files found.
packages/next-build/index.js
View file @
084919f3
...
...
@@ -218,7 +218,39 @@ async function clientBundler({Bundler, dir, buildId, config}) {
// 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
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
})
{
...
...
packages/next-server/lib/loadable.js
View file @
084919f3
...
...
@@ -25,7 +25,7 @@ import React from 'react'
import
PropTypes
from
'prop-types'
const
ALL_INITIALIZERS
=
[]
const
READY_INITIALIZERS
=
new
Map
()
let
READY_INITIALIZERS
=
{}
let
initialized
=
false
function
load
(
loader
)
{
...
...
@@ -136,10 +136,12 @@ function createLoadableComponent (loadFn, options) {
}
// Client only
if
(
!
initialized
&&
typeof
window
!==
'undefined'
&&
typeof
opts
.
webpack
===
'function'
)
{
const
moduleIds
=
opts
.
webpack
()
for
(
const
moduleId
of
moduleIds
)
{
READY_INITIALIZERS
.
set
(
moduleId
,
()
=>
{
if
(
!
initialized
&&
typeof
window
!==
'undefined'
&&
opts
.
modules
)
{
for
(
const
moduleId
of
opts
.
modules
)
{
if
(
!
READY_INITIALIZERS
[
moduleId
])
{
READY_INITIALIZERS
[
moduleId
]
=
[]
}
READY_INITIALIZERS
[
moduleId
].
push
(()
=>
{
return
init
()
})
}
...
...
@@ -297,18 +299,16 @@ Loadable.preloadAll = () => {
Loadable
.
preloadReady
=
(
webpackIds
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
initializers
=
webpackIds
.
reduce
((
allInitalizers
,
moduleId
)
=>
{
const
initializer
=
READY_INITIALIZERS
.
get
(
moduleId
)
const
initializer
=
READY_INITIALIZERS
[
moduleId
]
if
(
!
initializer
)
{
return
allInitalizers
}
allInitalizers
.
push
(
initializer
)
return
allInitalizers
return
allInitalizers
.
concat
(
initializer
)
},
[])
initialized
=
true
// Make sure the object is cleared
READY_INITIALIZERS
.
clear
()
READY_INITIALIZERS
=
{}
// We always will resolve, errors should be handled within loading UIs.
flushInitializers
(
initializers
).
then
(
resolve
,
resolve
)
...
...
packages/next-server/server/render.js
View file @
084919f3
...
...
@@ -95,8 +95,7 @@ async function doRender (req, res, pathname, query, {
App
=
App
.
default
||
App
Component
=
Component
.
default
||
Component
// const reactLoadableManifest = require(pagePath + '-loadable.json')
// const buildManifest = require(pagePath + '-assets.json')
const
reactLoadableManifest
=
require
(
pagePath
+
'-loadable.json'
)
if
(
typeof
Component
!==
'function'
)
{
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, {
const
ctx
=
{
err
,
req
,
res
,
pathname
,
query
,
asPath
}
const
router
=
new
Router
(
pathname
,
query
,
asPath
)
const
props
=
await
loadGetInitialProps
(
App
,
{
Component
,
router
,
ctx
})
// const files = buildManifest.assets
const
files
=
[
`static/
${
buildId
}
/main.js`
]
// the response might be finshed on the getinitialprops call
...
...
@@ -154,7 +152,7 @@ async function doRender (req, res, pathname, query, {
head
=
Head
.
rewind
()
||
defaultHead
()
}
return
{
html
,
head
,
/* buildManifest */
}
return
{
html
,
head
}
}
await
Loadable
.
preloadAll
()
// Make sure all dynamic imports are loaded
...
...
@@ -162,7 +160,7 @@ async function doRender (req, res, pathname, query, {
const
docProps
=
await
loadGetInitialProps
(
Document
,
{
...
ctx
,
renderPage
})
// const dynamicImports = [...(new Set(getDynamicImportBundles(reactLoadableManifest, reactLoadableModules)))]
const
dynamicImports
=
[]
const
dynamicImportsIds
=
dynamicImports
.
map
((
bundle
)
=>
bundle
.
id
)
const
dynamicImportsIds
=
reactLoadableModules
if
(
isResSent
(
res
))
return
...
...
packages/next/build/babel/plugins/react-loadable-plugin.js
View file @
084919f3
...
...
@@ -112,23 +112,6 @@ export default function ({ types: t, template }) {
t
.
objectProperty
(
t
.
identifier
(
'loadableGenerated'
),
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
.
identifier
(
'modules'
),
t
.
arrayExpression
(
...
...
packages/next/build/babel/preset.js
View file @
084919f3
...
...
@@ -46,7 +46,7 @@ module.exports = (context, opts = {}) => ({
plugins
:
[
require
(
'babel-plugin-react-require'
),
require
(
'@babel/plugin-syntax-dynamic-import'
),
//
require('./plugins/react-loadable-plugin'),
require
(
'./plugins/react-loadable-plugin'
),
require
(
'./plugins/next-to-next-server'
),
[
require
(
'@babel/plugin-proposal-class-properties'
),
opts
[
'class-properties'
]
||
{}],
require
(
'@babel/plugin-proposal-object-rest-spread'
),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment