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

Ensure build trace is correct with extra entries #28667

Merged
merged 5 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
assets[traceOutputName] = new sources.RawSource(
JSON.stringify({
version: TRACE_OUTPUT_VERSION,
files: [...entryFiles, ...this.entryTraces.get(entrypoint.name)!].map(
(file) => {
return nodePath
.relative(traceOutputPath, file)
.replace(/\\/g, '/')
}
),
files: [
...entryFiles,
...(this.entryTraces.get(entrypoint.name) || []),
].map((file) => {
return nodePath.relative(traceOutputPath, file).replace(/\\/g, '/')
}),
})
)
}
Expand Down Expand Up @@ -118,22 +117,35 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
// over the compilation modules list
const entryNameMap = new Map<string, string>()
const entryModMap = new Map<string, any>()
const additionalEntries = new Map<string, Map<string, any>>()

try {
const depModMap = new Map<string, any>()

compilation.entries.forEach((entry) => {
const name = entry.name || entry.options?.name

if (name?.startsWith('pages/') && entry.dependencies[0]) {
const entryMod = getModuleFromDependency(
compilation,
entry.dependencies[0]
)
if (name?.replace(/\\/g, '/').startsWith('pages/')) {
for (const dep of entry.dependencies) {
if (!dep) continue
const entryMod = getModuleFromDependency(compilation, dep)

if (entryMod && entryMod.resource) {
if (
entryMod.resource.replace(/\\/g, '/').includes('pages/')
) {
entryNameMap.set(entryMod.resource, name)
entryModMap.set(entryMod.resource, entryMod)
} else {
let curMap = additionalEntries.get(name)

if (entryMod.resource) {
entryNameMap.set(entryMod.resource, name)
entryModMap.set(entryMod.resource, entryMod)
if (!curMap) {
curMap = new Map()
additionalEntries.set(name, curMap)
}
curMap.set(entryMod.resource, entryMod)
}
}
}
}
})
Expand Down Expand Up @@ -225,6 +237,13 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {

const toTrace: string[] = [entry, ...depModMap.keys()]

const entryName = entryNameMap.get(entry)!
const curExtraEntries = additionalEntries.get(entryName)

if (curExtraEntries) {
toTrace.push(...curExtraEntries.keys())
}

const root = nodePath.parse(process.cwd()).root
const result = await nodeFileTrace(toTrace, {
base: root,
Expand All @@ -249,7 +268,7 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
// version: TRACE_OUTPUT_VERSION,
// tracedDeps,
// }
this.entryTraces.set(entryNameMap.get(entry)!, tracedDeps)
this.entryTraces.set(entryName, tracedDeps)
}

callback()
Expand Down
3 changes: 3 additions & 0 deletions test/integration/build-trace-extra-entries/content/hello.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
8 changes: 8 additions & 0 deletions test/integration/build-trace-extra-entries/lib/get-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from 'fs'
import path from 'path'

export function getData() {
return JSON.parse(
fs.readFileSync(path.join(process.cwd(), 'content/hello.json'), 'utf8')
)
}
25 changes: 25 additions & 0 deletions test/integration/build-trace-extra-entries/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path')

module.exports = {
experimental: {
nftTracing: true,
},
webpack(cfg, { isServer }) {
console.log(cfg.entry)
const origEntry = cfg.entry
cfg.entry = async () => {
const origEntries = await origEntry()

if (isServer) {
const curEntry = origEntries['pages/_app']
origEntries['pages/_app'] = [
path.resolve('lib/get-data.js'),
...curEntry,
]
console.log(origEntries)
}
return origEntries
}
return cfg
},
}
3 changes: 3 additions & 0 deletions test/integration/build-trace-extra-entries/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'index page'
}
32 changes: 32 additions & 0 deletions test/integration/build-trace-extra-entries/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-env jest */

import fs from 'fs-extra'
import { join } from 'path'
import { nextBuild } from 'next-test-utils'

jest.setTimeout(1000 * 60)

const appDir = join(__dirname, '..')

describe('build trace with extra entries', () => {
it('should build and trace correctly', async () => {
const result = await nextBuild(appDir, undefined, {
cwd: appDir,
})
expect(result.code).toBe(0)

const appTrace = await fs.readJSON(
join(appDir, '.next/server/pages/_app.js.nft.json')
)
const indexTrace = await fs.readJSON(
join(appDir, '.next/server/pages/index.js.nft.json')
)

expect(appTrace.files.some((file) => file.endsWith('hello.json'))).toBe(
true
)
expect(
indexTrace.files.some((file) => file.endsWith('hello.json'))
).toBeFalsy()
})
})