Ensure build trace is correct with extra entries (#28667)
* Ensure build trace is correct with extra entries * lint-fix * fix check on windows
This commit is contained in:
parent
c1dbc12609
commit
9a6542ba68
6 changed files with 106 additions and 16 deletions
|
@ -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, '/')
|
||||
}),
|
||||
})
|
||||
)
|
||||
}
|
||||
|
@ -118,6 +117,7 @@ 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>()
|
||||
|
@ -125,15 +125,27 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
|
|||
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.resource) {
|
||||
entryNameMap.set(entryMod.resource, name)
|
||||
entryModMap.set(entryMod.resource, entryMod)
|
||||
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 (!curMap) {
|
||||
curMap = new Map()
|
||||
additionalEntries.set(name, curMap)
|
||||
}
|
||||
curMap.set(entryMod.resource, entryMod)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -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,
|
||||
|
@ -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()
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"hello": "world"
|
||||
}
|
|
@ -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
test/integration/build-trace-extra-entries/next.config.js
Normal file
25
test/integration/build-trace-extra-entries/next.config.js
Normal file
|
@ -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
|
||||
},
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
export default function Page() {
|
||||
return 'index page'
|
||||
}
|
|
@ -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()
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue