Skip to content

[OLD dont merge] playing around #4767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
925 changes: 0 additions & 925 deletions .yarn/releases/yarn-4.5.0.cjs

This file was deleted.

935 changes: 935 additions & 0 deletions .yarn/releases/yarn-4.7.0.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ enableGlobalCache: false

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.5.0.cjs
yarnPath: .yarn/releases/yarn-4.7.0.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"packageManager": "yarn@4.5.0",
"packageManager": "yarn@4.7.0",
"workspaces": [
"packages/*"
],
Expand Down
1 change: 1 addition & 0 deletions packages/server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# server
3 changes: 2 additions & 1 deletion packages/server/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

// Conditionally change appRoot and packageRoot according to whether we're running from /dist/ or not (ts-node)
const path = require('path')
const isTsNode = !!process[Symbol.for('ts-node.register.instance')]
const isTsNode =
!!process[Symbol.for('ts-node.register.instance')] || process.env.VITEST === 'true'
const appRoot = __dirname
const packageRoot = isTsNode ? appRoot : path.resolve(__dirname, '../')

Expand Down
83 changes: 83 additions & 0 deletions packages/server/bootstrapTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const Module = require('module')

// Define your custom extensions to attempt in order
const EXTENSIONS_TO_TRY = ['.js', '.ts', '/index.js', '/index.ts']
const aliases = ['@/', '#/']

// const originalLoad = Module._load
// Module._load = function (request, parent, isMain) {
// const load = (newRequest) => originalLoad(newRequest || request, parent, isMain)
// const isCannotFind = (e) => e.message.includes('Cannot find module')

// const isOurAlias = aliases.some((alias) => request.startsWith(alias))
// if (!isOurAlias) {
// return load()
// }

// // Try as is
// let throwableError = undefined
// try {
// const res = load()
// return res
// } catch (e) {
// const cannotFindModule = isCannotFind(e)
// if (!cannotFindModule || EXTENSIONS_TO_TRY.some((ext) => request.endsWith(ext))) {
// throw e
// }

// throwableError = e
// }

// // Now lets see if we can resolve it with our custom extensions
// for (const ext of EXTENSIONS_TO_TRY) {
// try {
// return load(request + ext)
// } catch (e) {
// if (!throwableError || !isCannotFind(e)) {
// throwableError = e
// }
// }
// }

// throw throwableError || new Error('Fail')
// }

// Same version but with _resolveFilename
const originalResolveFilename = Module._resolveFilename
Module._resolveFilename = function (request, parent, isMain, options) {
const load = (newRequest) =>
originalResolveFilename(newRequest || request, parent, isMain, options)
const isCannotFind = (e) => e.message.includes('Cannot find module')

const isOurAlias = aliases.some((alias) => request.startsWith(alias))
if (!isOurAlias) {
return load()
}

// Try as is
let throwableError = undefined
try {
const res = load()
return res
} catch (e) {
const cannotFindModule = isCannotFind(e)
if (!cannotFindModule || EXTENSIONS_TO_TRY.some((ext) => request.endsWith(ext))) {
throw e
}

throwableError = e
}

// Now lets see if we can resolve it with our custom extensions
for (const ext of EXTENSIONS_TO_TRY) {
try {
return load(request + ext)
} catch (e) {
if (!throwableError || !isCannotFind(e)) {
throwableError = e
}
}
}

throw throwableError || new Error('Fail')
}
2 changes: 1 addition & 1 deletion packages/server/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const configs = [
}
},
{
files: ['**/*.mjs'],
files: ['**/*.mjs', 'esmLoader.js', 'vitest.config.js'],
languageOptions: {
sourceType: 'module'
}
Expand Down
90 changes: 90 additions & 0 deletions packages/server/esmLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import { register } from 'node:module'
import { appRoot, packageRoot } from './root.cjs'

/**
* Must be invoked through --import when running the node app to set up path aliases
* and extensionless imports
*/

/**
* PATH ALIAS DEFINITIONS
*/
const aliases = {
'@/': appRoot + '/',
'#/': packageRoot + '/'
}

const packageAliases = {
lodash: 'lodash-es'
}

/**
* 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
}
}
for (const [alias, target] of Object.entries(packageAliases)) {
if (specifier === alias) {
return target
}
}
return null // No alias found, fall back to default resolution
}

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
}

export { packageRoot, appRoot }
Loading