npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cdk-lambda-webpack

v2.3.2

Published

Construct for AWS Lambda with Webpack

Downloads

3

Readme

CDK construct for AWS Lambda with Webpack

This construct will budle your resources with webpack and create separate assets so only the used dependencies are included.

Example:

import * as cdk from "aws-cdk-lib/core";
import { LambdaWebpack } from "cdk-lambda-webpack";

/* For now, you need to use "async" stacks on the following way: */
export async function ExampleStack(
  scope: cdk.App,
  id: string,
  props?: cdk.StackProps
) {
  /* Instead of calling "super()", you create a stack instance */
  const stack = new cdk.Stack(scope, id, props);

  /* Use case is really similar to the lambda construct */
  const lambda = await LambdaWebpack(stack, `ExampleLambda`, {
    /* You can pass any option used by the Lambda construct, except 'code' */
    memorySize: 128,
    runtime: Runtime.NODEJS_14_X,
    functionName: `example-lambda`,
    timeout: cdk.Duration.seconds(25),
    environment: {
      NODE_ENV: "production",
    },
    /* Handle your point to your entry init file + : + the export holding code, on this case it's 'default' */
    handler: "./src/example.ts:default",
    /* Custom webpack config */
    webpack: {
      packager: "npm", // yarn or npm. Default is npm
      webpackConfigPath: "./webpack.config.js", // Points to the webpack config used to build the assets.
      includeModules: {
        forceExclude: ["aws-sdk"], // Force exclude "aws-sdk" module.
      },
      options: {}, // Other aditional options described below.
    },
  });
}

LambdaWebpack Construct API.

All of the Lambda construct parameters are available, except code, for obvious reasons.

We do have two reserved parameters, handler which their behaviour is diferent to the construct, and webpack which is a custom parameter of this library.

Handler parameter.

In our library, handler is a string parameter, which defines the file entry point, plus the exported function to call, those are being separated by a colon (:).

Some use examples:

  • Let's say that I want to use the default export of src/example.js, I will use ./src/example.js:default.
  • Let's say that I want to use the default export of src/example.ts, I will use ./src/example.ts:default.
  • Let's say that I want to use the named export "doRequest" of src/example.ts, I will use ./src/example.ts:doRequest.

Webpack parameter.

The webpack parameter declares configuration regarding how your assets are going to be built.

Webpack config file path.

The webpackConfigPath parameter is the path to the webpack config file.

IncludeModules:

Monorepo config

In some configuration (like monorepo), node_modules is in parent directory which is different from where package.json is. Set nodeModulesRelativeDir to specify the relative directory where node_modules is.

webpack: {
  includeModules: {
    nodeModulesRelativeDir: '../../' // relative path to current working directory.
  }
},
Forced inclusion

Sometimes it might happen that you use dynamic requires in your code, i.e. you require modules that are only known at runtime. Webpack is not able to detect such externals and the compiled package will miss the needed dependencies. In such cases you can force the plugin to include certain modules by setting them in the forceInclude array property. However the module must appear in your service's production dependencies in package.json.

webpack: {
  includeModules: {
    forceInclude: ['module1', 'module2']
  }
},
Forced exclusion

You can forcefully exclude detected external modules, e.g. if you have a module in your dependencies that is already installed at your provider's environment.

Just add them to the forceExclude array property and they will not be packaged.

webpack: {
  includeModules: {
    forceExclude: ['module1', 'module2']
  }
},

Packager and Options

Packager can be either npm or yarn, learn below to see which options are enabled for each case:

NPM

By default, the plugin uses NPM to package the external modules. However, if you use npm, you should use any version <5.5 >=5.7.1 as the versions in-between have some nasty bugs.

The NPM packager supports the following packagerOptions:

| Option | Type | Default | Description | | --------- | ---- | ------- | --------------------------------------------------- | | noInstall | bool | false | Do not run npm install (assume install completed) |

Yarn

Using yarn will switch the whole packaging pipeline to use yarn, so does it use a yarn.lock file.

The yarn packager supports the following packagerOptions:

| Option | Type | Default | Description | | ------------------ | ---- | ------- | ---------------------------------------------------- | | ignoreScripts | bool | false | Do not execute package.json hook scripts on install | | noInstall | bool | false | Do not run yarn install (assume install completed) | | noFrozenLockfile | bool | false | Do not require an up-to-date yarn.lock | | networkConcurrency | int | | Specify number of concurrent network requests |

Yarn2

There is an experimental Yarn2 packager, it's import is LambdaWebpackYarn2. It does not support additional config for now, usage:

import * as cdk from "aws-cdk-lib/core";
import { LambdaWebpack } from "cdk-lambda-webpack";

/* For now, you need to use "async" stacks on the following way: */
export async function ExampleStack(
  scope: cdk.App,
  id: string,
  props?: cdk.StackProps
) {
  /* Instead of calling "super()", you create a stack instance */
  const stack = new cdk.Stack(scope, id, props);

  /* Use case is really similar to the lambda construct */
  const lambda = await LambdaWebpack(stack, `ExampleLambda`, {
    /* You can pass any option used by the Lambda construct, except 'code' */
    memorySize: 128,
    runtime: Runtime.NODEJS_14_X,
    functionName: `example-lambda`,
    timeout: cdk.Duration.seconds(25),
    environment: {
      NODE_ENV: "production",
    },
    /* Handle your point to your entry init file + : + the export holding code, on this case it's 'default' */
    handler: "./src/example.ts:default",
    /* Custom webpack config */
    webpack: {
      webpackConfigPath: "./webpack.config.js", // Points to the webpack config used to build the assets.
    },
  });
}

It uses the internal API of yarn2 instead of using the command line, so anything that yarn2 supports, should be available.

As an added feature, it resolves workspace dependencies, so it should be safe to use in workspace environment (it can isolate a specific workspace and resolve their dependencies).

Still, it's a experimental feature, so please report any issues you find.


Special Thanks:

All the people working at Serverless Webpack. The logic for both Npm and Yarn1 are, mostly, extracted from there.

Yarn focus command which was a great starting point for Yarn2+ support.