Unverified Commit 08baf526 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

improve react-loadable-plugin (#24281)

Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>



## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added


Closes: https://github.com/vercel/next.js/issues/22741
parent 710ad297
...@@ -29,6 +29,8 @@ import { ...@@ -29,6 +29,8 @@ import {
types as BabelTypes, types as BabelTypes,
} from 'next/dist/compiled/babel/core' } from 'next/dist/compiled/babel/core'
import { relative as relativePath } from 'path'
export default function ({ export default function ({
types: t, types: t,
}: { }: {
...@@ -36,7 +38,10 @@ export default function ({ ...@@ -36,7 +38,10 @@ export default function ({
}): PluginObj { }): PluginObj {
return { return {
visitor: { visitor: {
ImportDeclaration(path: NodePath<BabelTypes.ImportDeclaration>) { ImportDeclaration(
path: NodePath<BabelTypes.ImportDeclaration>,
state: any
) {
let source = path.node.source.value let source = path.node.source.value
if (source !== 'next/dynamic') return if (source !== 'next/dynamic') return
...@@ -133,7 +138,8 @@ export default function ({ ...@@ -133,7 +138,8 @@ export default function ({
if (!loader || Array.isArray(loader)) { if (!loader || Array.isArray(loader)) {
return return
} }
const dynamicImports: BabelTypes.StringLiteral[] = [] const dynamicImports: BabelTypes.Expression[] = []
const dynamicKeys: BabelTypes.Expression[] = []
loader.traverse({ loader.traverse({
Import(importPath) { Import(importPath) {
...@@ -141,6 +147,18 @@ export default function ({ ...@@ -141,6 +147,18 @@ export default function ({
if (!Array.isArray(importArguments)) return if (!Array.isArray(importArguments)) return
const node: any = importArguments[0].node const node: any = importArguments[0].node
dynamicImports.push(node) dynamicImports.push(node)
dynamicKeys.push(
t.binaryExpression(
'+',
t.stringLiteral(
relativePath(
state.file.opts.caller.pagesDir,
state.file.opts.filename
) + ' -> '
),
node
)
)
}, },
}) })
...@@ -169,7 +187,7 @@ export default function ({ ...@@ -169,7 +187,7 @@ export default function ({
), ),
t.objectProperty( t.objectProperty(
t.identifier('modules'), t.identifier('modules'),
t.arrayExpression(dynamicImports) t.arrayExpression(dynamicKeys)
), ),
]) ])
) )
......
...@@ -1141,6 +1141,7 @@ export default async function getBaseWebpackConfig( ...@@ -1141,6 +1141,7 @@ export default async function getBaseWebpackConfig(
!isServer && !isServer &&
new ReactLoadablePlugin({ new ReactLoadablePlugin({
filename: REACT_LOADABLE_MANIFEST, filename: REACT_LOADABLE_MANIFEST,
pagesDir,
}), }),
!isServer && new DropClientPage(), !isServer && new DropClientPage(),
// Moment.js is an extremely popular library that bundles large locale files // Moment.js is an extremely popular library that bundles large locale files
......
...@@ -102,6 +102,7 @@ const customBabelLoader = babelLoader((babel) => { ...@@ -102,6 +102,7 @@ const customBabelLoader = babelLoader((babel) => {
options.caller.isServer = isServer options.caller.isServer = isServer
options.caller.isDev = development options.caller.isDev = development
options.caller.hasJsxRuntime = hasJsxRuntime options.caller.hasJsxRuntime = hasJsxRuntime
options.caller.pagesDir = pagesDir
const emitWarning = this.emitWarning.bind(this) const emitWarning = this.emitWarning.bind(this)
Object.defineProperty(options.caller, 'onWarning', { Object.defineProperty(options.caller, 'onWarning', {
......
...@@ -27,69 +27,132 @@ import { ...@@ -27,69 +27,132 @@ import {
sources, sources,
} from 'next/dist/compiled/webpack/webpack' } from 'next/dist/compiled/webpack/webpack'
function getModulesIterable(compilation: any, chunk: any) { import path from 'path'
function getModuleId(compilation: any, module: any): string | number {
if (isWebpack5) { if (isWebpack5) {
return compilation.chunkGraph.getChunkModulesIterable(chunk) return compilation.chunkGraph.getModuleId(module)
} }
return chunk.modulesIterable return module.id
} }
function getModuleId(compilation: any, module: any) { function getModuleFromDependency(
compilation: any,
dep: any
): webpack.Module & { resource?: string } {
if (isWebpack5) { if (isWebpack5) {
return compilation.chunkGraph.getModuleId(module) return compilation.moduleGraph.getModule(dep)
} }
return module.id return dep.module
} }
function buildManifest( function getOriginModuleFromDependency(
_compiler: webpack.Compiler, compilation: any,
compilation: webpack.compilation.Compilation dep: any
) { ): webpack.Module & { resource?: string } {
let manifest: { [k: string]: any[] } = {} if (isWebpack5) {
return compilation.moduleGraph.getParentModule(dep)
}
compilation.chunkGroups.forEach((chunkGroup) => { return dep.originModule
if (chunkGroup.isInitial()) { }
return
}
chunkGroup.origins.forEach((chunkGroupOrigin: any) => { function getChunkGroupFromBlock(
const { request } = chunkGroupOrigin compilation: any,
block: any
chunkGroup.chunks.forEach((chunk: any) => { ): webpack.compilation.ChunkGroup {
chunk.files.forEach((file: string) => { if (isWebpack5) {
if ( return compilation.chunkGraph.getBlockChunkGroup(block)
!( }
(file.endsWith('.js') || file.endsWith('.css')) &&
file.match(/^static\/(chunks|css)\//)
)
) {
return
}
for (const module of getModulesIterable(compilation, chunk)) { return block.chunkGroup
let id = getModuleId(compilation, module) }
if (!manifest[request]) { function buildManifest(
manifest[request] = [] _compiler: webpack.Compiler,
} compilation: webpack.compilation.Compilation,
pagesDir: string
) {
let manifest: { [k: string]: { id: string | number; files: string[] } } = {}
// This is allowed:
// import("./module"); <- ImportDependency
// We don't support that:
// import(/* webpackMode: "eager" */ "./module") <- ImportEagerDependency
// import(`./module/${param}`) <- ImportContextDependency
// Find all dependencies blocks which contains a `import()` dependency
const handleBlock = (block: any) => {
block.blocks.forEach(handleBlock)
const chunkGroup = getChunkGroupFromBlock(compilation, block)
for (const dependency of block.dependencies) {
if (dependency.type.startsWith('import()')) {
// get the referenced module
const module = getModuleFromDependency(compilation, dependency)
if (!module) return
// get the module containing the import()
const originModule = getOriginModuleFromDependency(
compilation,
dependency
)
const originRequest: string | undefined = originModule?.resource
if (!originRequest) return
// We construct a "unique" key from origin module and request
// It's not perfect unique, but that will be fine for us.
// We also need to construct the same in the babel plugin.
const key = `${path.relative(pagesDir, originRequest)} -> ${
dependency.request
}`
// Capture all files that need to be loaded.
const files = new Set<string>()
if (manifest[key]) {
// In the "rare" case where multiple chunk groups
// are created for the same `import()` or multiple
// import()s reference the same module, we merge
// the files to make sure to not miss files
// This may cause overfetching in edge cases.
for (const file of manifest[key].files) {
files.add(file)
}
}
// There might not be a chunk group when all modules
// are already loaded. In this case we only need need
// the module id and no files
if (chunkGroup) {
for (const chunk of (chunkGroup as any)
.chunks as webpack.compilation.Chunk[]) {
chunk.files.forEach((file: string) => {
if (
(file.endsWith('.js') || file.endsWith('.css')) &&
file.match(/^static\/(chunks|css)\//)
) {
files.add(file)
}
})
}
}
// Avoid duplicate files // usually we have to add the parent chunk groups too
if ( // but we assume that all parents are also imported by
manifest[request].some( // next/dynamic so they are loaded by the same technique
(item) => item.id === id && item.file === file
)
) {
continue
}
manifest[request].push({ id, file }) // add the id and files to the manifest
} const id = getModuleId(compilation, module)
}) manifest[key] = { id, files: Array.from(files) }
}) }
}) }
}) }
for (const module of compilation.modules) {
module.blocks.forEach(handleBlock)
}
manifest = Object.keys(manifest) manifest = Object.keys(manifest)
.sort() .sort()
...@@ -101,13 +164,15 @@ function buildManifest( ...@@ -101,13 +164,15 @@ function buildManifest(
export class ReactLoadablePlugin { export class ReactLoadablePlugin {
private filename: string private filename: string
private pagesDir: string
constructor(opts: { filename: string }) { constructor(opts: { filename: string; pagesDir: string }) {
this.filename = opts.filename this.filename = opts.filename
this.pagesDir = opts.pagesDir
} }
createAssets(compiler: any, compilation: any, assets: any) { createAssets(compiler: any, compilation: any, assets: any) {
const manifest = buildManifest(compiler, compilation) const manifest = buildManifest(compiler, compilation, this.pagesDir)
// @ts-ignore: TODO: remove when webpack 5 is stable // @ts-ignore: TODO: remove when webpack 5 is stable
assets[this.filename] = new sources.RawSource( assets[this.filename] = new sources.RawSource(
JSON.stringify(manifest, null, 2) JSON.stringify(manifest, null, 2)
......
...@@ -39,7 +39,7 @@ declare global { ...@@ -39,7 +39,7 @@ declare global {
__NEXT_HYDRATED_CB?: () => void __NEXT_HYDRATED_CB?: () => void
/* prod */ /* prod */
__NEXT_PRELOADREADY?: (ids?: string[]) => void __NEXT_PRELOADREADY?: (ids?: (string | number)[]) => void
__NEXT_DATA__: NEXT_DATA __NEXT_DATA__: NEXT_DATA
__NEXT_P: any[] __NEXT_P: any[]
} }
......
...@@ -3,7 +3,6 @@ import { ParsedUrlQuery } from 'querystring' ...@@ -3,7 +3,6 @@ import { ParsedUrlQuery } from 'querystring'
import { ComponentType } from 'react' import { ComponentType } from 'react'
import { UrlObject } from 'url' import { UrlObject } from 'url'
import { formatUrl } from './router/utils/format-url' import { formatUrl } from './router/utils/format-url'
import { ManifestItem } from '../server/load-components'
import { NextRouter } from './router/router' import { NextRouter } from './router/router'
import { Env } from '@next/env' import { Env } from '@next/env'
import { BuildManifest } from '../server/get-page-files' import { BuildManifest } from '../server/get-page-files'
...@@ -93,7 +92,7 @@ export type NEXT_DATA = { ...@@ -93,7 +92,7 @@ export type NEXT_DATA = {
nextExport?: boolean nextExport?: boolean
autoExport?: boolean autoExport?: boolean
isFallback?: boolean isFallback?: boolean
dynamicIds?: string[] dynamicIds?: (string | number)[]
err?: Error & { statusCode?: number } err?: Error & { statusCode?: number }
gsp?: boolean gsp?: boolean
gssp?: boolean gssp?: boolean
...@@ -185,7 +184,7 @@ export type DocumentProps = DocumentInitialProps & { ...@@ -185,7 +184,7 @@ export type DocumentProps = DocumentInitialProps & {
inAmpMode: boolean inAmpMode: boolean
hybridAmp: boolean hybridAmp: boolean
isDevelopment: boolean isDevelopment: boolean
dynamicImports: ManifestItem[] dynamicImports: string[]
assetPrefix?: string assetPrefix?: string
canonicalBase: string canonicalBase: string
headTags: any[] headTags: any[]
......
...@@ -16,11 +16,10 @@ export function interopDefault(mod: any) { ...@@ -16,11 +16,10 @@ export function interopDefault(mod: any) {
export type ManifestItem = { export type ManifestItem = {
id: number | string id: number | string
name: string files: string[]
file: string
} }
type ReactLoadableManifest = { [moduleId: string]: ManifestItem[] } type ReactLoadableManifest = { [moduleId: string]: ManifestItem }
export type LoadComponentsReturnType = { export type LoadComponentsReturnType = {
Component: React.ComponentType Component: React.ComponentType
......
...@@ -245,8 +245,8 @@ function renderDocument( ...@@ -245,8 +245,8 @@ function renderDocument(
ampPath: string ampPath: string
inAmpMode: boolean inAmpMode: boolean
hybridAmp: boolean hybridAmp: boolean
dynamicImportsIds: string[] dynamicImportsIds: (string | number)[]
dynamicImports: ManifestItem[] dynamicImports: string[]
headTags: any headTags: any
isFallback?: boolean isFallback?: boolean
gsp?: boolean gsp?: boolean
...@@ -1023,21 +1023,20 @@ export async function renderToHTML( ...@@ -1023,21 +1023,20 @@ export async function renderToHTML(
throw new Error(message) throw new Error(message)
} }
const dynamicImportIdsSet = new Set<string>() const dynamicImportsIds = new Set<string | number>()
const dynamicImports: ManifestItem[] = [] const dynamicImports = new Set<string>()
for (const mod of reactLoadableModules) { for (const mod of reactLoadableModules) {
const manifestItem: ManifestItem[] = reactLoadableManifest[mod] const manifestItem: ManifestItem = reactLoadableManifest[mod]
if (manifestItem) { if (manifestItem) {
manifestItem.forEach((item) => { dynamicImportsIds.add(manifestItem.id)
dynamicImports.push(item) manifestItem.files.forEach((item) => {
dynamicImportIdsSet.add(item.id as string) dynamicImports.add(item)
}) })
} }
} }
const dynamicImportsIds = [...dynamicImportIdsSet]
const hybridAmp = ampState.hybrid const hybridAmp = ampState.hybrid
const docComponentsRendered: DocumentProps['docComponentsRendered'] = {} const docComponentsRendered: DocumentProps['docComponentsRendered'] = {}
...@@ -1069,8 +1068,8 @@ export async function renderToHTML( ...@@ -1069,8 +1068,8 @@ export async function renderToHTML(
query, query,
inAmpMode, inAmpMode,
hybridAmp, hybridAmp,
dynamicImportsIds, dynamicImportsIds: Array.from(dynamicImportsIds),
dynamicImports, dynamicImports: Array.from(dynamicImports),
gsp: !!getStaticProps ? true : undefined, gsp: !!getStaticProps ? true : undefined,
gssp: !!getServerSideProps ? true : undefined, gssp: !!getServerSideProps ? true : undefined,
gip: hasPageGetInitialProps ? true : undefined, gip: hasPageGetInitialProps ? true : undefined,
......
...@@ -28,18 +28,6 @@ export type OriginProps = { ...@@ -28,18 +28,6 @@ export type OriginProps = {
crossOrigin?: string crossOrigin?: string
} }
function dedupe<T extends { file: string }>(bundles: T[]): T[] {
const files = new Set<string>()
const kept: T[] = []
for (const bundle of bundles) {
if (files.has(bundle.file)) continue
files.add(bundle.file)
kept.push(bundle)
}
return kept
}
type DocumentFiles = { type DocumentFiles = {
sharedFiles: readonly string[] sharedFiles: readonly string[]
pageFiles: readonly string[] pageFiles: readonly string[]
...@@ -167,9 +155,9 @@ export class Head extends Component< ...@@ -167,9 +155,9 @@ export class Head extends Component<
// Unmanaged files are CSS files that will be handled directly by the // Unmanaged files are CSS files that will be handled directly by the
// webpack runtime (`mini-css-extract-plugin`). // webpack runtime (`mini-css-extract-plugin`).
let unmangedFiles: Set<string> = new Set([]) let unmangedFiles: Set<string> = new Set([])
let dynamicCssFiles = dedupe( let dynamicCssFiles = Array.from(
dynamicImports.filter((f) => f.file.endsWith('.css')) new Set(dynamicImports.filter((file) => file.endsWith('.css')))
).map((f) => f.file) )
if (dynamicCssFiles.length) { if (dynamicCssFiles.length) {
const existing = new Set(cssFiles) const existing = new Set(cssFiles)
dynamicCssFiles = dynamicCssFiles.filter( dynamicCssFiles = dynamicCssFiles.filter(
...@@ -238,18 +226,18 @@ export class Head extends Component< ...@@ -238,18 +226,18 @@ export class Head extends Component<
} = this.context } = this.context
return ( return (
dedupe(dynamicImports) dynamicImports
.map((bundle) => { .map((file) => {
if (!bundle.file.endsWith('.js')) { if (!file.endsWith('.js')) {
return null return null
} }
return ( return (
<link <link
rel="preload" rel="preload"
key={bundle.file} key={file}
href={`${assetPrefix}/_next/${encodeURI( href={`${assetPrefix}/_next/${encodeURI(
bundle.file file
)}${devOnlyCacheBusterQueryString}`} )}${devOnlyCacheBusterQueryString}`}
as="script" as="script"
nonce={this.props.nonce} nonce={this.props.nonce}
...@@ -661,16 +649,15 @@ export class NextScript extends Component<OriginProps> { ...@@ -661,16 +649,15 @@ export class NextScript extends Component<OriginProps> {
devOnlyCacheBusterQueryString, devOnlyCacheBusterQueryString,
} = this.context } = this.context
return dedupe(dynamicImports).map((bundle) => { return dynamicImports.map((file) => {
if (!bundle.file.endsWith('.js') || files.allFiles.includes(bundle.file)) if (!file.endsWith('.js') || files.allFiles.includes(file)) return null
return null
return ( return (
<script <script
async={!isDevelopment} async={!isDevelopment}
key={bundle.file} key={file}
src={`${assetPrefix}/_next/${encodeURI( src={`${assetPrefix}/_next/${encodeURI(
bundle.file file
)}${devOnlyCacheBusterQueryString}`} )}${devOnlyCacheBusterQueryString}`}
nonce={this.props.nonce} nonce={this.props.nonce}
crossOrigin={ crossOrigin={
......
...@@ -337,10 +337,14 @@ export default function (render, fetch, ctx) { ...@@ -337,10 +337,14 @@ export default function (render, fetch, ctx) {
)) ))
const resources = [] const resources = []
const manifestKey = Object.keys(reactLoadableManifest).find((item) => {
return item
.replace(/\\/g, '/')
.endsWith('ssr.js -> ../../components/hello1')
})
// test dynamic chunk // test dynamic chunk
resources.push( resources.push('/_next/' + reactLoadableManifest[manifestKey].files[0])
'/_next/' + reactLoadableManifest['../../components/hello1'][0].file
)
// test main.js runtime etc // test main.js runtime etc
for (const item of buildManifest.pages['/dynamic/ssr']) { for (const item of buildManifest.pages['/dynamic/ssr']) {
......
export default () => {
return '4'
}
import dynamic from 'next/dynamic' import dynamic from 'next/dynamic'
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
import FourDirect from '../components/four'
const One = dynamic(() => import('../components/one')) const One = dynamic(() => import('../components/one'))
const Two = dynamic(() => import('../components/two')) const Two = dynamic(() => import('../components/two'))
const Three = dynamic(() => import('../components/three')) const Three = dynamic(() => import('../components/three'))
const Four = dynamic(() => import('../components/four'))
if (typeof window !== 'undefined') {
window.caughtErrors = ''
const origError = console.error
console.error = function (...args) {
window.caughtErrors += args.join(' ')
origError(...args)
}
}
export default () => { export default () => {
const [firstRender, setFirstRender] = useState('the-server-value') const [firstRender, setFirstRender] = useState('the-server-value')
...@@ -18,6 +30,8 @@ export default () => { ...@@ -18,6 +30,8 @@ export default () => {
<One /> <One />
<Two /> <Two />
<Three /> <Three />
<Four />
<FourDirect />
</div> </div>
<div id="first-render">{firstRender}</div> <div id="first-render">{firstRender}</div>
</> </>
......
...@@ -31,7 +31,8 @@ function runTests() { ...@@ -31,7 +31,8 @@ function runTests() {
const text = await browser.elementByCss('#first-render').text() const text = await browser.elementByCss('#first-render').text()
// Failure case is 'Index<!-- -->3<!-- --><!-- -->' // Failure case is 'Index<!-- -->3<!-- --><!-- -->'
expect(text).toBe('Index<!-- -->1<!-- -->2<!-- -->3') expect(text).toBe('Index<!-- -->1<!-- -->2<!-- -->3<!-- -->4<!-- -->4')
expect(await browser.eval('window.caughtErrors')).toBe('')
}) })
} }
......
...@@ -251,10 +251,14 @@ describe('Production Usage', () => { ...@@ -251,10 +251,14 @@ describe('Production Usage', () => {
const resources = new Set() const resources = new Set()
const manifestKey = Object.keys(reactLoadableManifest).find((item) => {
return item
.replace(/\\/g, '/')
.endsWith('bundle.js -> ../../components/hello1')
})
// test dynamic chunk // test dynamic chunk
resources.add( resources.add(url + reactLoadableManifest[manifestKey].files[0])
url + reactLoadableManifest['../../components/hello1'][0].file
)
// test main.js runtime etc // test main.js runtime etc
for (const item of buildManifest.pages['/']) { for (const item of buildManifest.pages['/']) {
......
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