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

NodeJS Webpack builder #116

Open
buggy opened this issue May 31, 2019 · 6 comments

Comments

Projects
None yet
4 participants
@buggy
Copy link

commented May 31, 2019

Description:

I'm proposing to add a new nodejs webpack builder that would be triggered by the presence of a webpack.config.js file.

There are two uses I can see:

  1. As a Node developer I want to use TypeScript or Babel to compile my code from a more recent version of Javascript to a version supported by AWS Lambda.
  2. As a Node developer I want to use tree shaking to reduce the size of my code.

The builder could be based on the existing nodejs npm builder. It would build the code using a command like npx webpack-cli app.ts -o ../.aws-ts/build/HelloWorldFunction/app.js --config ./webpack.config.js instead of using npm pack. The package.json would probably need to be copied independently.

The Serverless Framework uses the same approach (a serverless-webpack plugin) for TypeScript support through the webpack ts-loader.

@Jeremias-Tecnom

This comment has been minimized.

Copy link

commented Jun 3, 2019

I like this.
I ve been using this SAM scaffold (i've adapted it to my sollutions).
What I like about it, is that it builds multiple functions wich are in separate folders.

@sanathkr

This comment has been minimized.

Copy link
Contributor

commented Jun 6, 2019

@buggy Good idea! This has come up several times before in the Typescript context.

The command you mentioned involves detecting the specific typescript file as well. Is there a more generic command/solution that works even if the project doesn't use Typescript? Alternatively we can create one Typescript builder & one generic webpack builder.

We should also think about if and how the built artifacts can be used to debug. If we have to produce additional artifacts like say sourcemaps for debugging, we should also support that (may be as a v2 if its hard).

Would you be able to send a PR? We can help you take this forward.

@buggy

This comment has been minimized.

Copy link
Author

commented Jun 7, 2019

The command you mentioned involves detecting the specific typescript file as well. Is there a more generic command/solution that works even if the project doesn't use Typescript? Alternatively we can create one Typescript builder & one generic webpack builder.

@sanathkr The proposal is generic. I've used app.ts in my example but you would replace that with the name of the handler function from the template.yaml. If someone was using Babel instead of TypeScript and their handler was called src/handler.js then the command would become:

npx webpack-cli src/handler.js -o ../.aws-ts/build/HelloWorldFunction/app.js --config ./webpack.config.js

Developers would control the build process using the webpack.config.js allowing them to select the loaders they want to use (i.e. TypeScript, Babel, etc). I think we can provide one template with support for both TypeScript and Babel along with documentation for removing each if they only want one.

We should also think about if and how the built artifacts can be used to debug. If we have to produce additional artifacts like say sourcemaps for debugging, we should also support that (may be as a v2 if its hard).

The standard approach is to build sourcemaps and use https://www.npmjs.com/package/source-map-support. It's something I would want to support immediately. The developer just needs to include it at the top of their handler.

Would you be able to send a PR? We can help you take this forward.

Are there instructions somewhere for how to do local development for aws-lamba-builders and aws-sam-cli? Specifically, how to get aws-sam-cli to use my local instance of aws-lamba-builders?

@sanathkr

This comment has been minimized.

Copy link
Contributor

commented Jun 7, 2019

Awesome! Makes sense.

Actually I just realized we didn't write down these instructions. Let me do it for you tomorrow. It's quite simple actually.

@buggy

This comment has been minimized.

Copy link
Author

commented Jun 10, 2019

@sanathkr I was looking at this over the weekend and I don't think a dedicated builder is required but I need to figure out why adding a prepack script to the package.json fails when "npm pack" is executed during "sam build" but works if you use "npm pack".

If I can sort that out then the we just need a new template to get webpack working.

@jfuss

This comment has been minimized.

Copy link
Contributor

commented Jun 11, 2019

@buggy I just submitted a PR on how to do local testing through SAM CLI in #119. For this issue, it may require changes to SAM CLI too for testing the full workflow (if there is a new files we need to detect or a new workflow).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.