Skip to content

Commit 04f3164

Browse files
committed
refactor: don't rely on MODULE_NOT_FOUND for lack of advanced API routes warning
1 parent 1b01485 commit 04f3164

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

packages/runtime/src/helpers/analysis.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,32 @@ export const extractConfigFromFile = async (apiFilePath: string, appDir: string)
8888
return {}
8989
}
9090

91-
try {
92-
if (!extractConstValue) {
93-
// eslint-disable-next-line import/no-dynamic-require
94-
extractConstValue = require(findModuleFromBase({
95-
paths: [appDir],
96-
candidates: ['next/dist/build/analysis/extract-const-value'],
97-
}) ?? 'next/dist/build/analysis/extract-const-value')
98-
}
99-
if (!parseModule) {
100-
// eslint-disable-next-line prefer-destructuring, @typescript-eslint/no-var-requires, import/no-dynamic-require
101-
parseModule = require(findModuleFromBase({
102-
paths: [appDir],
103-
candidates: ['next/dist/build/analysis/parse-module'],
104-
}) ?? 'next/dist/build/analysis/parse-module').parseModule
105-
}
106-
} catch (error) {
107-
if (error.code === 'MODULE_NOT_FOUND') {
108-
if (!hasWarnedAboutNextVersion) {
109-
console.log("This version of Next.js doesn't support advanced API routes. Skipping...")
110-
hasWarnedAboutNextVersion = true
111-
}
112-
// Old Next.js version
113-
return {}
91+
const extractConstValueModulePath = findModuleFromBase({
92+
paths: [appDir],
93+
candidates: ['next/dist/build/analysis/extract-const-value'],
94+
})
95+
96+
const parseModulePath = findModuleFromBase({
97+
paths: [appDir],
98+
candidates: ['next/dist/build/analysis/parse-module'],
99+
})
100+
101+
if (!extractConstValueModulePath || !parseModulePath) {
102+
if (!hasWarnedAboutNextVersion) {
103+
console.log("This version of Next.js doesn't support advanced API routes. Skipping...")
104+
hasWarnedAboutNextVersion = true
114105
}
115-
throw error
106+
// Old Next.js version
107+
return {}
108+
}
109+
110+
if (!extractConstValue && extractConstValueModulePath) {
111+
// eslint-disable-next-line import/no-dynamic-require
112+
extractConstValue = require(extractConstValueModulePath)
113+
}
114+
if (!parseModule && parseModulePath) {
115+
// eslint-disable-next-line prefer-destructuring, @typescript-eslint/no-var-requires, import/no-dynamic-require
116+
parseModule = require(parseModulePath).parseModule
116117
}
117118

118119
const { extractExportedConstValue, UnsupportedValueError } = extractConstValue

test/helpers/analysis.spec.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,70 +11,70 @@ describe('static source analysis', () => {
1111
;(console.error as jest.Mock).mockRestore()
1212
})
1313
it('should extract config values from a source file', async () => {
14-
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background.js'))
14+
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background.js'), process.cwd())
1515
expect(config).toEqual({
1616
type: 'experimental-background',
1717
})
1818
})
1919
it('should extract config values from a TypeScript source file', async () => {
20-
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background.ts'))
20+
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background.ts'), process.cwd())
2121
expect(config).toEqual({
2222
type: 'experimental-background',
2323
})
2424
})
2525
it('should return an empty config if not defined', async () => {
26-
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/missing.ts'))
26+
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/missing.ts'), process.cwd())
2727
expect(config).toEqual({})
2828
})
2929

3030
it('should return an empty config if config is invalid', async () => {
31-
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/invalid.ts'))
31+
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/invalid.ts'), process.cwd())
3232
expect(config).toEqual({})
3333
})
3434

3535
it('should extract schedule values from a source file', async () => {
36-
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/scheduled.ts'))
36+
const config = await extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/scheduled.ts'), process.cwd())
3737
expect(config).toEqual({
3838
type: 'experimental-scheduled',
3939
schedule: '@daily',
4040
})
4141
})
4242
it('should throw if schedule is provided when type is background', async () => {
43-
await expect(extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background-schedule.ts'))).rejects.toThrow(
44-
'Unsupported config value in test/fixtures/analysis/background-schedule.ts',
45-
)
43+
await expect(
44+
extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background-schedule.ts'), process.cwd()),
45+
).rejects.toThrow('Unsupported config value in test/fixtures/analysis/background-schedule.ts')
4646
expect(console.error).toHaveBeenCalledWith(
4747
`Invalid config value in test/fixtures/analysis/background-schedule.ts: schedule is not allowed unless type is "experimental-scheduled"`,
4848
)
4949
})
5050
it('should throw if schedule is provided when type is default', async () => {
51-
await expect(extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/default-schedule.ts'))).rejects.toThrow(
52-
'Unsupported config value in test/fixtures/analysis/default-schedule.ts',
53-
)
51+
await expect(
52+
extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/default-schedule.ts'), process.cwd()),
53+
).rejects.toThrow('Unsupported config value in test/fixtures/analysis/default-schedule.ts')
5454
expect(console.error).toHaveBeenCalledWith(
5555
`Invalid config value in test/fixtures/analysis/default-schedule.ts: schedule is not allowed unless type is "experimental-scheduled"`,
5656
)
5757
})
5858
it('should throw if schedule is not provided when type is scheduled', async () => {
59-
await expect(extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/missing-schedule.ts'))).rejects.toThrow(
60-
'Unsupported config value in test/fixtures/analysis/missing-schedule.ts',
61-
)
59+
await expect(
60+
extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/missing-schedule.ts'), process.cwd()),
61+
).rejects.toThrow('Unsupported config value in test/fixtures/analysis/missing-schedule.ts')
6262
expect(console.error).toHaveBeenCalledWith(
6363
`Invalid config value in test/fixtures/analysis/missing-schedule.ts: schedule is required when type is "experimental-scheduled"`,
6464
)
6565
})
6666
it('should throw if edge runtime is specified for scheduled functions', async () => {
67-
await expect(extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/scheduled-edge.ts'))).rejects.toThrow(
68-
'Unsupported config value in test/fixtures/analysis/scheduled-edge.ts',
69-
)
67+
await expect(
68+
extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/scheduled-edge.ts'), process.cwd()),
69+
).rejects.toThrow('Unsupported config value in test/fixtures/analysis/scheduled-edge.ts')
7070
expect(console.error).toHaveBeenCalledWith(
7171
`Invalid config value in test/fixtures/analysis/scheduled-edge.ts: edge runtime is not supported for scheduled functions`,
7272
)
7373
})
7474
it('should throw if edge runtime is specified for background functions', async () => {
75-
await expect(extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background-edge.ts'))).rejects.toThrow(
76-
'Unsupported config value in test/fixtures/analysis/background-edge.ts',
77-
)
75+
await expect(
76+
extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background-edge.ts'), process.cwd()),
77+
).rejects.toThrow('Unsupported config value in test/fixtures/analysis/background-edge.ts')
7878
expect(console.error).toHaveBeenCalledWith(
7979
`Invalid config value in test/fixtures/analysis/background-edge.ts: edge runtime is not supported for background functions`,
8080
)

0 commit comments

Comments
 (0)