iopipe / serverless-plugin-iopipe

Automatically wrap your serverless framework functions with IOpipe
https://www.iopipe.com
Apache License 2.0
40 stars 8 forks source link

Add support for wrapping individually packaged lambda functions via serverless-webpack #64

Closed lucasklaassen closed 6 years ago

lucasklaassen commented 6 years ago

Description

serverless-webpack offers the ability to utilize webpacks Tree-Shaking and indiviually package lambda functions that only contain the modules and dependencies that they consume. This allows you to develop multiple lambda functions within the same repo or project and not bundle all of the dependencies for the entire project into every function. (just the ones that each function needs) Unfortunately it appears that the serverless-plugin-iopipe does not support this. Instead of wrapping each individual lambda function with IOPipe, it bundles everything into one file and wraps the functions within that one big file.

Application and library versions

serverless.yml

plugins:
  - serverless-plugin-iopipe
  - serverless-dynamodb-local
  - serverless-offline
  - serverless-webpack
  - serverless-secrets-plugin
  - serverless-vpc-discovery
  - serverless-sqs-alarms-plugin

custom:
...
  webpack:
    package:
      individually: true

webpack.config.js

const nodeExternals = require('webpack-node-externals');
const path = require('path');
const slsw = require('serverless-webpack');
const webpack = require('webpack');

module.exports = {
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  devtool: 'source-map',
  entry: slsw.lib.entries,
  target: 'node',
  externals: [nodeExternals()],
  output: {
    libraryTarget: 'commonjs',
    path: path.resolve(__dirname, '.webpack'),
    filename: '[name].js'
  },
  plugins: [
    new webpack.IgnorePlugin(/\.(md|yml.encrypted|sh|vm)$/)
  ],
  module: {
    rules: [{
      test: /\.jsx?$/,
      exclude: /node_modules/,
      use: [{
        loader: 'babel-loader'
      }]
    },
    {
      test: /\.json$/,
      use: [{
        loader: 'json-loader'
      }]
    },
    {
      test: /\.yml$/,
      use: [{
        loader: 'yml'
      }]
    }
    ]
  }
};
coreylight commented 6 years ago

Thanks for the issue @lucasklaassen! I believe there is a fairly straightforward path to resolving this by creating a new file per handler instead of 1 file that imports multiple handlers. We will add this to our to-do list.