diff --git a/example/test-export.ts b/example/test-export.ts new file mode 100644 index 00000000..e3a20c98 --- /dev/null +++ b/example/test-export.ts @@ -0,0 +1,8 @@ +const barFunc = (input: string): string => `bar: ${input}`; + +// explicitly define exported functions and their names +export const foo = (): string => "foo"; +export const bar = barFunc; +export const __init = (templateProcessor: { setData: (path: string, value: string) => void }): void => { + templateProcessor.setData("/messageFromInitFunction", "__init sidecar succeeded"); +} diff --git a/src/TemplateProcessor.ts b/src/TemplateProcessor.ts index a1f7dad8..19f47aa1 100644 --- a/src/TemplateProcessor.ts +++ b/src/TemplateProcessor.ts @@ -699,7 +699,7 @@ export default class TemplateProcessor { if (TemplateProcessor._isNodeJS || (typeof BUILD_TARGET !== 'undefined' && BUILD_TARGET !== 'web')) { try { resp = await this.localImport(importMe); - if (fileExtension === '.js' || fileExtension === '.mjs') { + if (fileExtension === '.js' || fileExtension === '.mjs' || fileExtension === '.ts' || fileExtension === '.mts') { return resp; //the module is directly returned and assigned } }catch(error){ @@ -1415,6 +1415,7 @@ export default class TemplateProcessor { const safe = this.withErrorHandling.bind(this); for (const name of functionNames) { try { + let generator:any = this.functionGenerators.get(name); if (generator) { const generated:any = await generator(metaInf, this); @@ -1635,6 +1636,16 @@ export default class TemplateProcessor { try { const fileExtension = path.extname(fullpath).toLowerCase(); + + // Check if TypeScript files can be imported + if (fileExtension === '.ts' || fileExtension === '.mts') { + try { + // Attempt to import TypeScript file + return await import(fullpath); + } catch (e) { + throw new Error('TypeScript imports not supported in this environment'); + } + } if (fileExtension === '.js' || fileExtension === '.mjs') { return await import(fullpath); } diff --git a/src/test/TemplateProcessor.test.js b/src/test/TemplateProcessor.test.js index 961507ee..62b68ade 100644 --- a/src/test/TemplateProcessor.test.js +++ b/src/test/TemplateProcessor.test.js @@ -5694,6 +5694,41 @@ test("test import with props", async () => { } }); +test("import js", async () => { + const tp = new TemplateProcessor({ + "lib": "${$import('./test-export.js')}", + "fooResult": "${lib.foo()}", + "barResult": "${lib.bar('test input')}" + }, {}, {importPath: 'example'} + ); + + await tp.initialize(); + + // Verify the imported functions work correctly + expect(tp.output.fooResult).toBe("foo"); + expect(tp.output.barResult).toBe("bar: test input"); + +}); - - +test("import ts", async () => { + const tp = new TemplateProcessor({ + "lib": "${$import('./test-export.ts')}", + "fooResult": "${lib.foo()}", + "barResult": "${lib.bar('test input')}" + }, {}, {importPath: 'example'} + ); + + await tp.initialize(); + + // Skip test if not running in Bun + if (process.versions.bun) { + + // Verify the imported functions work correctly + expect(tp.output.fooResult).toBe("foo"); + expect(tp.output.barResult).toBe("bar: test input"); + + } else { + // TypeScript imports are not supported in node + expect(tp.output.lib.error).toBeDefined(); + } +});