Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda: Can't use typescript module files #21635

Closed
dj-rabel opened this issue Aug 17, 2022 · 1 comment · Fixed by #25642
Closed

Lambda: Can't use typescript module files #21635

dj-rabel opened this issue Aug 17, 2022 · 1 comment · Fixed by #25642
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@dj-rabel
Copy link

dj-rabel commented Aug 17, 2022

Describe the bug

We've tried to compile ESM output (.mjs) files from .mts (TypeScript Module) files via CDK "NodejsFunction". We don't want to change everything in our project to be JavaScript module code. So the only option is to use .mjs, or in our case, .mts files.
But someone thought it is a good idea to prohibit any other file extensions than .jsx?, .tsx? or .mts. So we're constantly getting an error that .mts is not valid. (which it is and should be allowed)

if (!/\.(jsx?|tsx?|mjs)$/.test(entry)) {

As soon as I remove the referenced line from the functions.js it compiles and deploys fine ...

Expected Behavior

At least .mts should be allowed as well if there is .ts and .mjs allowed.

https://www.typescriptlang.org/docs/handbook/esm-node.html#new-file-extensions

Maybe it is a good idea to allow .cjs and .cts as well these days 😅

Current Behavior

Error "Only JavaScript or TypeScript entry files are supported." is thrown even for perfectly valid TypeScript files. Probably even for perfectly valid JavaScript files with the ending .cjs.

Reproduction Steps

Stack code to create lambda using mts file and ESM format.

import * as lambda from "aws-cdk-lib/aws-lambda-nodejs";
import { Runtime as LambdaRuntime } from "aws-cdk-lib/aws-lambda";

new lambda.NodejsFunction(this, 'DummyLambda', {
    entry: path.join('src', `DummyLambda.mts`),
    runtime: LambdaRuntime.NODEJS_16_X,
    bundling: {
        charset: lambda.Charset.UTF8,
        format: lambda.OutputFormat.ESM,
        target: "node16.15",
        @see https://github.com/evanw/esbuild/issues/584#issuecomment-740170568
        esbuildArgs: {
            "--main-fields": "module,main",
        },
    },
});

Contents of specified DummyLambda.mts can be any typescript code. Doenst matter for generating the error. The file isn't even required to exist.

Possible Solution

Regarding to https://www.typescriptlang.org/docs/handbook/esm-node.html#new-file-extensions at least .mts should be allowed. I would suggest to allow .cjs and .cts as well.

Additional Information/Context

No response

CDK CLI Version

2.37.1

Framework Version

No response

Node.js Version

All

OS

All

Language

Typescript

Language Version

All

Other information

No response

@dj-rabel dj-rabel added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 17, 2022
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Aug 17, 2022
@kaizencc kaizencc added @aws-cdk/aws-lambda-nodejs and removed @aws-cdk/aws-lambda Related to AWS Lambda labels Aug 18, 2022
@kaizencc kaizencc assigned corymhall and unassigned kaizencc Aug 18, 2022
@peterwoodworth peterwoodworth added p2 effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Aug 18, 2022
@corymhall corymhall removed their assignment Oct 18, 2022
@mergify mergify bot closed this as completed in #25642 Jun 5, 2023
mergify bot pushed a commit that referenced this issue Jun 5, 2023
This adds support for using TypeScript Module (`.mts`), TypeScript CommonJS (`.cts`) and JavaScript CommonJS (`.cjs`) entry files, as well as adding these to the default entry file search. 

This is a must for projects mixing ESM and CommonJS (quite common as not everything can/should be fully ESM yet) or where explicit file extension is desired.

I tried to follow the existing convention as best I could to keep it as simple as possible. I am not sure why the tests for the existing entry files are skipped as they seem to be working correctly, at least locally, so I enabled the new tests I added.

Closes #21635.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

github-actions bot commented Jun 5, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants