chore: working loader
This commit is contained in:
1
.vscode/launch.json
vendored
1
.vscode/launch.json
vendored
@@ -31,6 +31,7 @@
|
|||||||
"type": "node-terminal",
|
"type": "node-terminal",
|
||||||
"env": {
|
"env": {
|
||||||
"LOADER_TEST_FILE_PATH": "./dependency-test.js"
|
"LOADER_TEST_FILE_PATH": "./dependency-test.js"
|
||||||
|
// "LOADER_TEST_FILE_PATH": "../fields/config.ts"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -26,14 +26,10 @@ type ResolveFn = (...args: Required<ResolveArgs>) => Promise<ResolveResult>
|
|||||||
const locatedConfig = getTsconfig()
|
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
|
|
||||||
|
|
||||||
// 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
|
// Don't resolve d.ts files, because we aren't type-checking
|
||||||
tsconfig.noDtsResolution = true
|
tsconfig.noDtsResolution = true
|
||||||
|
tsconfig.module = ts.ModuleKind.ESNext
|
||||||
|
tsconfig.moduleResolution = ts.ModuleResolutionKind.NodeNext
|
||||||
|
|
||||||
const moduleResolutionCache = ts.createModuleResolutionCache(
|
const moduleResolutionCache = ts.createModuleResolutionCache(
|
||||||
ts.sys.getCurrentDirectory(),
|
ts.sys.getCurrentDirectory(),
|
||||||
@@ -55,12 +51,14 @@ export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
|
|||||||
const isTS = TS_EXTENSIONS.some((ext) => specifier.endsWith(ext))
|
const isTS = TS_EXTENSIONS.some((ext) => specifier.endsWith(ext))
|
||||||
const isClient = CLIENT_EXTENSIONS.some((ext) => specifier.endsWith(ext))
|
const isClient = CLIENT_EXTENSIONS.some((ext) => specifier.endsWith(ext))
|
||||||
|
|
||||||
|
// If a client file is resolved, we'll set `format: client`
|
||||||
|
// and short circuit, so the load step
|
||||||
|
// will return source code of empty object
|
||||||
if (isClient) {
|
if (isClient) {
|
||||||
const nextResult = await nextResolve(specifier, context, nextResolve)
|
const nextResult = await nextResolve(specifier, context, nextResolve)
|
||||||
const specifierSegments = specifier.split('.')
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
format: '.' + specifierSegments[specifierSegments.length - 1],
|
format: 'client',
|
||||||
shortCircuit: true,
|
shortCircuit: true,
|
||||||
url: nextResult.url,
|
url: nextResult.url,
|
||||||
}
|
}
|
||||||
@@ -75,9 +73,28 @@ export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// import/require from external library
|
// Try and resolve normally
|
||||||
if (context.parentURL.includes('/node_modules/') && !isTS) {
|
// This could fail, so we need to swallow that error
|
||||||
return nextResolve(specifier)
|
// and keep going
|
||||||
|
let nextResult: ResolveResult
|
||||||
|
|
||||||
|
// First, try to
|
||||||
|
if (!isTS) {
|
||||||
|
try {
|
||||||
|
nextResult = await nextResolve(specifier, context, nextResolve)
|
||||||
|
} catch (_) {
|
||||||
|
// swallow error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextResult) {
|
||||||
|
const nextResultIsTS = TS_EXTENSIONS.some((ext) => nextResult.url.endsWith(ext))
|
||||||
|
|
||||||
|
// If the next result is NOT TS, it does not need to be compiled
|
||||||
|
// so just send it on
|
||||||
|
if (!nextResultIsTS) {
|
||||||
|
return nextResult
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const { resolvedModule } = ts.resolveModuleName(
|
const { resolvedModule } = ts.resolveModuleName(
|
||||||
@@ -88,29 +105,20 @@ export const resolve: ResolveFn = async (specifier, context, nextResolve) => {
|
|||||||
moduleResolutionCache,
|
moduleResolutionCache,
|
||||||
)
|
)
|
||||||
|
|
||||||
// import from local project to local project TS file
|
|
||||||
if (resolvedModule) {
|
if (resolvedModule) {
|
||||||
const resolvedIsNodeModule = resolvedModule.resolvedFileName.includes('/node_modules/')
|
|
||||||
const resolvedIsTS = TS_EXTENSIONS.includes(resolvedModule.extension)
|
const resolvedIsTS = TS_EXTENSIONS.includes(resolvedModule.extension)
|
||||||
|
|
||||||
if (!resolvedIsNodeModule && resolvedIsTS) {
|
return {
|
||||||
return {
|
format: resolvedIsTS ? 'ts' : undefined,
|
||||||
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:
|
||||||
// - something TS couldn't resolve
|
// - something TS couldn't resolve
|
||||||
// - external library
|
|
||||||
// - local project non-TS file
|
// - local project non-TS file
|
||||||
return nextResolve(specifier)
|
return nextResolve(specifier, context, nextResolve)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface LoadContext {
|
interface LoadContext {
|
||||||
@@ -143,11 +151,11 @@ if (tsconfig.paths) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const load: LoadFn = async (url, context, nextLoad) => {
|
export const load: LoadFn = async (url, context, nextLoad) => {
|
||||||
if (CLIENT_EXTENSIONS.some((e) => context.format === e)) {
|
if (context.format === 'client') {
|
||||||
const rawSource = '{}'
|
const rawSource = 'export default {}'
|
||||||
|
|
||||||
return {
|
return {
|
||||||
format: 'json',
|
format: 'module',
|
||||||
shortCircuit: true,
|
shortCircuit: true,
|
||||||
source: rawSource,
|
source: rawSource,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { redirect } from 'next/navigation'
|
import { redirect } from 'next/navigation'
|
||||||
import { v4 as uuid } from 'uuid'
|
import { v4 as uuid } from 'uuid'
|
||||||
|
import { mongooseAdapter } from '@payloadcms/db-mongodb'
|
||||||
|
|
||||||
export { redirect, uuid }
|
export { redirect, uuid, mongooseAdapter }
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { load } from './load.js'
|
|||||||
async function init() {
|
async function init() {
|
||||||
try {
|
try {
|
||||||
const result = await load(process.env.LOADER_TEST_FILE_PATH)
|
const result = await load(process.env.LOADER_TEST_FILE_PATH)
|
||||||
console.log(result)
|
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
|||||||
@@ -27,21 +27,21 @@
|
|||||||
],
|
],
|
||||||
"baseUrl": ".",
|
"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"],
|
||||||
"@payloadcms/ui/fields/*": ["./packages/ui/src/fields/*/index.tsx"],
|
"@payloadcms/ui/fields/*": ["../packages/ui/src/fields/*/index.tsx"],
|
||||||
"@payloadcms/ui/forms/*": ["./packages/ui/src/forms/*/index.tsx"],
|
"@payloadcms/ui/forms/*": ["../packages/ui/src/forms/*/index.tsx"],
|
||||||
"@payloadcms/ui/graphics/*": ["./packages/ui/src/graphics/*/index.tsx"],
|
"@payloadcms/ui/graphics/*": ["../packages/ui/src/graphics/*/index.tsx"],
|
||||||
"@payloadcms/ui/hooks/*": ["./packages/ui/src/hooks/*.ts"],
|
"@payloadcms/ui/hooks/*": ["../packages/ui/src/hooks/*.ts"],
|
||||||
"@payloadcms/ui/icons/*": ["./packages/ui/src/icons/*/index.tsx"],
|
"@payloadcms/ui/icons/*": ["../packages/ui/src/icons/*/index.tsx"],
|
||||||
"@payloadcms/ui/providers/*": ["./packages/ui/src/providers/*/index.tsx"],
|
"@payloadcms/ui/providers/*": ["../packages/ui/src/providers/*/index.tsx"],
|
||||||
"@payloadcms/ui/templates/*": ["./packages/ui/src/templates/*/index.tsx"],
|
"@payloadcms/ui/templates/*": ["../packages/ui/src/templates/*/index.tsx"],
|
||||||
"@payloadcms/ui/utilities/*": ["./packages/ui/src/utilities/*.ts"],
|
"@payloadcms/ui/utilities/*": ["../packages/ui/src/utilities/*.ts"],
|
||||||
"@payloadcms/ui/scss": ["./packages/ui/src/scss.scss"],
|
"@payloadcms/ui/scss": ["../packages/ui/src/scss.scss"],
|
||||||
"@payloadcms/ui/scss/app.scss": ["./packages/ui/src/scss/app.scss"],
|
"@payloadcms/ui/scss/app.scss": ["../packages/ui/src/scss/app.scss"],
|
||||||
"payload/types": ["./packages/payload/src/exports/types/index.ts"],
|
"payload/types": ["../packages/payload/src/exports/types/index.ts"],
|
||||||
"@payloadcms/next/*": ["./packages/next/src/*"],
|
"@payloadcms/next/*": ["../packages/next/src/*"],
|
||||||
"@payloadcms/next": ["./packages/next/src/exports/*"],
|
"@payloadcms/next": ["../packages/next/src/exports/*"],
|
||||||
"@payload-config": ["./_community/config.ts"]
|
"@payload-config": ["./_community/config.ts"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -36,9 +36,6 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"paths": {
|
"paths": {
|
||||||
"payload/types": [
|
|
||||||
"./packages/payload/src/exports/types.ts"
|
|
||||||
],
|
|
||||||
"@payload-config": [
|
"@payload-config": [
|
||||||
"./test/_community/config.ts"
|
"./test/_community/config.ts"
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user