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 4b50653 commit d19a3be
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
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");

Check failure on line 5727 in src/test/TemplateProcessor.test.js

View workflow job for this annotation

GitHub Actions / test-bun

error: expect(received).toBe(expected)

Expected: "foo" Received: { error: { name: "JSONata evaluation exception", message: "Attempted to invoke a non-function", }, } at <anonymous> (/home/runner/work/stated/stated/src/test/TemplateProcessor.test.js:5727:37)
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 d19a3be

Please sign in to comment.