Unverified Commit 210a6980 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Solve large portion of webpack 5 deprecation warnings (#15806)

- Using `namedChunks` where possible, this will also allow for faster access to the chunks as we no longer have to look them up like we did before using `find`
- Using the new asset hooks introduced in the latest webpack beta
- Using the new externals function signature
parent ed0a47b1
This diff is collapsed.
import { Compiler, Plugin } from 'webpack' import webpack, { Compiler, Plugin } from 'webpack'
import { RawSource } from 'webpack-sources' import { RawSource } from 'webpack-sources'
import { PAGES_MANIFEST } from '../../../next-server/lib/constants' import { PAGES_MANIFEST } from '../../../next-server/lib/constants'
import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-entrypoint' import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-entrypoint'
export type PagesManifest = { [page: string]: string } export type PagesManifest = { [page: string]: string }
const isWebpack5 = parseInt(webpack.version!) === 5
// This plugin creates a pages-manifest.json from page entrypoints. // This plugin creates a pages-manifest.json from page entrypoints.
// This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR // This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR
// It's also used by next export to provide defaultPathMap // It's also used by next export to provide defaultPathMap
...@@ -15,43 +17,59 @@ export default class PagesManifestPlugin implements Plugin { ...@@ -15,43 +17,59 @@ export default class PagesManifestPlugin implements Plugin {
this.serverless = serverless this.serverless = serverless
} }
apply(compiler: Compiler): void { createAssets(compilation: any, assets: any) {
compiler.hooks.emit.tap('NextJsPagesManifest', (compilation) => { const entrypoints = compilation.entrypoints
const entrypoints = compilation.entrypoints const pages: PagesManifest = {}
const pages: PagesManifest = {}
for (const entrypoint of entrypoints.values()) {
for (const entrypoint of entrypoints.values()) { const pagePath = getRouteFromEntrypoint(entrypoint.name, this.serverless)
const pagePath = getRouteFromEntrypoint(
entrypoint.name, if (!pagePath) {
this.serverless continue
}
const files = entrypoint
.getFiles()
.filter(
(file: string) =>
!file.includes('webpack-runtime') && file.endsWith('.js')
) )
if (!pagePath) { if (files.length > 1) {
continue console.log(
} `Found more than one file in server entrypoint ${entrypoint.name}`,
files
const files = entrypoint )
.getFiles() continue
.filter(
(file: string) =>
!file.includes('webpack-runtime') && file.endsWith('.js')
)
if (files.length > 1) {
console.log(
`Found more than one file in server entrypoint ${entrypoint.name}`,
files
)
continue
}
// Write filename, replace any backslashes in path (on windows) with forwardslashes for cross-platform consistency.
pages[pagePath] = files[0].replace(/\\/g, '/')
} }
compilation.assets[PAGES_MANIFEST] = new RawSource( // Write filename, replace any backslashes in path (on windows) with forwardslashes for cross-platform consistency.
JSON.stringify(pages, null, 2) pages[pagePath] = files[0].replace(/\\/g, '/')
) }
assets[PAGES_MANIFEST] = new RawSource(JSON.stringify(pages, null, 2))
}
apply(compiler: Compiler): void {
if (isWebpack5) {
compiler.hooks.make.tap('NextJsPagesManifest', (compilation) => {
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
compilation.hooks.processAssets.tap(
{
name: 'NextJsPagesManifest',
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
(assets: any) => {
this.createAssets(compilation, assets)
}
)
})
return
}
compiler.hooks.emit.tap('NextJsPagesManifest', (compilation: any) => {
this.createAssets(compilation, compilation.assets)
}) })
} }
} }
...@@ -21,12 +21,30 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR ...@@ -21,12 +21,30 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
// Implementation of this PR: https://github.com/jamiebuilds/react-loadable/pull/132 // Implementation of this PR: https://github.com/jamiebuilds/react-loadable/pull/132
// 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 { import webpack, {
Compiler, Compiler,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
compilation as CompilationType, compilation as CompilationType,
} from 'webpack' } from 'webpack'
const isWebpack5 = parseInt(webpack.version!) === 5
function getModulesIterable(compilation: any, chunk: any) {
if (isWebpack5) {
return compilation.chunkGraph.getChunkModulesIterable(chunk)
}
return chunk.modulesIterable
}
function getModuleId(compilation: any, module: any) {
if (isWebpack5) {
return compilation.chunkGraph.getModuleId(module)
}
return module.id
}
function buildManifest( function buildManifest(
_compiler: Compiler, _compiler: Compiler,
compilation: CompilationType.Compilation compilation: CompilationType.Compilation
...@@ -47,8 +65,8 @@ function buildManifest( ...@@ -47,8 +65,8 @@ function buildManifest(
return return
} }
for (const module of chunk.modulesIterable) { for (const module of getModulesIterable(compilation, chunk)) {
let id = module.id let id = getModuleId(compilation, module)
if (!manifest[request]) { if (!manifest[request]) {
manifest[request] = [] manifest[request] = []
...@@ -88,22 +106,40 @@ export class ReactLoadablePlugin { ...@@ -88,22 +106,40 @@ export class ReactLoadablePlugin {
this.filename = opts.filename this.filename = opts.filename
} }
createAssets(compiler: any, compilation: any, assets: any) {
const manifest = buildManifest(compiler, compilation)
var json = JSON.stringify(manifest, null, 2)
assets[this.filename] = {
source() {
return json
},
size() {
return json.length
},
}
return assets
}
apply(compiler: Compiler) { apply(compiler: Compiler) {
compiler.hooks.emit.tapAsync( if (isWebpack5) {
'ReactLoadableManifest', compiler.hooks.make.tap('ReactLoadableManifest', (compilation) => {
(compilation, callback) => { // @ts-ignore TODO: Remove ignore when webpack 5 is stable
const manifest = buildManifest(compiler, compilation) compilation.hooks.processAssets.tap(
var json = JSON.stringify(manifest, null, 2) {
compilation.assets[this.filename] = { name: 'ReactLoadableManifest',
source() { // @ts-ignore TODO: Remove ignore when webpack 5 is stable
return json stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
size() {
return json.length
}, },
} (assets: any) => {
callback() this.createAssets(compiler, compilation, assets)
} }
) )
})
return
}
compiler.hooks.emit.tap('ReactLoadableManifest', (compilation: any) => {
this.createAssets(compiler, compilation, compilation.assets)
})
} }
} }
...@@ -29,13 +29,18 @@ export class ServerlessPlugin { ...@@ -29,13 +29,18 @@ export class ServerlessPlugin {
// Async chunks are usages of import() for example // Async chunks are usages of import() for example
const dynamicChunks = chunk.getAllAsyncChunks() const dynamicChunks = chunk.getAllAsyncChunks()
for (const dynamicChunk of dynamicChunks) { for (const dynamicChunk of dynamicChunks) {
for (const module of dynamicChunk.modulesIterable) { if (isWebpack5) {
if (isWebpack5) { // @ts-ignore TODO: Remove ignore when webpack 5 is stable
for (const module of compilation.chunkGraph.getChunkModulesIterable(
chunk
)) {
// Add module back into the entry chunk // Add module back into the entry chunk
chunk.addModule(module) chunk.addModule(module)
continue
} }
continue
}
for (const module of dynamicChunk.modulesIterable) {
// Webpack 4 has separate GraphHelpers // Webpack 4 has separate GraphHelpers
GraphHelpers.connectChunkAndModule(chunk, module) GraphHelpers.connectChunkAndModule(chunk, module)
} }
......
...@@ -368,9 +368,7 @@ export default class HotReloader { ...@@ -368,9 +368,7 @@ export default class HotReloader {
// We only watch `_document` for changes on the server compilation // We only watch `_document` for changes on the server compilation
// the rest of the files will be triggered by the client compilation // the rest of the files will be triggered by the client compilation
const documentChunk = compilation.chunks.find( const documentChunk = compilation.namedChunks.get('pages/_document')
(c) => c.name === `pages/_document`
)
// If the document chunk can't be found we do nothing // If the document chunk can't be found we do nothing
if (!documentChunk) { if (!documentChunk) {
console.warn('_document.js chunk not found') console.warn('_document.js chunk not found')
...@@ -409,9 +407,9 @@ export default class HotReloader { ...@@ -409,9 +407,9 @@ export default class HotReloader {
const { compilation } = stats const { compilation } = stats
const chunkNames = new Set( const chunkNames = new Set(
compilation.chunks [...compilation.namedChunks.keys()].filter(
.map((c) => c.name) (name) => !!getRouteFromEntrypoint(name)
.filter((name) => !!getRouteFromEntrypoint(name)) )
) )
if (this.prevChunkNames) { if (this.prevChunkNames) {
......
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