Unverified Commit 5ad0ea8e authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Remove `__NEXT_REPLACE__BUILD_ID__` workaround (#8445)

We no longer need this because we don't run terser on serverless bundles.

---

Closes https://github.com/zeit/next.js/issues/8436
parent 847e892d
...@@ -59,6 +59,7 @@ export function createEntrypoints( ...@@ -59,6 +59,7 @@ export function createEntrypoints(
absoluteDocumentPath: pages['/_document'], absoluteDocumentPath: pages['/_document'],
absoluteErrorPath: pages['/_error'], absoluteErrorPath: pages['/_error'],
distDir: DOT_NEXT_ALIAS, distDir: DOT_NEXT_ALIAS,
buildId,
assetPrefix: config.assetPrefix, assetPrefix: config.assetPrefix,
generateEtags: config.generateEtags, generateEtags: config.generateEtags,
ampBindInitData: config.experimental.ampBindInitData, ampBindInitData: config.experimental.ampBindInitData,
......
...@@ -571,11 +571,7 @@ export default async function getBaseWebpackConfig( ...@@ -571,11 +571,7 @@ export default async function getBaseWebpackConfig(
) )
}, },
}), }),
isLikeServerless && isServerless && new ServerlessPlugin(),
new ServerlessPlugin(buildId, {
isServer,
isTrace: isServerlessTrace,
}),
isServer && new PagesManifestPlugin(isLikeServerless), isServer && new PagesManifestPlugin(isLikeServerless),
target === 'server' && target === 'server' &&
isServer && isServer &&
......
...@@ -12,6 +12,7 @@ export type ServerlessLoaderQuery = { ...@@ -12,6 +12,7 @@ export type ServerlessLoaderQuery = {
absoluteAppPath: string absoluteAppPath: string
absoluteDocumentPath: string absoluteDocumentPath: string
absoluteErrorPath: string absoluteErrorPath: string
buildId: string
assetPrefix: string assetPrefix: string
ampBindInitData: boolean | string ampBindInitData: boolean | string
generateEtags: string generateEtags: string
...@@ -23,6 +24,7 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -23,6 +24,7 @@ const nextServerlessLoader: loader.Loader = function() {
distDir, distDir,
absolutePagePath, absolutePagePath,
page, page,
buildId,
canonicalBase, canonicalBase,
assetPrefix, assetPrefix,
ampBindInitData, ampBindInitData,
...@@ -88,7 +90,7 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -88,7 +90,7 @@ const nextServerlessLoader: loader.Loader = function() {
buildManifest, buildManifest,
reactLoadableManifest, reactLoadableManifest,
canonicalBase: "${canonicalBase}", canonicalBase: "${canonicalBase}",
buildId: "__NEXT_REPLACE__BUILD_ID__", buildId: "${buildId}",
assetPrefix: "${assetPrefix}", assetPrefix: "${assetPrefix}",
ampBindInitData: ${ampBindInitData === true || ampBindInitData: ${ampBindInitData === true ||
ampBindInitData === 'true'} ampBindInitData === 'true'}
......
...@@ -7,94 +7,24 @@ import { connectChunkAndModule } from 'webpack/lib/GraphHelpers' ...@@ -7,94 +7,24 @@ import { connectChunkAndModule } from 'webpack/lib/GraphHelpers'
* This is to make sure there is a single render bundle instead of that bundle importing dynamic chunks * This is to make sure there is a single render bundle instead of that bundle importing dynamic chunks
*/ */
const NEXT_REPLACE_BUILD_ID = '__NEXT_REPLACE__BUILD_ID__'
function replaceInBuffer(buffer: Buffer, from: string, to: string) {
const target = Buffer.from(from, 'utf8')
const replacement = Buffer.from(to, 'utf8')
function bufferTee(source: Buffer): Buffer {
const index = source.indexOf(target)
if (index === -1) {
// Escape recursion loop
return source
}
const b1 = source.slice(0, index)
const b2 = source.slice(index + target.length)
const nextBuffer = bufferTee(b2)
return Buffer.concat(
[b1, replacement, nextBuffer],
index + replacement.length + nextBuffer.length
)
}
return bufferTee(buffer)
}
function interceptFileWrites(
compiler: Compiler,
contentFn: (input: Buffer) => Buffer
) {
compiler.outputFileSystem = new Proxy(compiler.outputFileSystem, {
get(target, propKey) {
const orig = (target as any)[propKey]
if (propKey !== 'writeFile') {
return orig
}
return function(targetPath: string, content: Buffer, ...args: any[]) {
return orig.call(target, targetPath, contentFn(content), ...args)
}
},
})
}
export class ServerlessPlugin { export class ServerlessPlugin {
private buildId: string
private isServer: boolean
private isTrace: boolean
constructor(
buildId: string,
{ isServer, isTrace }: { isServer: boolean; isTrace: boolean }
) {
this.buildId = buildId
this.isServer = isServer
this.isTrace = isTrace
}
apply(compiler: Compiler) { apply(compiler: Compiler) {
if (!this.isServer) { compiler.hooks.compilation.tap('ServerlessPlugin', compilation => {
return compilation.hooks.optimizeChunksBasic.tap('ServerlessPlugin', chunks => {
} chunks.forEach(chunk => {
// If chunk is not an entry point skip them
interceptFileWrites(compiler, content => if (chunk.hasEntryModule()) {
replaceInBuffer(content, NEXT_REPLACE_BUILD_ID, this.buildId) const dynamicChunks = chunk.getAllAsyncChunks()
) if (dynamicChunks.size !== 0) {
for (const dynamicChunk of dynamicChunks) {
if (!this.isTrace) { for (const module of dynamicChunk.modulesIterable) {
compiler.hooks.compilation.tap('ServerlessPlugin', compilation => { connectChunkAndModule(chunk, module)
compilation.hooks.optimizeChunksBasic.tap(
'ServerlessPlugin',
chunks => {
chunks.forEach(chunk => {
// If chunk is not an entry point skip them
if (chunk.hasEntryModule()) {
const dynamicChunks = chunk.getAllAsyncChunks()
if (dynamicChunks.size !== 0) {
for (const dynamicChunk of dynamicChunks) {
for (const module of dynamicChunk.modulesIterable) {
connectChunkAndModule(chunk, module)
}
}
} }
} }
}) }
} }
) })
}) })
} })
} }
} }
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