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

Support ES Modules #87

Open
ffMathy opened this issue Jul 20, 2023 · 2 comments
Open

Support ES Modules #87

ffMathy opened this issue Jul 20, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@ffMathy
Copy link

ffMathy commented Jul 20, 2023

See https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/

Here is my template file:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Globals:
  Function:
    Runtime: nodejs18.x

Resources:
  Lambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: app.handler
      Timeout: 10
      Architectures:
      - arm64
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /events
            Method: POST

When running the plugin, the final template file generated is this:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Globals:
  Function:
    Runtime: nodejs18.x
Resources:
  Lambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: Lambda
      Handler: app.mjs.handler
      Timeout: 10
      Architectures:
        - arm64
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /events
            Method: POST

Note that app.mjs.handler is wrong. It should be just app.handler.

In my webpack.config.js, I have:

  • output.library.type set to "module".
  • output.library.chunkFormat set to "module".
  • experiments.outputModule set to true.

In my package.json, type is set to "module".

With this, .mjs files are generated instead of .js files. But because the file generated is called app.mjs.js instead of app.mjs, Lambda doesn't load it as an ES Module.

@ffMathy
Copy link
Author

ffMathy commented Jul 20, 2023

If I manually go in and rename the app.mjs.js file to app.mjs and then change the template.yml to also point to app.handler instead of app.mjs.handler, it works just fine.

So I suppose this should be an easy fix maybe?

What do you think?

@ffMathy
Copy link
Author

ffMathy commented Jul 20, 2023

As a workaround, I can do this:

filename: (chunkData) => awsSamPlugin.filename(chunkData).replace(".js", ".mjs")

In the config.

@buggy buggy added the bug Something isn't working label Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants