Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use deterministic module IDs for server #41066

Merged
merged 4 commits into from Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 1 addition & 6 deletions packages/next/build/webpack-config.ts
Expand Up @@ -1278,12 +1278,7 @@ export default async function getBaseWebpackConfig(
emitOnErrors: !dev,
checkWasmTypes: false,
nodeEnv: false,
...(hasServerComponents
? {
// We have to use the names here instead of hashes to ensure the consistency between compilers.
moduleIds: isClient ? 'deterministic' : 'named',
}
: {}),
...(hasServerComponents ? { moduleIds: 'deterministic' } : {}),
splitChunks: (():
| Required<webpack.Configuration>['optimization']['splitChunks']
| false => {
Expand Down
@@ -1,5 +1,6 @@
import { stringify } from 'querystring'
import path from 'path'
import { relative } from 'path'
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import {
getInvalidator,
Expand All @@ -11,7 +12,7 @@ import type {
ClientComponentImports,
NextFlightClientEntryLoaderOptions,
} from '../loaders/next-flight-client-entry-loader'
import { APP_DIR_ALIAS } from '../../../lib/constants'
import { APP_DIR_ALIAS, WEBPACK_LAYERS } from '../../../lib/constants'
import {
COMPILER_NAMES,
EDGE_RUNTIME_WEBPACK,
Expand All @@ -31,6 +32,9 @@ const PLUGIN_NAME = 'ClientEntryPlugin'

export const injectedClientEntries = new Map()

export const serverModuleIds = new Map<string, string | number>()
export const edgeServerModuleIds = new Map<string, string | number>()

// TODO-APP: ensure .scss / .sass also works.
const regexCSS = /\.css$/

Expand Down Expand Up @@ -77,6 +81,60 @@ export class FlightClientEntryPlugin {
}
}
})

const recordModule = (id: number | string, mod: any) => {
const modResource = mod.resourceResolveData?.path || mod.resource

if (
mod.resourceResolveData?.context?.issuerLayer ===
WEBPACK_LAYERS.server
) {
return
}

if (typeof id !== 'undefined' && modResource) {
// Note that this isn't that reliable as webpack is still possible to assign
// additional queries to make sure there's no conflict even using the `named`
// module ID strategy.
let ssrNamedModuleId = relative(compiler.context, modResource)
if (!ssrNamedModuleId.startsWith('.')) {
// TODO use getModuleId instead
ssrNamedModuleId = `./${ssrNamedModuleId.replace(/\\/g, '/')}`
}

if (this.isEdgeServer) {
edgeServerModuleIds.set(
ssrNamedModuleId.replace(/\/next\/dist\/esm\//, '/next/dist/'),
id
)
} else {
serverModuleIds.set(ssrNamedModuleId, id)
}
}
}

compilation.chunkGroups.forEach((chunkGroup) => {
chunkGroup.chunks.forEach((chunk: webpack.Chunk) => {
const chunkModules = compilation.chunkGraph.getChunkModulesIterable(
chunk
) as Iterable<webpack.NormalModule>

for (const mod of chunkModules) {
const modId = compilation.chunkGraph.getModuleId(mod)

recordModule(modId, mod)

// If this is a concatenation, register each child to the parent ID.
// TODO: remove any
const anyModule = mod as any
if (anyModule.modules) {
anyModule.modules.forEach((concatenatedMod: any) => {
recordModule(modId, concatenatedMod)
})
}
}
})
})
})
}

Expand Down Expand Up @@ -323,6 +381,7 @@ export class FlightClientEntryPlugin {
// Check if request is for css file.
if ((!inClientComponentBoundary && isClientComponent) || isCSS) {
clientComponentImports.push(modRequest)

return
}

Expand Down
25 changes: 17 additions & 8 deletions packages/next/build/webpack/plugins/flight-manifest-plugin.ts
Expand Up @@ -10,6 +10,11 @@ import { FLIGHT_MANIFEST } from '../../../shared/lib/constants'
import { relative } from 'path'
import { isClientComponentModule } from '../loaders/utils'

import {
edgeServerModuleIds,
serverModuleIds,
} from './flight-client-entry-plugin'

// This is the module that will be used to anchor all client references to.
// I.e. it will have all the client files as async deps from this point on.
// We use the Flight client implementation because you can't get to these
Expand Down Expand Up @@ -304,16 +309,20 @@ export class FlightManifestPlugin {
}
}

moduleIdMapping[id] = moduleIdMapping[id] || {}
moduleIdMapping[id][name] = {
...moduleExports[name],
id: ssrNamedModuleId,
if (serverModuleIds.has(ssrNamedModuleId)) {
moduleIdMapping[id] = moduleIdMapping[id] || {}
moduleIdMapping[id][name] = {
...moduleExports[name],
id: serverModuleIds.get(ssrNamedModuleId)!,
}
}

edgeModuleIdMapping[id] = edgeModuleIdMapping[id] || {}
edgeModuleIdMapping[id][name] = {
...moduleExports[name],
id: ssrNamedModuleId.replace(/\/next\/dist\//, '/next/dist/esm/'),
if (edgeServerModuleIds.has(ssrNamedModuleId)) {
edgeModuleIdMapping[id] = edgeModuleIdMapping[id] || {}
edgeModuleIdMapping[id][name] = {
...moduleExports[name],
id: edgeServerModuleIds.get(ssrNamedModuleId)!,
}
}
})

Expand Down