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

Update to use project dir for file tracing base #30857

Merged
merged 7 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
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