Marco
Marco

Reputation: 23945

GraphqlLoaderPlugin is not a function error when porting from sls to sst

We are trying to port an exisiting application from Serverless 3 to sst.

The serverless.yml has a custom.esbuild section:

custom:
  esbuild:
    bundle: true
    minify: false
    plugins: ./esbuild-plugins.js
    sourcemap: true
    target: es2022

with the esbuild-plugins.js being defined as:

const { default: graphqlLoaderPlugin } = require("@luckycatfactory/esbuild-graphql-loader");
const { nodeExternalsPlugin } = require("esbuild-node-externals");
const fs = require("fs-extra");
const path = require("path");

const copyPlugin = ({ from, to }) => {
    return {
        name: "copy",
        setup(build) {
            const target = build.initialOptions.outfile
                ? path.dirname(build.initialOptions.outfile)
                : path.resolve(build.initialOptions.outdir);
            build.onEnd(() => fs.copySync(from, path.join(target, to), { overwrite: true }));
        },
    };
};

// eslint-disable-next-line no-undef
module.exports = [
    graphqlLoaderPlugin(),
    nodeExternalsPlugin(),
    copyPlugin({
        from: "src/type-defs",
        to: "./type-defs",
    }),
];

Now the problem arises, when I try to move that plugins section over to sst. Here's the relevant part of the stack for the function in question:

import * as graphqlLoaderPlugin from "@luckycatfactory/esbuild-graphql-loader";

export function API({ stack }: StackContext) {
    const { stackName, stage } = stack;
    console.log(`Deploying ${stackName} to stage: ${stage}`);

    const function = new Function(stack, "function", {
        handler: "./src/lambda.handler",
        runtime: "nodejs20.x",
        memorySize: 768,
        copyFiles: [{ from: "./src/type-defs" }],
        nodejs: {
            esbuild: {
                plugins: [graphqlLoaderPlugin.default()],
            },
        },
        environment: {
           /* ...*/
        },
    });

    const api = new ApiGatewayV1Api(stack, "api", {
        routes: {
            "ANY /graphql": {
                function: function,

                cdk: {
                    method: {
                        apiKeyRequired: true,
                    },
                },
            },
        },
    });

    /* omitted usageplans, apikeys and other goodness */

Using the current esbuild configuration yields the following error:

TypeError: graphqlLoaderPlugin.default is not a function

The same is true, when I change to import to import graphqlLoaderPlugin from "@luckycatfactory/esbuild-graphql-loader"; and the plugin call to graphqlLoaderPlugin().

What is the correct syntax for loading a plugin into esbuild when using sst?

Upvotes: 0

Views: 32

Answers (1)

Marco
Marco

Reputation: 23945

I found that importing it via a js module using require did the trick.

const { default: graphqlLoaderPlugin } = require("@luckycatfactory/esbuild-graphql-loader");

module.exports = [
    graphqlLoaderPlugin()
]

importing this module into the sst stack, now successfully builds.

Upvotes: 0

Related Questions