Skip to content

Commit

Permalink
feat(lambda-nodejs): allow jsx and tsx entry files (aws#8892)
Browse files Browse the repository at this point in the history
Support JSX adn TSX for entry files

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold authored Jul 6, 2020
1 parent 5e43348 commit 4ba20fd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ automatically transpiled and bundled whether it's written in JavaScript or TypeS
Alternatively, an entry file and handler can be specified:
```ts
new lambda.NodejsFunction(this, 'MyFunction', {
entry: '/path/to/my/file.ts',
entry: '/path/to/my/file.ts', // accepts .js, .jsx, .ts and .tsx files
handler: 'myExportedFunc'
});
```
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class NodejsFunction extends lambda.Function {
*/
function findEntry(id: string, entry?: string): string {
if (entry) {
if (!/\.(js|ts)$/.test(entry)) {
if (!/\.(jsx?|tsx?)$/.test(entry)) {
throw new Error('Only JavaScript or TypeScript entry files are supported.');
}
if (!fs.existsSync(entry)) {
Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/test/function.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import '@aws-cdk/assert/jest';
import { Runtime } from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import * as fs from 'fs';
import * as path from 'path';
import { NodejsFunction } from '../lib';
import { Bundling } from '../lib/bundling';

Expand Down Expand Up @@ -67,6 +69,18 @@ test('throws when entry is not js/ts', () => {
})).toThrow(/Only JavaScript or TypeScript entry files are supported/);
});

test('accepts tsx', () => {
const entry = path.join(__dirname, 'handler.tsx');

fs.symlinkSync(path.join(__dirname, 'function.test.handler1.ts'), entry);

expect(() => new NodejsFunction(stack, 'Fn', {
entry,
})).not.toThrow();

fs.unlinkSync(entry);
});

test('throws when entry does not exist', () => {
expect(() => new NodejsFunction(stack, 'Fn', {
entry: 'notfound.ts',
Expand Down

0 comments on commit 4ba20fd

Please sign in to comment.