Skip to content

Commit d19a3be

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 4b50653 commit d19a3be

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

Diff for: src/TemplateProcessor.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ export default class TemplateProcessor {
699699
if (TemplateProcessor._isNodeJS || (typeof BUILD_TARGET !== 'undefined' && BUILD_TARGET !== 'web')) {
700700
try {
701701
resp = await this.localImport(importMe);
702-
if (fileExtension === '.js' || fileExtension === '.mjs') {
702+
if (fileExtension === '.js' || fileExtension === '.mjs' || fileExtension === '.ts' || fileExtension === '.mts') {
703703
return resp; //the module is directly returned and assigned
704704
}
705705
}catch(error){
@@ -1415,6 +1415,7 @@ export default class TemplateProcessor {
14151415
const safe = this.withErrorHandling.bind(this);
14161416
for (const name of functionNames) {
14171417
try {
1418+
14181419
let generator:any = this.functionGenerators.get(name);
14191420
if (generator) {
14201421
const generated:any = await generator(metaInf, this);
@@ -1635,6 +1636,16 @@ export default class TemplateProcessor {
16351636

16361637
try {
16371638
const fileExtension = path.extname(fullpath).toLowerCase();
1639+
1640+
// Check if TypeScript files can be imported
1641+
if (fileExtension === '.ts' || fileExtension === '.mts') {
1642+
try {
1643+
// Attempt to import TypeScript file
1644+
return await import(fullpath);
1645+
} catch (e) {
1646+
throw new Error('TypeScript imports not supported in this environment');
1647+
}
1648+
}
16381649
if (fileExtension === '.js' || fileExtension === '.mjs') {
16391650
return await import(fullpath);
16401651
}

Diff for: src/test/TemplateProcessor.test.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -5694,6 +5694,41 @@ test("test import with props", async () => {
56945694
}
56955695
});
56965696

5697+
test("import js", async () => {
5698+
const tp = new TemplateProcessor({
5699+
"lib": "${$import('./test-export.js')}",
5700+
"fooResult": "${lib.foo()}",
5701+
"barResult": "${lib.bar('test input')}"
5702+
}, {}, {importPath: 'example'}
5703+
);
5704+
5705+
await tp.initialize();
5706+
5707+
// Verify the imported functions work correctly
5708+
expect(tp.output.fooResult).toBe("foo");
5709+
expect(tp.output.barResult).toBe("bar: test input");
5710+
5711+
});
56975712

5698-
5699-
5713+
test("import ts", async () => {
5714+
const tp = new TemplateProcessor({
5715+
"lib": "${$import('./test-export.ts')}",
5716+
"fooResult": "${lib.foo()}",
5717+
"barResult": "${lib.bar('test input')}"
5718+
}, {}, {importPath: 'example'}
5719+
);
5720+
5721+
await tp.initialize();
5722+
5723+
// Skip test if not running in Bun
5724+
if (process.versions.bun) {
5725+
5726+
// Verify the imported functions work correctly
5727+
expect(tp.output.fooResult).toBe("foo");
5728+
expect(tp.output.barResult).toBe("bar: test input");
5729+
5730+
} else {
5731+
// TypeScript imports are not supported in node
5732+
expect(tp.output.lib.error).toBeDefined();
5733+
}
5734+
});

0 commit comments

Comments
 (0)