spring-media / aws-lambda-router

Improved routing for AWS Lambda like SNS and ApiGateway
Apache License 2.0
98 stars 52 forks source link

Move from dynamic imports to static imports? #65

Open TomiTakussaari opened 3 years ago

TomiTakussaari commented 3 years ago

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 commented 3 years ago

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.