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

Would be possible to watch only one function? #69

Open
mastazi opened this issue Dec 18, 2021 · 2 comments
Open

Would be possible to watch only one function? #69

mastazi opened this issue Dec 18, 2021 · 2 comments

Comments

@mastazi
Copy link

mastazi commented Dec 18, 2021

Currently I have npm run watch set to run webpack-cli -w and my webpack.config.js is the same as the one shown at the beginning of the README here on Github.

Currently in our SAM project my dev machine takes more than a minute to build. So I was wondering if there was a way to create a command where I can run npm run watch myFunction and it will only watch that single function? Usually when doing dev work I'm often only working on one single function at a time so this would be super helpful.

I suspect that there is a way but my knowledge of Webpack & of the aws-sam-webpack-plugin is not great. Any ideas?

@mastazi
Copy link
Author

mastazi commented Dec 27, 2021

Replying to myself...

I am now able to do the following:

Assuming that:

  1. the handler is in src/handlers/users/create.ts and
  2. the function name (in template.yml) is createUser,

then I can build like this:
npm run build -- --env handler=users/create.ts --env functionName=createUser
and I can watch with:
npm run watch -- --env handler=users/create.ts --env functionName=createUser

This assumes that all handlers are in src/handlers. If you don't have a handlers folder then modify the line starting with entry: below.

const path = require("path");
const AwsSamPlugin = require("aws-sam-webpack-plugin");

module.exports = (env) => {
  let awsSamPlugin = {}
  const singleFunction = (env.handler && env.functionName);
  if (singleFunction) {
    awsSamPlugin = {
      entry: () => '',
      filename: () => ''
    }
  } else {
    awsSamPlugin = new AwsSamPlugin();
  }
  return {
    // Loads the entry object from the AWS::Serverless::Function resources in your
    // SAM config. Setting this to a function will
    entry: singleFunction ? './src/handlers/' + env.handler : () => awsSamPlugin.entry(),

    // Write the output to the .aws-sam/build folder
    output: {
      filename: singleFunction ? './.aws-sam/build/' + env.functionName + '/app.js' : (chunkData) => awsSamPlugin.filename(chunkData),
      libraryTarget: "commonjs2",
      path: path.resolve(".")
    },

    // Create source maps
    devtool: "source-map",

    // Resolve .ts and .js extensions
    resolve: {
      extensions: [".ts", ".js"]
    },

    // Target node
    target: "node",

    // AWS recommends always including the aws-sdk in your Lambda package but excluding can significantly reduce
    // the size of your deployment package. If you want to always include it then comment out this line. It has
    // been included conditionally because the node10.x docker image used by SAM local doesn't include it.
    externals: process.env.NODE_ENV === "development" ? [] : ["aws-sdk"],

    // Set the webpack mode
    mode: process.env.NODE_ENV || "production",

    // Add the TypeScript loader
    module: {
      rules: [{ test: /\.tsx?$/, loader: "ts-loader" }]
    },

    // Add the AWS SAM Webpack plugin
    plugins: singleFunction ? [] : [awsSamPlugin]
  }
};

@mastazi
Copy link
Author

mastazi commented Dec 27, 2021

@graphboss I saw the source of aws-sam-webpack-plugin and I think this could be achieved by adding an optional parameter to entry like so:

awsSamPlugin.entry(functionName)

then it would be possible to do

npm run watch -- --env functionName=myFunction

thus eliminating the need to create two env variables (since aws-sam-webpack-plugin is already able to find the path of each handler).

If you think this is a viable change I can make a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant