Skip to content

Commit

Permalink
feat: Added ts-import support to fetch & transpile Typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrydn committed Mar 18, 2024
1 parent 008c02e commit bdb1580
Show file tree
Hide file tree
Showing 4 changed files with 2,603 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.nyc_output/
.ts-import-cache/
coverage/
node_modules/
7 changes: 6 additions & 1 deletion lib/handlers.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import createDebug from 'debug';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { load as tsImport } from 'ts-import';

import { assert } from './utils.mjs';

const cacheDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '../.ts-import-cache');
const debug = createDebug('aws-lambda-rie-http');

/**
Expand Down Expand Up @@ -35,7 +38,9 @@ export async function getHandler(handlerName) {
debug(filePath);
assert(filePath, new Error(`File not found: ${fileName}.(js|cjs|mjs|ts)`));

const handler = await import(filePath);
const handler = filePath.endsWith('.ts')
? await tsImport(filePath, { mode: 'transpile', transpileOptions: { cache: { dir: cacheDir } } })
: await import(filePath);

if (handler && handler.default && typeof handler.default[handlerFnName] === 'function') {
// module.exports.handler = function handler() {}
Expand Down
Loading

0 comments on commit bdb1580

Please sign in to comment.