Commit 8c175e5d authored by Joe Haddad's avatar Joe Haddad Committed by Tim Neutkens

Replace `buildId` well known string during output (#6782)

* Replace buildId well known string during output

* Fix linting
parent 480fb565
...@@ -303,7 +303,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe ...@@ -303,7 +303,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
return /next-server[\\/]dist[\\/]/.test(context) || /next[\\/]dist[\\/]/.test(context) return /next-server[\\/]dist[\\/]/.test(context) || /next[\\/]dist[\\/]/.test(context)
} }
}), }),
target === 'serverless' && isServer && new ServerlessPlugin(buildId, { sourceMap: dev }), target === 'serverless' && isServer && new ServerlessPlugin(buildId),
target !== 'serverless' && isServer && new PagesManifestPlugin(), target !== 'serverless' && isServer && new PagesManifestPlugin(),
target !== 'serverless' && isServer && new NextJsSSRModuleCachePlugin({ outputPath }), target !== 'serverless' && isServer && new NextJsSSRModuleCachePlugin({ outputPath }),
isServer && new NextJsSsrImportPlugin(), isServer && new NextJsSsrImportPlugin(),
......
import { Compiler } from 'webpack' import { Compiler } from 'webpack'
import { SourceMapSource, RawSource } from 'webpack-sources'
import GraphHelpers from 'webpack/lib/GraphHelpers' import GraphHelpers from 'webpack/lib/GraphHelpers'
/** /**
...@@ -10,16 +9,60 @@ import GraphHelpers from 'webpack/lib/GraphHelpers' ...@@ -10,16 +9,60 @@ import GraphHelpers from 'webpack/lib/GraphHelpers'
const NEXT_REPLACE_BUILD_ID = '__NEXT_REPLACE__BUILD_ID__' 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 buildId: string
private sourceMap: boolean
constructor(buildId: string, { sourceMap = false } = {}) { constructor(buildId: string) {
this.buildId = buildId this.buildId = buildId
this.sourceMap = sourceMap
} }
apply(compiler: Compiler) { apply(compiler: Compiler) {
interceptFileWrites(compiler, content =>
replaceInBuffer(content, NEXT_REPLACE_BUILD_ID, this.buildId)
)
compiler.hooks.compilation.tap('ServerlessPlugin', compilation => { compiler.hooks.compilation.tap('ServerlessPlugin', compilation => {
compilation.hooks.optimizeChunksBasic.tap('ServerlessPlugin', chunks => { compilation.hooks.optimizeChunksBasic.tap('ServerlessPlugin', chunks => {
chunks.forEach(chunk => { chunks.forEach(chunk => {
...@@ -35,48 +78,6 @@ export class ServerlessPlugin { ...@@ -35,48 +78,6 @@ export class ServerlessPlugin {
} }
} }
}) })
compilation.hooks.afterOptimizeChunkAssets.tap(
'ServerlessPlugin',
chunks => {
chunks
.reduce(
(acc, chunk) => acc.concat(chunk.files || []),
[] as any[]
)
.forEach(file => {
const asset = compilation.assets[file]
let input
let inputSourceMap
if (this.sourceMap) {
if (asset.sourceAndMap) {
const sourceAndMap = asset.sourceAndMap()
inputSourceMap = sourceAndMap.map
input = sourceAndMap.source
} else {
inputSourceMap = asset.map()
input = asset.source()
}
} else {
input = asset.source()
}
const output = input.replace(NEXT_REPLACE_BUILD_ID, this.buildId)
if (this.sourceMap && inputSourceMap) {
compilation.assets[file] = new SourceMapSource(
output,
file,
inputSourceMap
)
} else {
compilation.assets[file] = new RawSource(output)
}
})
}
)
}) })
}) })
} }
......
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