feat: moduleResolution: bundler in config loaders
This commit is contained in:
@@ -27,7 +27,13 @@ const locatedConfig = getTsconfig()
|
|||||||
const tsconfig = locatedConfig.config.compilerOptions as unknown as ts.CompilerOptions
|
const tsconfig = locatedConfig.config.compilerOptions as unknown as ts.CompilerOptions
|
||||||
|
|
||||||
tsconfig.module = ts.ModuleKind.ESNext
|
tsconfig.module = ts.ModuleKind.ESNext
|
||||||
tsconfig.moduleResolution = ts.ModuleResolutionKind.NodeNext
|
|
||||||
|
// Specify bundler resolution for Next.js compatibility.
|
||||||
|
// We will use TS to resolve file paths for most flexibility
|
||||||
|
tsconfig.moduleResolution = ts.ModuleResolutionKind.Bundler
|
||||||
|
|
||||||
|
// Don't resolve d.ts files, because we aren't type-checking
|
||||||
|
tsconfig.noDtsResolution = true
|
||||||
|
|
||||||
const moduleResolutionCache = ts.createModuleResolutionCache(
|
const moduleResolutionCache = ts.createModuleResolutionCache(
|
||||||
ts.sys.getCurrentDirectory(),
|
ts.sys.getCurrentDirectory(),
|
||||||
@@ -78,16 +84,21 @@ export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// import from local project to local project TS file
|
// import from local project to local project TS file
|
||||||
if (
|
if (resolvedModule) {
|
||||||
resolvedModule &&
|
const resolvedIsNodeModule = resolvedModule.resolvedFileName.includes('/node_modules/')
|
||||||
!resolvedModule.resolvedFileName.includes('/node_modules/') &&
|
const resolvedIsTS = EXTENSIONS.includes(resolvedModule.extension)
|
||||||
EXTENSIONS.includes(resolvedModule.extension)
|
|
||||||
) {
|
if (!resolvedIsNodeModule && resolvedIsTS) {
|
||||||
return {
|
return {
|
||||||
format: 'ts',
|
format: 'ts',
|
||||||
shortCircuit: true,
|
shortCircuit: true,
|
||||||
url: pathToFileURL(resolvedModule.resolvedFileName).href,
|
url: pathToFileURL(resolvedModule.resolvedFileName).href,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We want to use TS "Bundler" moduleResolution for just about all files
|
||||||
|
// so we pass the TS result here
|
||||||
|
return nextResolve(pathToFileURL(resolvedModule.resolvedFileName).href)
|
||||||
}
|
}
|
||||||
|
|
||||||
// import from local project to either:
|
// import from local project to either:
|
||||||
|
|||||||
8
test/config-loader/int.spec.ts
Normal file
8
test/config-loader/int.spec.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { load } from './load.js'
|
||||||
|
|
||||||
|
describe('Config Loader', () => {
|
||||||
|
it('should load using TS moduleResolution: bundler', async () => {
|
||||||
|
const file = await load('./next-navigation-test.js')
|
||||||
|
expect(typeof file.redirect).toStrictEqual('function')
|
||||||
|
})
|
||||||
|
})
|
||||||
16
test/config-loader/load.ts
Normal file
16
test/config-loader/load.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { register } from 'node:module'
|
||||||
|
import path from 'node:path'
|
||||||
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
||||||
|
|
||||||
|
export const load = async (filePath) => {
|
||||||
|
const filename = fileURLToPath(import.meta.url)
|
||||||
|
const dirname = path.dirname(filename)
|
||||||
|
const url = pathToFileURL(dirname).toString() + '/'
|
||||||
|
|
||||||
|
// Need to register loader from payload/dist for a true test of functionality
|
||||||
|
register('../../packages/payload/dist/bin/register/index.js', url)
|
||||||
|
|
||||||
|
const result = await import(filePath)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
3
test/config-loader/next-navigation-test.js
Normal file
3
test/config-loader/next-navigation-test.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import { redirect } from 'next/navigation'
|
||||||
|
|
||||||
|
export { redirect }
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
"name": "next"
|
"name": "next"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@payloadcms/ui/assets": ["./packages/ui/src/assets/index.ts"],
|
"@payloadcms/ui/assets": ["./packages/ui/src/assets/index.ts"],
|
||||||
"@payloadcms/ui/elements/*": ["./packages/ui/src/elements/*/index.tsx"],
|
"@payloadcms/ui/elements/*": ["./packages/ui/src/elements/*/index.tsx"],
|
||||||
|
|||||||
@@ -164,4 +164,4 @@
|
|||||||
".next/types/**/*.ts",
|
".next/types/**/*.ts",
|
||||||
"scripts/**/*.ts"
|
"scripts/**/*.ts"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user