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

sls-relative-paths

v1.1.1

Published

Allow defining Serverless Framework functions handlers paths relatively

Downloads

627

Readme

✨ DRYer Serverless configuration

When defining a Serverless Framework service file in TS or JS, it's common to import functions configurations from the same folders as their handlers.

It helps keeping the service file light, even when the number of functions grows:

// serverless.ts(|js)
import { myFunction } from 'functions/myFunction/config.ts(|js)';
import { anotherFunction } from 'functions/anotherFunction/config.ts(|js)';

export const myService = {
  service: 'my-service',
  functions: {
    myFunction,
    anotherFunction,
    ...
  },
  ...
};

However, handlers paths still need to be provided in the functions files:

// functions/myFunction/handler.ts(|js)
export const main = ... // function code
// functions/myFunction/config.ts(|js)
export const myFunction = {
  // 👇 Still needed
  handler: 'functions/myFunction/handler.main', // Wait... but that's where I am 😭
  ...
};

This is a code duplication that can annoy developers, and frequently cause bugs (typically when moving code around or copy/pasting functions).

That's when sls-relative-paths comes to the rescue 💪

Sls-relative-paths

The sls-relative-paths plugin allows you to define your handlers paths relatively to their functions files:

// serverless.ts(|js)
import { myFunction } from 'functions/myFunction/config.ts';

export const myService = {
  service: 'my-service',
  plugins: ['sls-relative-paths'], // 👈 add plugin
  functions: {
    myFunction,
    ...
  },
  ...
};
// functions/myFunction/config.ts(|js)
export const myFunction = {
  dirName: __dirname, // 👈 dirname is required to re-construct the complete path
  handler: './handler.main', // 🎉 relative path will work!
  ...
};

You can also set a default relative path in your service file:

// serverless.ts(|js)
import { myFunction } from 'functions/myFunction/config.ts';

export const myService = {
  service: 'my-service',
  plugins: ['sls-relative-paths'],
  functions: {
    myFunction,
    ...
  },
  relativePaths: {
    default: 'handler.main'
  },
  ...
};
// functions/myFunction/config.ts(|js)
export const myFunction = {
  dirName: __dirname,
  // 🙌 'handler' prop not needed and defaulted to 'handler.main'
  ...
};

Installation

# npm
npm install --save-dev sls-relative-paths

# yarn
yarn add --dev sls-relative-paths

Other exports

ServiceProperties type

In TS, you can assign this type to your service file:

// serverless.ts(|js)
import type { ServiceProperties } from 'sls-relative-path';
import type { AWS } from '@serverless/typescript';

export const myService: AWS & ServiceProperties = {
  service: 'my-service',
  plugins: ['sls-relative-paths'],
  functions: {
    myFunction,
    ...
  },
  relativePaths: {
    default: ['handler', 'main'] // ❌ default should be a string
  },
  ...
};

FnProperties type

In TS, you can assign this type to your functions files:

  // functions/myFunction/config.ts(|js)
import type { FnProperties } from 'sls-relative-path';
import type { AWS } from '@serverless/typescript';

type FnConfig = NonNullable<AWS['functions']>[string] & FnProperties;

export const myFunction: FnConfig = {
  dirName: undefined, // ❌ dirName is required
  handler: './handler.main',
  ...
};