-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up AMP bundle removal (#14130)
Drops the entrypoint instead of removing the underlying file.
- Loading branch information
1 parent
05f61b2
commit 27f653b
Showing
9 changed files
with
123 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 82 additions & 17 deletions
99
packages/next/build/webpack/plugins/next-drop-client-page-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,94 @@ | ||
import { Compiler, Plugin } from 'webpack' | ||
import { extname } from 'path' | ||
import { Compiler, compilation as CompilationType, Plugin } from 'webpack' | ||
import { STRING_LITERAL_DROP_BUNDLE } from '../../../next-server/lib/constants' | ||
|
||
export const ampFirstEntryNamesMap: WeakMap< | ||
CompilationType.Compilation, | ||
string[] | ||
> = new WeakMap() | ||
|
||
const PLUGIN_NAME = 'DropAmpFirstPagesPlugin' | ||
|
||
// Recursively look up the issuer till it ends up at the root | ||
function findEntryModule(mod: any): CompilationType.Module | null { | ||
const queue = new Set([mod]) | ||
for (const module of queue) { | ||
for (const reason of module.reasons) { | ||
if (!reason.module) return module | ||
queue.add(reason.module) | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
function handler(parser: any) { | ||
function markAsAmpFirst() { | ||
const entryModule = findEntryModule(parser.state.module) | ||
|
||
if (!entryModule) { | ||
return | ||
} | ||
|
||
// @ts-ignore buildInfo exists on Module | ||
entryModule.buildInfo.NEXT_ampFirst = true | ||
} | ||
|
||
parser.hooks.varDeclarationConst | ||
.for(STRING_LITERAL_DROP_BUNDLE) | ||
.tap(PLUGIN_NAME, markAsAmpFirst) | ||
|
||
parser.hooks.varDeclarationLet | ||
.for(STRING_LITERAL_DROP_BUNDLE) | ||
.tap(PLUGIN_NAME, markAsAmpFirst) | ||
|
||
parser.hooks.varDeclaration | ||
.for(STRING_LITERAL_DROP_BUNDLE) | ||
.tap(PLUGIN_NAME, markAsAmpFirst) | ||
} | ||
|
||
// Prevents outputting client pages when they are not needed | ||
export class DropClientPage implements Plugin { | ||
ampPages = new Set() | ||
|
||
apply(compiler: Compiler) { | ||
compiler.hooks.emit.tap('DropClientPage', (compilation) => { | ||
Object.keys(compilation.assets).forEach((assetKey) => { | ||
const asset = compilation.assets[assetKey] | ||
compiler.hooks.compilation.tap( | ||
PLUGIN_NAME, | ||
(compilation, { normalModuleFactory }) => { | ||
normalModuleFactory.hooks.parser | ||
.for('javascript/auto') | ||
.tap(PLUGIN_NAME, handler) | ||
|
||
if (asset?._value?.includes?.('__NEXT_DROP_CLIENT_FILE__')) { | ||
const cleanAssetKey = assetKey.replace(/\\/g, '/') | ||
const page = '/' + cleanAssetKey.split('pages/')[1] | ||
const pageNoExt = page.split(extname(page))[0] | ||
if (!ampFirstEntryNamesMap.has(compilation)) { | ||
ampFirstEntryNamesMap.set(compilation, []) | ||
} | ||
|
||
delete compilation.assets[assetKey] | ||
const ampFirstEntryNamesItem = ampFirstEntryNamesMap.get( | ||
compilation | ||
) as string[] | ||
|
||
// Detect being re-ran through a child compiler and don't re-mark the | ||
// page as AMP | ||
if (!pageNoExt.endsWith('.module')) { | ||
this.ampPages.add(pageNoExt.replace(/\/index$/, '') || '/') | ||
compilation.hooks.seal.tap(PLUGIN_NAME, () => { | ||
// Remove preparedEntrypoint that has bundle drop marker | ||
// This will ensure webpack does not create chunks/bundles for this particular entrypoint | ||
for ( | ||
let i = compilation._preparedEntrypoints.length - 1; | ||
i >= 0; | ||
i-- | ||
) { | ||
const entrypoint = compilation._preparedEntrypoints[i] | ||
if (entrypoint?.module?.buildInfo?.NEXT_ampFirst) { | ||
ampFirstEntryNamesItem.push(entrypoint.name) | ||
compilation._preparedEntrypoints.splice(i, 1) | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
for (let i = compilation.entries.length - 1; i >= 0; i--) { | ||
const entryModule = compilation.entries[i] | ||
if (entryModule?.buildInfo?.NEXT_ampFirst) { | ||
compilation.entries.splice(i, 1) | ||
} | ||
} | ||
}) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters