Skip to content

Commit

Permalink
allow the import of ts where ts is allowed.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adrian committed Jan 15, 2025
1 parent 8cb9cfc commit 6b2a4da
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,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 @@ -1680,6 +1680,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 @@ -1989,6 +1990,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
42 changes: 42 additions & 0 deletions src/test/TemplateProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,8 @@ test("local import textfile with non-absolute --importPath", async () => {
});
});



test("deep view", async () => {
const template = {
"closureExpression": "/${ ($names := $distinct(data.pD.data.name); {'yAxis': [ {'categories': $names} ]}) }",
Expand Down Expand Up @@ -3649,4 +3651,44 @@ test("test data flow 3", 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();
}
});



0 comments on commit 6b2a4da

Please sign in to comment.