Skip to content

Commit 02b1efa

Browse files
authored
Merge pull request #79 from ro-savage/add-js-support
Add ability to handle import of .ts or .js files, closes #78
2 parents e137a99 + f1a3960 commit 02b1efa

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

src/typescript.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function makeDefaultTypescriptConfig() {
99
preserveConstEnums: true,
1010
strictNullChecks: true,
1111
sourceMap: true,
12+
allowJs: true,
1213
target: ts.ScriptTarget.ES5,
1314
moduleResolution: ts.ModuleResolutionKind.NodeJs,
1415
lib: ['lib.es2015.d.ts'],
@@ -53,7 +54,21 @@ export function extractFileNames(cwd: string, provider: string, functions?: { [k
5354
const fnName = _.last(h.split('.'))
5455
const fnNameLastAppearanceIndex = h.lastIndexOf(fnName)
5556
// replace only last instance to allow the same name for file and handler
56-
return h.substring(0, fnNameLastAppearanceIndex) + 'ts'
57+
const fileName = h.substring(0, fnNameLastAppearanceIndex)
58+
59+
// Check if the .ts files exists. If so return that to watch
60+
if (fs.existsSync(path.join(cwd, fileName + 'ts'))) {
61+
return fileName + 'ts'
62+
}
63+
64+
// Check if the .js files exists. If so return that to watch
65+
if (fs.existsSync(path.join(cwd, fileName + 'js'))) {
66+
return fileName + 'js'
67+
}
68+
69+
// Can't find the files. Watch will have an exception anyway. So throw one with error.
70+
console.log(`Cannot locate handler - ${fileName} not found`)
71+
throw new Error('Typescript compilation failed. Please ensure handlers exists with ext .ts or .js')
5772
})
5873
}
5974

tests/assets/hello.ts

Whitespace-only changes.

tests/assets/jsfile.js

Whitespace-only changes.

tests/assets/world.ts

Whitespace-only changes.

tests/typescript.extractFileName.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import * as path from 'path'
44

55
const functions: { [key: string]: ServerlessFunction } = {
66
hello: {
7-
handler: 'my-folder/hello.handler',
7+
handler: 'tests/assets/hello.handler',
88
package: {
99
include: [],
1010
exclude: []
1111
}
1212
},
1313
world: {
14-
handler: 'my-folder/my-subfolder/world.handler',
14+
handler: 'tests/assets/world.handler',
1515
package: {
1616
include: [],
1717
exclude: []
1818
}
1919
},
20-
create: {
21-
handler: 'create.create',
20+
js: {
21+
handler: 'tests/assets/jsfile.create',
2222
package: {
2323
include: [],
2424
exclude: []
@@ -32,9 +32,9 @@ describe('extractFileName', () => {
3232
extractFileNames(process.cwd(), 'aws', functions),
3333
).toEqual(
3434
[
35-
'my-folder/hello.ts',
36-
'my-folder/my-subfolder/world.ts',
37-
'create.ts',
35+
'tests/assets/hello.ts',
36+
'tests/assets/world.ts',
37+
'tests/assets/jsfile.js',
3838
],
3939
)
4040
})

0 commit comments

Comments
 (0)