Skip to content
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

Add typescript import #98

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions example/test-export.ts
Original file line number Diff line number Diff line change
@@ -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");
}
13 changes: 12 additions & 1 deletion src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
39 changes: 37 additions & 2 deletions src/test/TemplateProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});