Skip to content

Commit

Permalink
Update to use project dir for file tracing base (#30857)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
ijjk and kodiakhq[bot] committed Nov 4, 2021
1 parent 9518248 commit 5de4f66
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,7 @@ export default async function getBaseWebpackConfig(
appDir: dir,
esmExternals: config.experimental.esmExternals,
staticImageImports: !config.images.disableStaticImages,
outputFileTracingRoot: config.experimental.outputFileTracingRoot,
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const TRACE_IGNORES = [
'**/*/next/dist/server/next.js',
'**/*/next/dist/bin/next',
]
const root = nodePath.parse(process.cwd()).root

function getModuleFromDependency(
compilation: any,
Expand Down Expand Up @@ -81,6 +80,7 @@ function getFilesMapFromReasons(

export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
private appDir: string
private tracingRoot: string
private entryTraces: Map<string, Set<string>>
private excludeFiles: string[]
private esmExternals?: NextConfigComplete['experimental']['esmExternals']
Expand All @@ -91,17 +91,20 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
excludeFiles,
esmExternals,
staticImageImports,
outputFileTracingRoot,
}: {
appDir: string
excludeFiles?: string[]
staticImageImports: boolean
outputFileTracingRoot?: string
esmExternals?: NextConfigComplete['experimental']['esmExternals']
}) {
this.appDir = appDir
this.entryTraces = new Map()
this.esmExternals = esmExternals
this.excludeFiles = excludeFiles || []
this.staticImageImports = staticImageImports
this.tracingRoot = outputFileTracingRoot || appDir
}

// Here we output all traced assets and webpack chunks to a
Expand Down Expand Up @@ -141,7 +144,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
}

const result = await nodeFileTrace([...chunksToTrace], {
base: root,
base: this.tracingRoot,
processCwd: this.appDir,
readFile: async (path) => {
if (chunksToTrace.has(path)) {
Expand Down Expand Up @@ -193,9 +196,9 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {

entryFiles.forEach((file) => {
parentFilesMap
.get(nodePath.relative(root, file))
.get(nodePath.relative(this.tracingRoot, file))
?.forEach((child) => {
allEntryFiles.add(nodePath.join(root, child))
allEntryFiles.add(nodePath.join(this.tracingRoot, child))
})
})
// don't include the entry itself in the trace
Expand Down Expand Up @@ -326,7 +329,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
})
.traceAsyncFn(async () => {
const result = await nodeFileTrace(entriesToTrace, {
base: root,
base: this.tracingRoot,
processCwd: this.appDir,
readFile,
readlink,
Expand Down Expand Up @@ -355,25 +358,28 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
const parentFilesMap = getFilesMapFromReasons(fileList, reasons)
entryPaths.forEach((entry) => {
const entryName = entryNameMap.get(entry)!
const normalizedEntry = nodePath.relative(root, entry)
const normalizedEntry = nodePath.relative(
this.tracingRoot,
entry
)
const curExtraEntries = additionalEntries.get(entryName)
const finalDeps = new Set<string>()

parentFilesMap.get(normalizedEntry)?.forEach((dep) => {
finalDeps.add(nodePath.join(root, dep))
finalDeps.add(nodePath.join(this.tracingRoot, dep))
})

if (curExtraEntries) {
for (const extraEntry of curExtraEntries.keys()) {
const normalizedExtraEntry = nodePath.relative(
root,
this.tracingRoot,
extraEntry
)
finalDeps.add(extraEntry)
parentFilesMap
.get(normalizedExtraEntry)
?.forEach((dep) => {
finalDeps.add(nodePath.join(root, dep))
finalDeps.add(nodePath.join(this.tracingRoot, dep))
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export type NextConfig = { [key: string]: any } & {
serverComponents?: boolean
fullySpecified?: boolean
urlImports?: NonNullable<webpack5.Configuration['experiments']>['buildHttp']
outputFileTracingRoot?: string
}
}

Expand Down Expand Up @@ -236,6 +237,7 @@ export const defaultConfig: NextConfig = {
concurrentFeatures: false,
serverComponents: false,
fullySpecified: false,
outputFileTracingRoot: process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT || '',
},
future: {
strictPostcssConfiguration: false,
Expand Down
1 change: 1 addition & 0 deletions test/lib/next-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function runNextCommand(argv, options = {}) {
...options.env,
NODE_ENV: '',
__NEXT_TEST_MODE: 'true',
NEXT_PRIVATE_OUTPUT_TRACE_ROOT: path.join(__dirname, '../../'),
}

return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 5de4f66

Please sign in to comment.