Unverified Commit 38ac55dd authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update output tracing resolving (#29473)

parent e5960dc6
...@@ -23,6 +23,7 @@ test/**/out* ...@@ -23,6 +23,7 @@ test/**/out*
test/**/next-env.d.ts test/**/next-env.d.ts
.DS_Store .DS_Store
/e2e-tests /e2e-tests
test/tmp/**
# Editors # Editors
**/.idea **/.idea
......
...@@ -1296,7 +1296,6 @@ export default async function getBaseWebpackConfig( ...@@ -1296,7 +1296,6 @@ export default async function getBaseWebpackConfig(
isWebpack5 && isWebpack5 &&
new TraceEntryPointsPlugin({ new TraceEntryPointsPlugin({
appDir: dir, appDir: dir,
esmExternals: config.experimental.esmExternals,
}), }),
// Moment.js is an extremely popular library that bundles large locale files // 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 // by default due to how Webpack interprets its code. This is a practical
......
...@@ -9,11 +9,6 @@ import { ...@@ -9,11 +9,6 @@ import {
isWebpack5, isWebpack5,
sources, sources,
} from 'next/dist/compiled/webpack/webpack' } from 'next/dist/compiled/webpack/webpack'
import {
NODE_ESM_RESOLVE_OPTIONS,
NODE_RESOLVE_OPTIONS,
} from '../../webpack-config'
import { NextConfigComplete } from '../../../server/config-shared'
const PLUGIN_NAME = 'TraceEntryPointsPlugin' const PLUGIN_NAME = 'TraceEntryPointsPlugin'
const TRACE_IGNORES = [ const TRACE_IGNORES = [
...@@ -38,20 +33,16 @@ export class TraceEntryPointsPlugin implements webpack.Plugin { ...@@ -38,20 +33,16 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
private appDir: string private appDir: string
private entryTraces: Map<string, string[]> private entryTraces: Map<string, string[]>
private excludeFiles: string[] private excludeFiles: string[]
private esmExternals: NextConfigComplete['experimental']['esmExternals']
constructor({ constructor({
appDir, appDir,
excludeFiles, excludeFiles,
esmExternals,
}: { }: {
appDir: string appDir: string
excludeFiles?: string[] excludeFiles?: string[]
esmExternals?: NextConfigComplete['experimental']['esmExternals']
}) { }) {
this.appDir = appDir this.appDir = appDir
this.entryTraces = new Map() this.entryTraces = new Map()
this.esmExternals = esmExternals
this.excludeFiles = excludeFiles || [] this.excludeFiles = excludeFiles || []
} }
...@@ -308,10 +299,18 @@ export class TraceEntryPointsPlugin implements webpack.Plugin { ...@@ -308,10 +299,18 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
const tracedDeps: string[] = [] const tracedDeps: string[] = []
for (const file of result.fileList) { for (const file of result.fileList) {
// don't include the entry itself
if (result.reasons[file].type === 'initial') { if (result.reasons[file].type === 'initial') {
continue continue
} }
tracedDeps.push(nodePath.join(root, file)) const filepath = nodePath.join(root, file)
// don't include transpiled files as they are included
// in the webpack output (e.g. chunks or the entry itself)
if (depModMap.get(filepath)?.originalSource?.()) {
continue
}
tracedDeps.push(filepath)
} }
// entryMod.buildInfo.cachedNextEntryTrace = { // entryMod.buildInfo.cachedNextEntryTrace = {
...@@ -354,80 +353,32 @@ export class TraceEntryPointsPlugin implements webpack.Plugin { ...@@ -354,80 +353,32 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
} }
) )
const resolver = compilation.resolverFactory.get('normal') const resolver = compilation.resolverFactory.get('normal')
const looseEsmExternals = this.esmExternals === 'loose'
const doResolve = async ( const doResolve = async (
request: string, request: string,
context: string, context: string,
isEsmRequested?: boolean _isEsmRequested?: boolean
): Promise<string> => { ): Promise<string> => {
const preferEsm = isEsmRequested && this.esmExternals return new Promise((resolve, reject) => {
const curResolver = resolver.withOptions( resolver.resolve(
preferEsm ? NODE_ESM_RESOLVE_OPTIONS : NODE_RESOLVE_OPTIONS {},
) context,
request,
let res: string = '' {
try { fileDependencies: compilation.fileDependencies,
res = await new Promise((resolve, reject) => { missingDependencies: compilation.missingDependencies,
curResolver.resolve( contextDependencies: compilation.contextDependencies,
{}, },
context, (err: any, result: string) => {
request, if (err) return reject(err)
{
fileDependencies: compilation.fileDependencies, if (!result) {
missingDependencies: compilation.missingDependencies, return reject(new Error('module not found'))
contextDependencies: compilation.contextDependencies,
},
(err: any, result: string) => {
if (err) reject(err)
else resolve(result)
}
)
})
} catch (err) {
if (!(isEsmRequested && looseEsmExternals)) {
throw err
}
// continue to attempt alternative resolving
}
if (!res && isEsmRequested && looseEsmExternals) {
const altResolver = resolver.withOptions(
preferEsm ? NODE_RESOLVE_OPTIONS : NODE_ESM_RESOLVE_OPTIONS
)
res = await new Promise((resolve, reject) => {
altResolver.resolve(
{},
context,
request,
{
fileDependencies: compilation.fileDependencies,
missingDependencies: compilation.missingDependencies,
contextDependencies: compilation.contextDependencies,
},
(err: any, result: string) => {
if (err) reject(err)
else resolve(result)
} }
) resolve(result)
}) }
}
if (!res) {
// we should not get here as one of the two above resolves should
// have thrown but this is here as a safeguard
throw new Error(
'invariant: failed to resolve ' +
JSON.stringify({
isEsmRequested,
looseEsmExternals,
request,
res,
})
) )
} })
return res
} }
this.tapfinishModules( this.tapfinishModules(
......
...@@ -118,8 +118,6 @@ describe('Production Usage', () => { ...@@ -118,8 +118,6 @@ describe('Production Usage', () => {
/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/,
/next\/link\.js/,
/next\/dist\/client\/link\.js/,
], ],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/], notTests: [/node_modules\/react\/cjs\/react\.development\.js/],
}, },
...@@ -131,8 +129,6 @@ describe('Production Usage', () => { ...@@ -131,8 +129,6 @@ describe('Production Usage', () => {
/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/,
/next\/router\.js/,
/next\/dist\/client\/router\.js/,
], ],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/], notTests: [/node_modules\/react\/cjs\/react\.development\.js/],
}, },
......
...@@ -96,29 +96,37 @@ export async function createNext(opts: { ...@@ -96,29 +96,37 @@ export async function createNext(opts: {
nextConfig?: NextConfig nextConfig?: NextConfig
skipStart?: boolean skipStart?: boolean
}): Promise<NextInstance> { }): Promise<NextInstance> {
if (nextInstance) { try {
throw new Error(`createNext called without destroying previous instance`) if (nextInstance) {
} throw new Error(`createNext called without destroying previous instance`)
}
if (testMode === 'dev') { if (testMode === 'dev') {
// next dev // next dev
nextInstance = new NextDevInstance(opts) nextInstance = new NextDevInstance(opts)
} else if (testMode === 'deploy') { } else if (testMode === 'deploy') {
// Vercel // Vercel
throw new Error('to-implement') throw new Error('to-implement')
} else { } else {
// next build + next start // next build + next start
nextInstance = new NextStartInstance(opts) nextInstance = new NextStartInstance(opts)
} }
nextInstance.on('destroy', () => { nextInstance.on('destroy', () => {
nextInstance = undefined nextInstance = undefined
}) })
await nextInstance.setup() await nextInstance.setup()
if (!opts.skipStart) { if (!opts.skipStart) {
await nextInstance.start() await nextInstance.start()
}
return nextInstance!
} catch (err) {
console.error('Failed to create next instance', err)
try {
await nextInstance.destroy()
} catch (_) {}
process.exit(1)
} }
return nextInstance!
} }
...@@ -50,12 +50,15 @@ export class NextInstance { ...@@ -50,12 +50,15 @@ export class NextInstance {
} }
console.log(`Creating test directory with isolated next...`) console.log(`Creating test directory with isolated next...`)
const tmpDir = process.env.NEXT_TEST_DIR || (await fs.realpath(os.tmpdir())) const skipIsolatedNext = !!process.env.NEXT_SKIP_ISOLATE
const tmpDir = skipIsolatedNext
? path.join(__dirname, '../../tmp')
: process.env.NEXT_TEST_DIR || (await fs.realpath(os.tmpdir()))
this.testDir = path.join(tmpDir, `next-test-${Date.now()}`) this.testDir = path.join(tmpDir, `next-test-${Date.now()}`)
if (process.env.NEXT_TEST_STARTER && !this.dependencies) { if (process.env.NEXT_TEST_STARTER && !this.dependencies) {
await fs.copy(process.env.NEXT_TEST_STARTER, this.testDir) await fs.copy(process.env.NEXT_TEST_STARTER, this.testDir)
} else { } else if (!skipIsolatedNext) {
this.testDir = await createNextInstall({ this.testDir = await createNextInstall({
react: 'latest', react: 'latest',
'react-dom': 'latest', 'react-dom': 'latest',
......
import path from 'path'
import resolveFrom from 'resolve-from'
import { spawn } from 'child_process' import { spawn } from 'child_process'
import { NextInstance } from './base' import { NextInstance } from './base'
...@@ -22,7 +24,9 @@ export class NextDevInstance extends NextInstance { ...@@ -22,7 +24,9 @@ export class NextDevInstance extends NextInstance {
} }
// we don't use yarn next here as yarn detaches itself from the // we don't use yarn next here as yarn detaches itself from the
// child process making it harder to kill all processes // child process making it harder to kill all processes
this.childProcess = spawn('node', ['node_modules/next/dist/bin/next'], { const nextDir = path.dirname(resolveFrom(this.testDir, 'next/package.json'))
this.childProcess = spawn('node', [path.join(nextDir, '/dist/bin/next')], {
cwd: this.testDir, cwd: this.testDir,
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', 'pipe', 'pipe'],
shell: false, shell: false,
......
import path from 'path' import path from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import resolveFrom from 'resolve-from'
import { spawn, SpawnOptions } from 'child_process' import { spawn, SpawnOptions } from 'child_process'
import { NextInstance } from './base' import { NextInstance } from './base'
...@@ -48,10 +49,11 @@ export class NextStartInstance extends NextInstance { ...@@ -48,10 +49,11 @@ export class NextStartInstance extends NextInstance {
this.emit('stderr', [msg]) this.emit('stderr', [msg])
}) })
} }
const nextDir = path.dirname(resolveFrom(this.testDir, 'next/package.json'))
this.childProcess = spawn( this.childProcess = spawn(
'node', 'node',
['node_modules/next/dist/bin/next', 'build'], [path.join(nextDir, '/dist/bin/next'), 'build'],
spawnOpts spawnOpts
) )
handleStdio() handleStdio()
...@@ -59,7 +61,7 @@ export class NextStartInstance extends NextInstance { ...@@ -59,7 +61,7 @@ export class NextStartInstance extends NextInstance {
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
this.childProcess.on('exit', (code) => { this.childProcess.on('exit', (code) => {
if (code) reject(new Error(`next build failed with code ${code}`)) if (code) reject(new Error(`next build failed with code ${code}`))
resolve() else resolve()
}) })
}) })
this._buildId = ( this._buildId = (
...@@ -76,7 +78,7 @@ export class NextStartInstance extends NextInstance { ...@@ -76,7 +78,7 @@ export class NextStartInstance extends NextInstance {
// child process making it harder to kill all processes // child process making it harder to kill all processes
this.childProcess = spawn( this.childProcess = spawn(
'node', 'node',
['node_modules/next/dist/bin/next', 'start'], [path.join(nextDir, '/dist/bin/next'), 'start'],
spawnOpts spawnOpts
) )
handleStdio() handleStdio()
......
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