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*
test/**/next-env.d.ts
.DS_Store
/e2e-tests
test/tmp/**
# Editors
**/.idea
......
......@@ -1296,7 +1296,6 @@ export default async function getBaseWebpackConfig(
isWebpack5 &&
new TraceEntryPointsPlugin({
appDir: dir,
esmExternals: config.experimental.esmExternals,
}),
// 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
......
......@@ -9,11 +9,6 @@ import {
isWebpack5,
sources,
} 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 TRACE_IGNORES = [
......@@ -38,20 +33,16 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
private appDir: string
private entryTraces: Map<string, string[]>
private excludeFiles: string[]
private esmExternals: NextConfigComplete['experimental']['esmExternals']
constructor({
appDir,
excludeFiles,
esmExternals,
}: {
appDir: string
excludeFiles?: string[]
esmExternals?: NextConfigComplete['experimental']['esmExternals']
}) {
this.appDir = appDir
this.entryTraces = new Map()
this.esmExternals = esmExternals
this.excludeFiles = excludeFiles || []
}
......@@ -308,10 +299,18 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
const tracedDeps: string[] = []
for (const file of result.fileList) {
// don't include the entry itself
if (result.reasons[file].type === 'initial') {
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 = {
......@@ -354,80 +353,32 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
}
)
const resolver = compilation.resolverFactory.get('normal')
const looseEsmExternals = this.esmExternals === 'loose'
const doResolve = async (
request: string,
context: string,
isEsmRequested?: boolean
_isEsmRequested?: boolean
): Promise<string> => {
const preferEsm = isEsmRequested && this.esmExternals
const curResolver = resolver.withOptions(
preferEsm ? NODE_ESM_RESOLVE_OPTIONS : NODE_RESOLVE_OPTIONS
)
let res: string = ''
try {
res = await new Promise((resolve, reject) => {
curResolver.resolve(
{},
context,
request,
{
fileDependencies: compilation.fileDependencies,
missingDependencies: compilation.missingDependencies,
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)
return new Promise((resolve, reject) => {
resolver.resolve(
{},
context,
request,
{
fileDependencies: compilation.fileDependencies,
missingDependencies: compilation.missingDependencies,
contextDependencies: compilation.contextDependencies,
},
(err: any, result: string) => {
if (err) return reject(err)
if (!result) {
return reject(new Error('module not found'))
}
)
})
}
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,
})
resolve(result)
}
)
}
return res
})
}
this.tapfinishModules(
......
......@@ -118,8 +118,6 @@ describe('Production Usage', () => {
/node_modules\/react\/index\.js/,
/node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/,
/next\/link\.js/,
/next\/dist\/client\/link\.js/,
],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/],
},
......@@ -131,8 +129,6 @@ describe('Production Usage', () => {
/node_modules\/react\/index\.js/,
/node_modules\/react\/package\.json/,
/node_modules\/react\/cjs\/react\.production\.min\.js/,
/next\/router\.js/,
/next\/dist\/client\/router\.js/,
],
notTests: [/node_modules\/react\/cjs\/react\.development\.js/],
},
......
......@@ -96,29 +96,37 @@ export async function createNext(opts: {
nextConfig?: NextConfig
skipStart?: boolean
}): Promise<NextInstance> {
if (nextInstance) {
throw new Error(`createNext called without destroying previous instance`)
}
try {
if (nextInstance) {
throw new Error(`createNext called without destroying previous instance`)
}
if (testMode === 'dev') {
// next dev
nextInstance = new NextDevInstance(opts)
} else if (testMode === 'deploy') {
// Vercel
throw new Error('to-implement')
} else {
// next build + next start
nextInstance = new NextStartInstance(opts)
}
if (testMode === 'dev') {
// next dev
nextInstance = new NextDevInstance(opts)
} else if (testMode === 'deploy') {
// Vercel
throw new Error('to-implement')
} else {
// next build + next start
nextInstance = new NextStartInstance(opts)
}
nextInstance.on('destroy', () => {
nextInstance = undefined
})
nextInstance.on('destroy', () => {
nextInstance = undefined
})
await nextInstance.setup()
await nextInstance.setup()
if (!opts.skipStart) {
await nextInstance.start()
if (!opts.skipStart) {
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 {
}
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()}`)
if (process.env.NEXT_TEST_STARTER && !this.dependencies) {
await fs.copy(process.env.NEXT_TEST_STARTER, this.testDir)
} else {
} else if (!skipIsolatedNext) {
this.testDir = await createNextInstall({
react: 'latest',
'react-dom': 'latest',
......
import path from 'path'
import resolveFrom from 'resolve-from'
import { spawn } from 'child_process'
import { NextInstance } from './base'
......@@ -22,7 +24,9 @@ export class NextDevInstance extends NextInstance {
}
// we don't use yarn next here as yarn detaches itself from the
// 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,
stdio: ['ignore', 'pipe', 'pipe'],
shell: false,
......
import path from 'path'
import fs from 'fs-extra'
import resolveFrom from 'resolve-from'
import { spawn, SpawnOptions } from 'child_process'
import { NextInstance } from './base'
......@@ -48,10 +49,11 @@ export class NextStartInstance extends NextInstance {
this.emit('stderr', [msg])
})
}
const nextDir = path.dirname(resolveFrom(this.testDir, 'next/package.json'))
this.childProcess = spawn(
'node',
['node_modules/next/dist/bin/next', 'build'],
[path.join(nextDir, '/dist/bin/next'), 'build'],
spawnOpts
)
handleStdio()
......@@ -59,7 +61,7 @@ export class NextStartInstance extends NextInstance {
await new Promise<void>((resolve, reject) => {
this.childProcess.on('exit', (code) => {
if (code) reject(new Error(`next build failed with code ${code}`))
resolve()
else resolve()
})
})
this._buildId = (
......@@ -76,7 +78,7 @@ export class NextStartInstance extends NextInstance {
// child process making it harder to kill all processes
this.childProcess = spawn(
'node',
['node_modules/next/dist/bin/next', 'start'],
[path.join(nextDir, '/dist/bin/next'), 'start'],
spawnOpts
)
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