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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@amplify-playground/code-from-asset-helper

v2.0.0-alpha.5

Published

A helper to build, bundle and copy a lambda or AppSync function to the Amplify assets directory.

Downloads

1,021

Readme

Code from Asset Helper

A helper to build, bundle and copy a lambda or AppSync function to the Amplify assets directory.

Installation

npm install --save-dev @amplify-playground/code-from-asset-helper

Basic Usage

Lambda Functions

The example below defines an Amplify backend and builds and bundles the function my-custom-function.lambda.ts using esbuild.

import {
  BuildMode,
  lambdaCodeFromAssetHelper,
} from "@amplify-playground/code-from-asset-helper";
import { defineBackend } from "@aws-amplify/backend";
import {
  Function as LambdaFunction,
  Runtime as LambdaRuntime,
} from "aws-cdk-lib/aws-lambda";
import path from "path";

const backend = defineBackend({
  //..
});

const stack = backend.createStack("MyCustomStack");

const buildConfig: AssetHelperConfig = {
  buildMode: BuildMode.Esbuild,
};

const myLambda = new LambdaFunction(stack, "MyCustomFunction", {
  handler: "index.handler",
  code: lambdaCodeFromAssetHelper(
    path.resolve("amplify/functions/my-custom-function.lambda.ts"),
    buildConfig
  ),
  runtime: LambdaRuntime.NODEJS_20_X,
});

Configuration

Disable building and copy file as is:

const buildConfig: AssetHelperConfig = {
  buildMode: BuildMode.Off,
};

Transpile using the TypeScript transpiler:

const buildConfig: AssetHelperConfig = {
  buildMode: BuildMode.Typescript,
  tsTranspileOptions: {}, // Optional
};

Build and bundle to a single file using esbuild (recommended):

const buildConfig: AssetHelperConfig = {
  buildMode: BuildMode.Esbuild,
  esBuildOptions: {}, // Optional
};

AppSync functions

The following example configures a pipeline resolver for a field createTodo :

//...

const backend = defineBackend({
  data,
});

const buildConfig: AppSyncAssetHelperConfigES = {
  buildMode: BuildMode.Esbuild,
};

backend.data.addResolver("createTodo", {
  fieldName: "createTodo",
  typeName: "Mutation",
  runtime: FunctionRuntime.JS_1_0_0,
  code: Code.fromAsset(
    //this function is plain js
    path.join(__dirnameCommon, "pipeline-handler.resolver.js")
  ),
  pipelineConfig: [
    new AppsyncFunction(stack, "TodoCreate", {
      api: backend.data.resources.graphqlApi,
      dataSource: todoDataSource,
      name: "TodoCreate",
      code: await appSyncCodeFromAssetHelper(
        path.join(__dirname, "todo-create.resolver.ts"),
        buildConfig
      ),
      runtime: FunctionRuntime.JS_1_0_0,
    }),
  ],
});

Linting

ESLint is called on the target source file as well as each its nested import files. Linting rules are picked up from your eslint configuration file (eg. eslint.config.mjs) in your project. Assuming that you are working with Lambda (node) and AppSync resolvers (AppSync_JS) then you will want a set of rules that constrains your code to what is available to the respective runtime environments.

The following configures linting for a project that contains source for both environments. As such, the project uses the convention of suffixing AppSync resolvers with *.resolver.ts and Lambda code with *.lambda.ts. This allows the eslint rules to apply the contraints appropriately. (For your project, you may choose instead to identify your AppSync/Lambda code based on directory path. Modify the glob patterns as necessary to reflect this.)

The config below also bans importing *.resolver.ts code in *.lambda.ts scripts and vice versa.

With this config in your project, you can get live linting in your IDE as well as when Amplify builds your code imported using the code from asset helper functions.

//File: eslint.config.mjs
import eslint from "@eslint/js";
import typescriptEslintParser from "@typescript-eslint/parser";
import globals from "globals";
import tseslint from "typescript-eslint";

// The AWS AppSync plugin that provides the rules for AppSync_JS:
import appsyncPlugin from "@aws-appsync/eslint-plugin";

export default tseslint.config(
  {
    ignores: [...ignoreFiles],
  },
  {
    languageOptions: {
      parser: typescriptEslintParser,
      sourceType: "module",
      globals: {
        ...globals.node,
      },
      parserOptions: {
        projectService: {
          allowDefaultProject: ["eslint.config.mjs", "./eslint.config.mjs"],
        },
      },
    },
  },
  {
    // :: Main Source Files
    extends: [
      eslint.configs.recommended,
      tseslint.configs.recommendedTypeChecked,
    ],

    rules: {
      ...rulesNoUnusedVarsConfig,
    },
    files: ["**/*.ts", "eslint.config.mjs"],
    ignores: [...ignoreFiles],
  },
  {
    // :: Resolver files
    extends: [
      eslint.configs.recommended,
      tseslint.configs.recommendedTypeChecked,
      appsyncPlugin.configs.recommended,
    ],
    rules: {
      "@typescript-eslint/no-restricted-imports": [
        "error",
        {
          patterns: [
            {
              group: ["aws-cdk-lib", "aws-cdk-lib/*"],
              message: "usage of appsync lambda libraries is disallowed.",
              allowTypeImports: true,
            },
            {
              group: ["*.lambda"],
              message: "usage of .lambda utilities is not allowed",
              allowTypeImports: true,
            },
          ],
        },
      ],
      ...rulesNoUnusedVarsConfig,
    },
    files: ["**/*.resolver.ts"],
    ignores: [...ignoreFiles, "**/*.test.ts", "**/*.lambda.ts"],
  },
  {
    // :: Lambda files
    extends: [
      eslint.configs.recommended,
      tseslint.configs.recommendedTypeChecked,
    ],
    rules: {
      "@typescript-eslint/no-restricted-imports": [
        "error",
        {
          patterns: [
            {
              group: ["@aws-appsync/utils", "@aws-appsync/utils/*"],
              message:
                "usage of appsync utils in a lambda function is disallowed.",
              allowTypeImports: true,
            },
            {
              group: ["*.resolver"],
              message: "usage of .resolver utilities is not allowed",
              allowTypeImports: true,
            },
          ],
        },
      ],
      ...rulesNoUnusedVarsConfig,
    },
    files: ["**/*.lambda.ts"],
    ignores: [...ignoreFiles, "**/*.test.ts", "**/*.resolver.ts"],
  },
  {
    // :: Tests
    extends: [eslint.configs.recommended, tseslint.configs.recommended],
    rules: {
      ...rulesNoUnusedVarsConfig,
    },
    files: ["**/*.{.test.ts}"],
    ignores: [...ignoreFiles],
  }
);

// Ignore Files
const ignoreFiles = [
  "**/build/**",
  "**/dist/**",
  "**/node_modules/**",
  "**/.amplify/",
  "**/codegen-out/**",
  "**/*.generated.ts",
];

// (Optional) Rule Override: allow unusued vars that are explicitly prefixed with `_unused`
const rulesNoUnusedVarsConfig = {
  "@typescript-eslint/no-unused-vars": [
    2,
    {
      argsIgnorePattern: "^_unused",
      varsIgnorePattern: "^_unused",
      caughtErrorsIgnorePattern: "^_unused",
    },
  ],
};

Breaking Changes

v1.x > v2.x:

  • Upgrades eslint from v8 to v9. Eslint's FlatConfig is now required.
  • The code from asset helpers no longer include the default configs but instead relies on your eslint config file (eslint.config.mjs).

License

Apache License 2.0