Skip to content

Commit 6b2a4da

Browse files
author
adrian
committed
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.
1 parent 8cb9cfc commit 6b2a4da

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/TemplateProcessor.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ export default class TemplateProcessor {
665665
if (TemplateProcessor._isNodeJS || (typeof BUILD_TARGET !== 'undefined' && BUILD_TARGET !== 'web')) {
666666
try {
667667
resp = await this.localImport(importMe);
668-
if (fileExtension === '.js' || fileExtension === '.mjs') {
668+
if (fileExtension === '.js' || fileExtension === '.mjs' || fileExtension === '.ts' || fileExtension === '.mts') {
669669
return resp; //the module is directly returned and assigned
670670
}
671671
}catch(error){
@@ -1680,6 +1680,7 @@ export default class TemplateProcessor {
16801680
const safe = this.withErrorHandling.bind(this);
16811681
for (const name of functionNames) {
16821682
try {
1683+
16831684
let generator:any = this.functionGenerators.get(name);
16841685
if (generator) {
16851686
const generated:any = await generator(metaInf, this);
@@ -1989,6 +1990,16 @@ export default class TemplateProcessor {
19891990

19901991
try {
19911992
const fileExtension = path.extname(fullpath).toLowerCase();
1993+
1994+
// Check if TypeScript files can be imported
1995+
if (fileExtension === '.ts' || fileExtension === '.mts') {
1996+
try {
1997+
// Attempt to import TypeScript file
1998+
return await import(fullpath);
1999+
} catch (e) {
2000+
throw new Error('TypeScript imports not supported in this environment');
2001+
}
2002+
}
19922003
if (fileExtension === '.js' || fileExtension === '.mjs') {
19932004
return await import(fullpath);
19942005
}

src/test/TemplateProcessor.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,8 @@ test("local import textfile with non-absolute --importPath", async () => {
13701370
});
13711371
});
13721372

1373+
1374+
13731375
test("deep view", async () => {
13741376
const template = {
13751377
"closureExpression": "/${ ($names := $distinct(data.pD.data.name); {'yAxis': [ {'categories': $names} ]}) }",
@@ -3649,4 +3651,44 @@ test("test data flow 3", async () => {
36493651
}
36503652
});
36513653

3654+
test("import js", async () => {
3655+
const tp = new TemplateProcessor({
3656+
"lib": "${$import('./test-export.js')}",
3657+
"fooResult": "${lib.foo()}",
3658+
"barResult": "${lib.bar('test input')}"
3659+
}, {}, {importPath: 'example'}
3660+
);
3661+
3662+
await tp.initialize();
3663+
3664+
// Verify the imported functions work correctly
3665+
expect(tp.output.fooResult).toBe("foo");
3666+
expect(tp.output.barResult).toBe("bar: test input");
3667+
3668+
});
3669+
3670+
test("import ts", async () => {
3671+
const tp = new TemplateProcessor({
3672+
"lib": "${$import('./test-export.ts')}",
3673+
"fooResult": "${lib.foo()}",
3674+
"barResult": "${lib.bar('test input')}"
3675+
}, {}, {importPath: 'example'}
3676+
);
3677+
3678+
await tp.initialize();
3679+
3680+
// Skip test if not running in Bun
3681+
if (process.versions.bun) {
3682+
3683+
// Verify the imported functions work correctly
3684+
expect(tp.output.fooResult).toBe("foo");
3685+
expect(tp.output.barResult).toBe("bar: test input");
3686+
3687+
} else {
3688+
// TypeScript imports are not supported in node
3689+
expect(tp.output.lib.error).toBeDefined();
3690+
}
3691+
});
3692+
3693+
36523694

0 commit comments

Comments
 (0)