Unverified Commit 071ecb0b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update tracing to collect from reasons (#29975)

This updates the `outputFileTracing` to fix a few cases we noticed where files weren't caught and also ensures we match webpack's `esm` resolving so that we don't include `cjs` files when webpack expects `esm`, it also updates to the latest `@vercel/nft` version which removes the caching in favor of ensuring the `reasons` map contains all parents allowing us to trace all entries in one `nodeFileTrace` run and the collect the separate files for each entry from the `reasons` map giving us much better performance/reliability.  

This also ensures we don't include static image imports when enabled in the traces since they can drastically increase deployment size on larger projects. 
parent b7114bce
......@@ -214,6 +214,9 @@ export const NODE_BASE_ESM_RESOLVE_OPTIONS = {
let TSCONFIG_WARNED = false
export const nextImageLoaderRegex =
/\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i
export default async function getBaseWebpackConfig(
dir: string,
{
......@@ -1057,7 +1060,7 @@ export default async function getBaseWebpackConfig(
...(!config.images.disableStaticImages
? [
{
test: /\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i,
test: nextImageLoaderRegex,
loader: 'next-image-loader',
issuer: { not: regexLikeCss },
dependency: { not: ['url'] },
......@@ -1199,6 +1202,8 @@ export default async function getBaseWebpackConfig(
!dev &&
new TraceEntryPointsPlugin({
appDir: dir,
esmExternals: config.experimental.esmExternals,
staticImageImports: !config.images.disableStaticImages,
}),
// 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
......
import curry from 'next/dist/compiled/lodash.curry'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { nextImageLoaderRegex } from '../../../../webpack-config'
import { loader } from '../../helpers'
import { ConfigurationContext, ConfigurationFn, pipe } from '../../utils'
import { getCustomDocumentImageError } from './messages'
......@@ -12,7 +13,7 @@ export const images = curry(async function images(
loader({
oneOf: [
{
test: /\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i,
test: nextImageLoaderRegex,
use: {
loader: 'error-loader',
options: {
......
This diff is collapsed.
......@@ -183,7 +183,7 @@
"@types/webpack-sources1": "npm:@types/webpack-sources@0.1.5",
"@types/ws": "8.2.0",
"@vercel/ncc": "0.27.0",
"@vercel/nft": "0.16.1",
"@vercel/nft": "0.17.0",
"amphtml-validator": "1.0.33",
"arg": "4.1.0",
"async-retry": "1.2.3",
......
......@@ -91,7 +91,7 @@ async function main() {
let totalUncompressedSize = 0
for (const file of result.fileList) {
if (result.reasons[file].type === 'initial') {
if (result.reasons.get(file).type === 'initial') {
continue
}
tracedDeps.add(file.replace(/\\/g, '/'))
......
import fs from 'fs'
import path from 'path'
import Image from 'next/image'
import testImage from '../public/test.jpg'
export default function Page(props) {
return (
<div>
<Image src={testImage} placeholder="blur" alt="test" />
</div>
)
}
export function getServerSideProps() {
try {
// this should be included in the trace since it's not an
// import
fs.readFileSync(path.join(process.cwd(), 'public/another.jpg'))
} catch (_) {}
return {
props: {},
}
}
......@@ -25,10 +25,16 @@ describe('build trace with extra entries', () => {
const anotherTrace = await fs.readJSON(
join(appDir, '.next/server/pages/another.js.nft.json')
)
const imageTrace = await fs.readJSON(
join(appDir, '.next/server/pages/image-import.js.nft.json')
)
expect(appTrace.files.some((file) => file.endsWith('hello.json'))).toBe(
true
)
expect(
appTrace.files.some((file) => file.endsWith('lib/get-data.js'))
).toBe(true)
expect(
indexTrace.files.some((file) => file.endsWith('hello.json'))
).toBeFalsy()
......@@ -51,5 +57,11 @@ describe('build trace with extra entries', () => {
file.includes('nested-structure/dist/constants.js')
)
).toBe(true)
expect(
imageTrace.files.some((file) => file.includes('public/another.jpg'))
).toBe(true)
expect(
imageTrace.files.some((file) => file.includes('public/test.jpg'))
).toBe(false)
})
})
import Link from 'next/link'
if (typeof window === 'undefined') {
import('nanoid').then((mod) => console.log(mod.nanoid()))
}
export default () => (
<div>
<Link href="/about">
......
......@@ -104,8 +104,13 @@ describe('Production Usage', () => {
/next\/link\.js/,
/next\/dist\/client\/link\.js/,
/next\/dist\/shared\/lib\/router\/utils\/resolve-rewrites\.js/,
/node_modules\/nanoid\/index\.js/,
/node_modules\/nanoid\/url-alphabet\/index\.js/,
],
notTests: [
/node_modules\/react\/cjs\/react\.development\.js/,
/node_modules\/nanoid\/index\.cjs/,
],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/],
},
{
page: '/counter',
......
......@@ -5054,10 +5054,10 @@
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.27.0.tgz#c0cfeebb0bebb56052719efa4a0ecc090a932c76"
integrity sha512-DllIJQapnU2YwewIhh/4dYesmMQw3h2cFtabECc/zSJHqUbNa0eJuEkRa6DXbZvh1YPWBtYQoPV17NlDpBw1Vw==
"@vercel/nft@0.16.1":
version "0.16.1"
resolved "https://registry.yarnpkg.com/@vercel/nft/-/nft-0.16.1.tgz#a636e3fc55f5502a3c652f8c1e01ca2bfac6cf68"
integrity sha512-OiPCdRDo9qA8QRQkgdyPInjF6TkvtWv79/WVpweufeOxjwUYju0jIUuFLS2PeB4wAIoKc/1CiizjhmJRqXNwAA==
"@vercel/nft@0.17.0":
version "0.17.0"
resolved "https://registry.yarnpkg.com/@vercel/nft/-/nft-0.17.0.tgz#28851fefe42fae7a116dc5e23a0a9da29929a18b"
integrity sha512-dXz4RAODHpEPl1Yfzk1UfVpZfqhY9O80FdI9sF/Hw2bXWj5022U9Z46VpdNYe8pDluSuQv/JUo/ZP4/OcsfDRw==
dependencies:
"@mapbox/node-pre-gyp" "^1.0.5"
acorn "^8.3.0"
......
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