Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Move from dynamic imports to static imports? #65

Open
TomiTakussaari opened this issue Apr 15, 2021 · 1 comment
Open

Move from dynamic imports to static imports? #65

TomiTakussaari opened this issue Apr 15, 2021 · 1 comment

Comments

@TomiTakussaari
Copy link

Hi

Index.ts uses dynamic importing to dynamically load only the processors that are needed.

This causes problems with bundling, such as esbuild, because they are unable to bundle the required files.

Could this be changed to something like this:

import * as ProxyIntegration from "aws-lambda-router/lib/proxyIntegration";
import * as SnsIntegration from "aws-lambda-router/lib/sns";
import * as SqsIntegration from "aws-lambda-router/lib/sqs";
import * as S3Integration from "aws-lambda-router/lib/s3";

const processors = {
  proxyIntegration: ProxyIntegration,
  sns: SnsIntegration,
  sqs: SqsIntegration,
  s3: S3Integration,
};

const extractEventProcessorMapping = (routeConfig: RouteConfig) => {
  const processorMap = new Map<string, EventProcessor>();
  for (const key of Object.keys(routeConfig)) {
    if (key === "debug" || key === "onError") {
      continue;
    }
    try {
      const processor = processors[key];
      if (!processor) {
        throw new Error(`Could not find processor ${key}`);
      }
      processorMap.set(key, processor);
    } catch (error) {
      throw new Error(
        `The event processor '${key}', that is mentioned in the routerConfig, cannot be instantiated (${error.toString()})`
      );
    }
  }
  return processorMap;
};

This way imports are static, and esbuild could bundle the project.

@chgohlke
Copy link
Member

Hi @TomiTakussaari , i think you are right. We don't really need dynamic loading at this point. If I can find the time, I will look into it. But if you have the desire to create a PR, then feel free to do so.

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

No branches or pull requests

2 participants