Skip to content

Commit 1774f47

Browse files
committed
refactor: get rid of unneded new helper
1 parent e4fadf4 commit 1774f47

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

packages/runtime/src/helpers/analysis.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import fs, { existsSync } from 'fs'
22

33
import { relative } from 'pathe'
44

5-
import { getNextModulePath } from './files'
65
import { ApiRouteType } from './types'
6+
import { findModuleFromBase } from './utils'
77

88
export interface ApiStandardConfig {
99
type?: never
@@ -92,11 +92,17 @@ export const extractConfigFromFile = async (apiFilePath: string, appDir: string)
9292
try {
9393
if (!extractConstValue) {
9494
// eslint-disable-next-line import/no-dynamic-require
95-
extractConstValue = require(getNextModulePath(appDir, ['next/dist/build/analysis/extract-const-value']))
95+
extractConstValue = require(findModuleFromBase({
96+
paths: [appDir],
97+
candidates: ['next/dist/build/analysis/extract-const-value'],
98+
}))
9699
}
97100
if (!parseModule) {
98101
// eslint-disable-next-line prefer-destructuring, @typescript-eslint/no-var-requires, import/no-dynamic-require
99-
parseModule = require(getNextModulePath(appDir, ['next/dist/build/analysis/parse-module'])).parseModule
102+
parseModule = require(findModuleFromBase({
103+
paths: [appDir],
104+
candidates: ['next/dist/build/analysis/parse-module'],
105+
})).parseModule
100106
}
101107
} catch (error) {
102108
if (error.code === 'MODULE_NOT_FOUND') {

packages/runtime/src/helpers/files.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,21 +338,7 @@ export const getServerFile = (root: string, includeBase = true) => {
338338
candidates.unshift('next/dist/server/base-server')
339339
}
340340

341-
return getNextModulePath(root, candidates)
342-
}
343-
344-
export const getNextModulePath = (root: string, candidates: Array<string>): string | null => {
345-
const module = findModuleFromBase({ candidates, paths: [root] })
346-
if (module) {
347-
return module
348-
}
349-
350-
for (const candidate of candidates) {
351-
try {
352-
return require.resolve(candidate)
353-
} catch {}
354-
}
355-
return null
341+
return findModuleFromBase({ candidates, paths: [root] })
356342
}
357343

358344
/**

packages/runtime/src/helpers/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ export const findModuleFromBase = ({ paths, candidates }): string | null => {
267267
// Ignore the error
268268
}
269269
}
270+
// if we couldn't find a module from paths, let's try to resolve from here
271+
for (const candidate of candidates) {
272+
try {
273+
const modulePath = require.resolve(candidate)
274+
if (modulePath) {
275+
return modulePath
276+
}
277+
} catch {
278+
// Ignore the error
279+
}
280+
}
270281
return null
271282
}
272283

0 commit comments

Comments
 (0)