Unverified Commit 2a5c21b6 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update to filter loader specific files from traces (#32267)

* Update to filter loader specific files from traces

* remove debug log
parent 797e260e
...@@ -10,7 +10,6 @@ import { TRACE_OUTPUT_VERSION } from '../../../shared/lib/constants' ...@@ -10,7 +10,6 @@ import { TRACE_OUTPUT_VERSION } from '../../../shared/lib/constants'
import { webpack, sources } from 'next/dist/compiled/webpack/webpack' import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack' import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import { import {
nextImageLoaderRegex,
NODE_ESM_RESOLVE_OPTIONS, NODE_ESM_RESOLVE_OPTIONS,
NODE_RESOLVE_OPTIONS, NODE_RESOLVE_OPTIONS,
resolveExternal, resolveExternal,
...@@ -32,7 +31,8 @@ function getModuleFromDependency( ...@@ -32,7 +31,8 @@ function getModuleFromDependency(
function getFilesMapFromReasons( function getFilesMapFromReasons(
fileList: Set<string>, fileList: Set<string>,
reasons: NodeFileTraceReasons reasons: NodeFileTraceReasons,
ignoreFn?: (file: string, parent?: string) => Boolean
) { ) {
// this uses the reasons tree to collect files specific to a // this uses the reasons tree to collect files specific to a
// certain parent allowing us to not have to trace each parent // certain parent allowing us to not have to trace each parent
...@@ -53,7 +53,10 @@ function getFilesMapFromReasons( ...@@ -53,7 +53,10 @@ function getFilesMapFromReasons(
parentFiles = new Set() parentFiles = new Set()
parentFilesMap.set(parent, parentFiles) parentFilesMap.set(parent, parentFiles)
} }
if (!ignoreFn?.(file, parent)) {
parentFiles.add(file) parentFiles.add(file)
}
const parentReason = reasons.get(parent) const parentReason = reasons.get(parent)
if (parentReason?.parents) { if (parentReason?.parents) {
...@@ -202,9 +205,11 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -202,9 +205,11 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
JSON.stringify({ JSON.stringify({
version: TRACE_OUTPUT_VERSION, version: TRACE_OUTPUT_VERSION,
files: [ files: [
...new Set([
...entryFiles, ...entryFiles,
...allEntryFiles, ...allEntryFiles,
...(this.entryTraces.get(entrypoint.name) || []), ...(this.entryTraces.get(entrypoint.name) || []),
]),
].map((file) => { ].map((file) => {
return nodePath return nodePath
.relative(traceOutputPath, file) .relative(traceOutputPath, file)
...@@ -349,7 +354,19 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -349,7 +354,19 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
await finishModulesSpan await finishModulesSpan
.traceChild('collect-traced-files') .traceChild('collect-traced-files')
.traceAsyncFn(() => { .traceAsyncFn(() => {
const parentFilesMap = getFilesMapFromReasons(fileList, reasons) const parentFilesMap = getFilesMapFromReasons(
fileList,
reasons,
(file) => {
file = nodePath.join(this.tracingRoot, file)
const depMod = depModMap.get(file)
return (
Array.isArray(depMod?.loaders) &&
depMod.loaders.length > 0
)
}
)
entryPaths.forEach((entry) => { entryPaths.forEach((entry) => {
const entryName = entryNameMap.get(entry)! const entryName = entryNameMap.get(entry)!
const normalizedEntry = nodePath.relative( const normalizedEntry = nodePath.relative(
...@@ -455,6 +472,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -455,6 +472,7 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
.catch((err) => callback(err)) .catch((err) => callback(err))
} }
) )
let resolver = compilation.resolverFactory.get('normal') let resolver = compilation.resolverFactory.get('normal')
function getPkgName(name: string) { function getPkgName(name: string) {
...@@ -491,6 +509,12 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -491,6 +509,12 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
return reject(new Error('module not found')) return reject(new Error('module not found'))
} }
// webpack resolver doesn't strip loader query info
// from the result so use path instead
if (result.includes('?') || result.includes('!')) {
result = resContext?.path || result
}
try { try {
// we need to collect all parent package.json's used // we need to collect all parent package.json's used
// as webpack's resolve doesn't expose this and parent // as webpack's resolve doesn't expose this and parent
...@@ -570,11 +594,6 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance { ...@@ -570,11 +594,6 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
job: import('@vercel/nft/out/node-file-trace').Job, job: import('@vercel/nft/out/node-file-trace').Job,
isEsmRequested: boolean isEsmRequested: boolean
): Promise<string> => { ): Promise<string> => {
if (this.staticImageImports && nextImageLoaderRegex.test(request)) {
throw new Error(
`not resolving ${request} as this is handled by next-image-loader`
)
}
const context = nodePath.dirname(parent) const context = nodePath.dirname(parent)
// When in esm externals mode, and using import, we resolve with // When in esm externals mode, and using import, we resolve with
// ESM resolving options. // ESM resolving options.
......
...@@ -79,7 +79,7 @@ describe('TypeScript Features', () => { ...@@ -79,7 +79,7 @@ describe('TypeScript Features', () => {
).toBe(true) ).toBe(true)
expect( expect(
helloTrace.files.some((file) => file.includes('components/world.js')) helloTrace.files.some((file) => file.includes('components/world.js'))
).toBe(true) ).toBe(false)
expect( expect(
helloTrace.files.some((file) => file.includes('react/index.js')) helloTrace.files.some((file) => file.includes('react/index.js'))
).toBe(true) ).toBe(true)
......
import data from 'mypackage/data'
console.log(data)
export default () => 'Hello from a' export default () => 'Hello from a'
...@@ -110,25 +110,35 @@ describe('TypeScript Features', () => { ...@@ -110,25 +110,35 @@ describe('TypeScript Features', () => {
singleAliasTrace.files.some((file) => singleAliasTrace.files.some((file) =>
file.includes('components/hello.js') file.includes('components/hello.js')
) )
).toBe(true) ).toBe(false)
expect( expect(
wildcardAliasTrace.files.some((file) => wildcardAliasTrace.files.some((file) =>
file.includes('mypackage/myfile.js') file.includes('mypackage/myfile.js')
) )
).toBe(true) ).toBe(true)
expect(
wildcardAliasTrace.files.some((file) =>
file.includes('mypackage/data.js')
)
).toBe(false)
expect( expect(
resolveOrderTrace.files.some((file) => file.includes('lib/a/api.js')) resolveOrderTrace.files.some((file) => file.includes('lib/a/api.js'))
).toBe(false)
expect(
resolveOrderTrace.files.some((file) =>
file.includes('mypackage/data.js')
)
).toBe(true) ).toBe(true)
expect( expect(
resolveFallbackTrace.files.some((file) => resolveFallbackTrace.files.some((file) =>
file.includes('lib/b/b-only.js') file.includes('lib/b/b-only.js')
) )
).toBe(true) ).toBe(false)
expect( expect(
basicAliasTrace.files.some((file) => basicAliasTrace.files.some((file) =>
file.includes('components/world.js') file.includes('components/world.js')
) )
).toBe(true) ).toBe(false)
}) })
}) })
}) })
import url from 'url'
import Link from 'next/link' import Link from 'next/link'
console.log(url.parse('https://example.com'))
export default () => ( export default () => (
<div> <div>
<Link href="/"> <Link href="/">
......
import Link from 'next/link' import Link from 'next/link?loaderQuery'
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
try {
let file = 'clear.js'
require('es5-ext/array/#/' + file)
} catch (_) {}
import('nanoid').then((mod) => console.log(mod.nanoid())) import('nanoid').then((mod) => console.log(mod.nanoid()))
} }
......
...@@ -103,9 +103,6 @@ describe.skip('Production Usage with swcMinify', () => { ...@@ -103,9 +103,6 @@ describe.skip('Production Usage with swcMinify', () => {
expect( expect(
serverTrace.files.some((file) => file.includes('node_modules/sharp')) serverTrace.files.some((file) => file.includes('node_modules/sharp'))
).toBe(false) ).toBe(false)
expect(
serverTrace.files.some((file) => file.includes('react.development.js'))
).toBe(false)
} }
const checks = [ const checks = [
...@@ -117,7 +114,7 @@ describe.skip('Production Usage with swcMinify', () => { ...@@ -117,7 +114,7 @@ describe.skip('Production Usage with swcMinify', () => {
/node_modules\/react\/package\.json/, /node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
], ],
notTests: [], notTests: [/\0/, /\?/, /!/],
}, },
{ {
page: '/client-error', page: '/client-error',
...@@ -127,13 +124,12 @@ describe.skip('Production Usage with swcMinify', () => { ...@@ -127,13 +124,12 @@ describe.skip('Production Usage with swcMinify', () => {
/node_modules\/react\/index\.js/, /node_modules\/react\/index\.js/,
/node_modules\/react\/package\.json/, /node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
/node_modules\/next/,
/next\/link\.js/, /next\/link\.js/,
/next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
/next\/dist\/pages\/_error\.js/,
/next\/error\.js/, /next\/error\.js/,
], ],
notTests: [], notTests: [/\0/, /\?/, /!/],
}, },
{ {
page: '/dynamic', page: '/dynamic',
...@@ -143,11 +139,11 @@ describe.skip('Production Usage with swcMinify', () => { ...@@ -143,11 +139,11 @@ describe.skip('Production Usage with swcMinify', () => {
/node_modules\/react\/index\.js/, /node_modules\/react\/index\.js/,
/node_modules\/react\/package\.json/, /node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
/node_modules\/next/,
/next\/link\.js/, /next\/link\.js/,
/next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
], ],
notTests: [], notTests: [/\0/, /\?/, /!/],
}, },
{ {
page: '/index', page: '/index',
...@@ -157,16 +153,19 @@ describe.skip('Production Usage with swcMinify', () => { ...@@ -157,16 +153,19 @@ describe.skip('Production Usage with swcMinify', () => {
/node_modules\/react\/index\.js/, /node_modules\/react\/index\.js/,
/node_modules\/react\/package\.json/, /node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
/node_modules\/next/,
/next\/link\.js/, /next\/link\.js/,
/next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
/node_modules\/nanoid\/index\.js/, /node_modules\/nanoid\/index\.js/,
/node_modules\/nanoid\/url-alphabet\/index\.js/, /node_modules\/nanoid\/url-alphabet\/index\.js/,
/node_modules\/es5-ext\/array\/#\/clear\.js/,
], ],
notTests: [ notTests: [
/node_modules\/nanoid\/index\.cjs/,
/next\/dist\/pages\/_error\.js/, /next\/dist\/pages\/_error\.js/,
/next\/error\.js/, /next\/error\.js/,
/\0/,
/\?/,
/!/,
], ],
}, },
{ {
...@@ -177,11 +176,12 @@ describe.skip('Production Usage with swcMinify', () => { ...@@ -177,11 +176,12 @@ describe.skip('Production Usage with swcMinify', () => {
/node_modules\/react\/index\.js/, /node_modules\/react\/index\.js/,
/node_modules\/react\/package\.json/, /node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
/node_modules\/react\/cjs\/react\.development\.js/,
/node_modules\/next/,
/next\/router\.js/, /next\/router\.js/,
/next\/dist\/client\/router\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
], ],
notTests: [], notTests: [/\0/, /\?/, /!/],
}, },
{ {
page: '/next-import', page: '/next-import',
...@@ -191,11 +191,17 @@ describe.skip('Production Usage with swcMinify', () => { ...@@ -191,11 +191,17 @@ describe.skip('Production Usage with swcMinify', () => {
/node_modules\/react\/index\.js/, /node_modules\/react\/index\.js/,
/node_modules\/react\/package\.json/, /node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
/node_modules\/next/,
/next\/link\.js/, /next\/link\.js/,
/next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
], ],
notTests: [/next\/dist\/server\/next\.js/, /next\/dist\/bin/], notTests: [
/next\/dist\/server\/next\.js/,
/next\/dist\/bin/,
/\0/,
/\?/,
/!/,
],
}, },
] ]
...@@ -206,14 +212,27 @@ describe.skip('Production Usage with swcMinify', () => { ...@@ -206,14 +212,27 @@ describe.skip('Production Usage with swcMinify', () => {
) )
const { version, files } = JSON.parse(contents) const { version, files } = JSON.parse(contents)
expect(version).toBe(1) expect(version).toBe(1)
expect([...new Set(files)].length).toBe(files.length)
expect( expect(
check.tests.every((item) => files.some((file) => item.test(file))) check.tests.every((item) => {
if (files.some((file) => item.test(file))) {
return true
}
console.error(`Failed to find ${item} in`, files)
return false
})
).toBe(true) ).toBe(true)
if (sep === '/') { if (sep === '/') {
expect( expect(
check.notTests.some((item) => files.some((file) => item.test(file))) check.notTests.some((item) => {
if (files.some((file) => item.test(file))) {
console.error(`Found unexpected ${item} in`, files)
return true
}
return false
})
).toBe(false) ).toBe(false)
} }
} }
......
import Link from 'next/link' import Link from 'next/link?loaderQuery'
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
try { try {
......
...@@ -126,7 +126,7 @@ describe('Production Usage', () => { ...@@ -126,7 +126,7 @@ describe('Production Usage', () => {
/node_modules\/react\/package\.json/, /node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
], ],
notTests: [/\0/], notTests: [/\0/, /\?/, /!/],
}, },
{ {
page: '/client-error', page: '/client-error',
...@@ -138,12 +138,10 @@ describe('Production Usage', () => { ...@@ -138,12 +138,10 @@ describe('Production Usage', () => {
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
/node_modules\/next/, /node_modules\/next/,
/next\/link\.js/, /next\/link\.js/,
/next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
/next\/dist\/pages\/_error\.js/,
/next\/error\.js/, /next\/error\.js/,
], ],
notTests: [/\0/], notTests: [/\0/, /\?/, /!/],
}, },
{ {
page: '/dynamic', page: '/dynamic',
...@@ -155,10 +153,9 @@ describe('Production Usage', () => { ...@@ -155,10 +153,9 @@ describe('Production Usage', () => {
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
/node_modules\/next/, /node_modules\/next/,
/next\/link\.js/, /next\/link\.js/,
/next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
], ],
notTests: [/\0/], notTests: [/\0/, /\?/, /!/],
}, },
{ {
page: '/index', page: '/index',
...@@ -170,13 +167,18 @@ describe('Production Usage', () => { ...@@ -170,13 +167,18 @@ describe('Production Usage', () => {
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
/node_modules\/next/, /node_modules\/next/,
/next\/link\.js/, /next\/link\.js/,
/next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
/node_modules\/nanoid\/index\.js/, /node_modules\/nanoid\/index\.js/,
/node_modules\/nanoid\/url-alphabet\/index\.js/, /node_modules\/nanoid\/url-alphabet\/index\.js/,
/node_modules\/es5-ext\/array\/#\/clear\.js/, /node_modules\/es5-ext\/array\/#\/clear\.js/,
], ],
notTests: [/next\/dist\/pages\/_error\.js/, /next\/error\.js/, /\0/], notTests: [
/next\/dist\/pages\/_error\.js/,
/next\/error\.js/,
/\0/,
/\?/,
/!/,
],
}, },
{ {
page: '/counter', page: '/counter',
...@@ -189,10 +191,9 @@ describe('Production Usage', () => { ...@@ -189,10 +191,9 @@ describe('Production Usage', () => {
/node_modules\/react\/cjs\/react\.development\.js/, /node_modules\/react\/cjs\/react\.development\.js/,
/node_modules\/next/, /node_modules\/next/,
/next\/router\.js/, /next\/router\.js/,
/next\/dist\/client\/router\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
], ],
notTests: [/\0/], notTests: [/\0/, /\?/, /!/],
}, },
{ {
page: '/next-import', page: '/next-import',
...@@ -204,10 +205,15 @@ describe('Production Usage', () => { ...@@ -204,10 +205,15 @@ describe('Production Usage', () => {
/node_modules\/react\/cjs\/react\.production\.min\.js/, /node_modules\/react\/cjs\/react\.production\.min\.js/,
/node_modules\/next/, /node_modules\/next/,
/next\/link\.js/, /next\/link\.js/,
/next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/, /next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
], ],
notTests: [/next\/dist\/server\/next\.js/, /next\/dist\/bin/, /\0/], notTests: [
/next\/dist\/server\/next\.js/,
/next\/dist\/bin/,
/\0/,
/\?/,
/!/,
],
}, },
] ]
...@@ -218,6 +224,7 @@ describe('Production Usage', () => { ...@@ -218,6 +224,7 @@ describe('Production Usage', () => {
) )
const { version, files } = JSON.parse(contents) const { version, files } = JSON.parse(contents)
expect(version).toBe(1) expect(version).toBe(1)
expect([...new Set(files)].length).toBe(files.length)
expect( expect(
check.tests.every((item) => { check.tests.every((item) => {
......
...@@ -74,8 +74,6 @@ describe('should set-up next', () => { ...@@ -74,8 +74,6 @@ describe('should set-up next', () => {
dot: true, dot: true,
}) })
console.error({ files })
for (const file of files) { for (const file of files) {
if (file.endsWith('.json') || file.endsWith('.html')) { if (file.endsWith('.json') || file.endsWith('.html')) {
await fs.remove(join(next.testDir, '.next/server', file)) await fs.remove(join(next.testDir, '.next/server', file))
......
...@@ -70,8 +70,6 @@ describe('should set-up next', () => { ...@@ -70,8 +70,6 @@ describe('should set-up next', () => {
dot: true, dot: true,
}) })
console.error({ files })
for (const file of files) { for (const file of files) {
if (file.endsWith('.json') || file.endsWith('.html')) { if (file.endsWith('.json') || file.endsWith('.html')) {
await fs.remove(join(next.testDir, '.next/server', file)) await fs.remove(join(next.testDir, '.next/server', file))
......
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