bde148f286
* wip * some extra fixes * stuff kinda works? * need to figure out mocks * need to figure out mocks * fix db listener * gqlgen fix * minor gqlgen watch adjustment * lint fixes * delete old codegen file * converting migrations to ESM * getModuleDIrectory * vitest sort of works * added back ts-vitest * resolve gql double load * fixing test timeout configs * TSC lint fix * fix automate tests * moar debugging * debugging * more debugging * codegen update * server works * yargs migrated * chore(server): getting rid of global mocks for Server ESM (#5046) * got rid of email mock * got rid of comment mocks * got rid of multi region mocks * got rid of stripe mock * admin override mock updated * removed final mock * fixing import.meta.resolve calls * another import.meta.resolve fix * added requested test * nyc ESM fix * removed unneeded deps + linting * yarn lock forgot to commit * tryna fix flakyness * email capture util fix * sendEmail fix * fix TSX check * sender transporter fix + CR comments * merge main fix * test fixx * circleci fix * gqlgen bigint fix * error formatter fix * more error formatting improvements * esmloader added to Dockerfile * more dockerfile fixes * bg jobs fix
85 lines
2.1 KiB
JavaScript
85 lines
2.1 KiB
JavaScript
import path from 'node:path'
|
|
import { pathToFileURL } from 'node:url'
|
|
import { register } from 'node:module'
|
|
import { appRoot, packageRoot } from './root.js'
|
|
|
|
/**
|
|
* Must be invoked through --import when running the node app to set up the following:
|
|
* - Custom path aliases for imports
|
|
* - Extensionless imports
|
|
* - Directory imports like in CJS
|
|
*/
|
|
|
|
/**
|
|
* PATH ALIAS DEFINITIONS
|
|
*/
|
|
const aliases = {
|
|
'@/': appRoot + '/',
|
|
'#/': packageRoot + '/'
|
|
}
|
|
|
|
/**
|
|
* EXTENSIONS TO EVALUATE FOR EXTENSIONLESS IMPORTS
|
|
*/
|
|
const extensions = ['.js', '.mjs', '.cjs', '.json']
|
|
|
|
// Register the module hooks
|
|
register('./esmLoader.js', {
|
|
parentURL: import.meta.url
|
|
})
|
|
|
|
// Custom path resolver
|
|
function resolveAlias(specifier) {
|
|
for (const [alias, target] of Object.entries(aliases)) {
|
|
if (specifier.startsWith(alias)) {
|
|
const relativePath = specifier.replace(alias, target)
|
|
return pathToFileURL(path.resolve(relativePath)).href
|
|
}
|
|
}
|
|
return null // No alias found, fall back to default resolution
|
|
}
|
|
|
|
/**
|
|
* Adjust global ESM resolution logic to allow for path/package aliases, dir imports and extensionless imports
|
|
*/
|
|
export async function resolve(specifier, _context, nextResolve) {
|
|
// Resolve alias
|
|
const aliasResolved = resolveAlias(specifier)
|
|
specifier = aliasResolved || specifier
|
|
|
|
// Try to resolve as is
|
|
let throwableError = undefined
|
|
try {
|
|
return await nextResolve(specifier)
|
|
} catch (e) {
|
|
throwableError = e
|
|
}
|
|
|
|
const isDirImport = throwableError.code === 'ERR_UNSUPPORTED_DIR_IMPORT'
|
|
|
|
// Didn't work, try with extensions
|
|
for (const ext of extensions) {
|
|
try {
|
|
return await nextResolve(specifier + ext)
|
|
} catch (e) {
|
|
if (!throwableError) {
|
|
throwableError = e
|
|
}
|
|
}
|
|
}
|
|
|
|
// If it was a dir import also, try that with extensions
|
|
specifier = isDirImport ? path.join(specifier, 'index') : specifier
|
|
for (const ext of extensions) {
|
|
try {
|
|
return await nextResolve(specifier + ext)
|
|
} catch (e) {
|
|
if (!throwableError) {
|
|
throwableError = e
|
|
}
|
|
}
|
|
}
|
|
|
|
throw throwableError
|
|
}
|