From d19a3bee48215f35e87c61e148a74df9fbb6b6c6 Mon Sep 17 00:00:00 2001 From: adrian Date: Tue, 14 Jan 2025 16:20:03 -0800 Subject: [PATCH 1/2] allow the import of ts where ts is allowed. test: imports ts on bun and test catch error in npm feat(TemplateProcessor): add support for importing TypeScript files fix(TemplateProcessor): handle TypeScript imports with error handling test(TemplateProcessor): add tests for importing JavaScript and TypeScript files Add support for importing TypeScript files in environments that support it, and provide error handling for environments that do not. Add tests to verify the functionality of importing both JavaScript and TypeScript files. --- src/TemplateProcessor.ts | 13 +++++++++- src/test/TemplateProcessor.test.js | 39 ++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) 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(); + } +}); From 54471198828a0451d06759870072a9a69c6a9808 Mon Sep 17 00:00:00 2001 From: adrian Date: Thu, 16 Jan 2025 06:53:42 -0800 Subject: [PATCH 2/2] include type script file --- example/test-export.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 example/test-export.ts 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"); +}