Skip to content

Commit 6adc892

Browse files
Copilotkobenguyent
andcommitted
Fix TypeScript import detection for files with dots in name
Fixed issue where TypeScript files with dots in their names (e.g., abstract.helper.ts) were not being detected and transpiled when imported without extension. The problem was that path.extname() would consider the last dot as a file extension. Now the transpiler: - Checks if an import has a standard module extension (.js, .mjs, .cjs, .json, .node) - If not, tries adding .ts extension to find and transpile TypeScript files - This correctly handles files like abstract.helper.ts, material.component.helper.ts, etc. Added test case to verify this scenario works correctly. Co-authored-by: kobenguyent <7845001+kobenguyent@users.noreply.github.com>
1 parent 37878c1 commit 6adc892

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

lib/utils/typescript.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ const __dirname = __dirname_fn(__filename);
142142
}
143143
}
144144

145-
// Try adding .ts extension if file doesn't exist and no extension provided
146-
if (!path.extname(importedPath)) {
145+
// Check for standard module extensions to determine if we should try adding .ts
146+
const ext = path.extname(importedPath)
147+
const standardExtensions = ['.js', '.mjs', '.cjs', '.json', '.node']
148+
const hasStandardExtension = standardExtensions.includes(ext.toLowerCase())
149+
150+
// If it doesn't end with .ts and doesn't have a standard extension, try adding .ts
151+
if (!importedPath.endsWith('.ts') && !hasStandardExtension) {
147152
const tsPath = importedPath + '.ts'
148153
if (fs.existsSync(tsPath)) {
149154
importedPath = tsPath
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export abstract class AbstractHelper {
2+
protected config: any;
3+
constructor(config?: any) {
4+
this.config = config;
5+
}
6+
abstract customMethod(): string;
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { AbstractHelper } from "./abstract.helper";
2+
3+
class MaterialComponentHelper extends AbstractHelper {
4+
customMethod(): string {
5+
return "Material component works";
6+
}
7+
}
8+
9+
export default MaterialComponentHelper;

test/unit/container_test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,22 @@ describe('Container', () => {
386386
expect(helper.customMethod()).to.eql('[Helper] Material component helper loaded')
387387
expect(helper.clickButton).to.be.a('function')
388388
})
389+
390+
it('should load TypeScript helper with dots in filename that imports another file with dots', async () => {
391+
const tsHelperPath = path.join(__dirname, '../data/typescript-support/material.component.helper.ts')
392+
await container.create({
393+
helpers: {
394+
MaterialComponentHelper: {
395+
require: tsHelperPath,
396+
},
397+
},
398+
})
399+
await container.started()
400+
401+
const helper = container.helpers('MaterialComponentHelper')
402+
expect(helper).to.be.ok
403+
expect(helper.customMethod).to.be.a('function')
404+
expect(helper.customMethod()).to.eql('Material component works')
405+
})
389406
})
390407
})

0 commit comments

Comments
 (0)