Skip to content

Commit

Permalink
Undo "node16" module resolution, manually attempt both with import_
Browse files Browse the repository at this point in the history
This basically will attempt CJS with `require()` and ESM with `import()`
separately instead of doing both with `import()`.

While `import()` does support both, it gets more complicated when trying
to load TS files with ts-node, which requires specific loader config
instead of working out of the box.
  • Loading branch information
jacobmischka committed Mar 29, 2023
1 parent a629a5e commit 6a92466
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@interval/sdk",
"version": "1.0.0-dev0",
"version": "1.0.0-dev1",
"description": "The frontendless framework for high growth companies. Interval automatically generates apps by inlining the UI in your backend code. It's a faster and more maintainable way to build internal tools, rapid prototypes, and more.",
"homepage": "https://interval.com",
"repository": {
Expand All @@ -24,6 +24,7 @@
"dev": "run(){ yarn build:envoy && nodemon --watch src -e ts src/examples/${1:-basic}/index.ts; }; run"
},
"dependencies": {
"@brillout/import": "^0.2.2",
"cross-fetch": "^3.1.5",
"evt": "^2.4.10",
"superjson": "^1.9.1",
Expand Down
4 changes: 2 additions & 2 deletions src/classes/IntervalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export default class IntervalClient {
if (typeof window === 'undefined' && this.#config.routesDirectory) {
try {
const { loadRoutesFromFileSystem } = await import(
'../utils/fileActionLoader.js'
'../utils/fileActionLoader'
)
fileSystemRoutes = await loadRoutesFromFileSystem(
this.#config.routesDirectory,
Expand Down Expand Up @@ -826,7 +826,7 @@ export default class IntervalClient {
switch (inputs.type) {
case 'offer': {
const { DataChannelConnection } = await import(
'./DataChannelConnection.js'
'./DataChannelConnection'
)

const iceConfig = await this.#interval.fetchIceConfig()
Expand Down
80 changes: 50 additions & 30 deletions src/utils/fileActionLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import path from 'path'
import fsRoot from 'fs'
import { import_ } from '@brillout/import'

import Action from '../classes/Action'
import Page from '../classes/Page'
Expand All @@ -28,43 +29,62 @@ async function loadFolder(currentDirectory: string, logger: Logger) {

const ext = path.extname(file)
const slug = path.basename(file, ext || undefined)

const attemptLoadRoute = (fileExports: any) => {
if (slug === 'index') {
if ('default' in fileExports) {
let defaultExport = fileExports.default
if ('default' in defaultExport) {
defaultExport = defaultExport.default
}

if (defaultExport instanceof Page) {
Object.assign(defaultExport.routes, router.routes)
router = defaultExport
} else {
logger.warn(
`Default export of ${fullPath} is not a Page class instance, skipping.`
)
}
}
} else {
if ('default' in fileExports) {
let defaultExport = fileExports.default
if ('default' in defaultExport) {
defaultExport = defaultExport.default
}

if (
defaultExport instanceof Page ||
defaultExport instanceof Action
) {
router.routes[slug] = defaultExport
} else {
logger.warn(
`Default export of ${fullPath} is not a Page or Action class instance, skipping.`
)
}
}
}
}

if ((await fs.stat(fullPath)).isDirectory()) {
const group = await loadFolder(path.join(currentDirectory, slug), logger)
router.routes[slug] = group
} else if (ext === '.ts' || ext === '.js') {
} else if (ext === '.ts' || ext === '.js' || ext === '.mjs') {
try {
const fileExports = await import(fullPath)

if (slug === 'index') {
if ('default' in fileExports) {
if (fileExports.default instanceof Page) {
Object.assign(fileExports.default.routes, router.routes)
router = fileExports.default
} else {
logger.warn(
`Default export of ${fullPath} is not a Page class instance, skipping.`
)
}
}
} else {
if ('default' in fileExports) {
if (
fileExports.default instanceof Page ||
fileExports.default instanceof Action
) {
router.routes[slug] = fileExports.default
} else {
logger.warn(
`Default export of ${fullPath} is not a Page or Action class instance, skipping.`
)
}
}
}
attemptLoadRoute(await import(fullPath))
} catch (err) {
logger.warn(
`Failed loading file at ${fullPath} as module, skipping.`,
logger.debug(
`Failed loading file at ${fullPath} as CommonJS, trying again as module.`,
err
)

try {
attemptLoadRoute(await import_(fullPath))
} catch (err) {
logger.debug(`Failed loading file at ${fullPath}, skipping.`, err)
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"target": "ES2019",
"declaration": true,
"esModuleInterop": true,
"moduleResolution": "Node16",
"strict": true,
"strictNullChecks": true,
"rootDir": "./src"
Expand Down

0 comments on commit 6a92466

Please sign in to comment.