serverless-plugin-optimize: Cannot work with prisma

Serverless: Typescript compiled.
Serverless: Optimize: starting engines
Serverless: Optimize: accounts-dev-main
 
  Syntax Error -------------------------------------------
 
  SyntaxError: Unexpected token (19074:12) while parsing /Users//services/node_modules/@prisma/client/runtime/index.js while parsing file: /Users//services/node_modules/@prisma/client/runtime/index.js
      at DestroyableTransform.end [as _flush] (/Users//services/node_modules/insert-module-globals/index.js:114:21)

https://github.com/prisma/prisma/issues/5392#issuecomment-793100660

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 3
  • Comments: 16

Most upvoted comments

I’m switching to webpack and it works


plugins:
  # - serverless-bundle
  # - "serverless-plugin-typescript"
  - serverless-webpack
  # - "@hewmen/serverless-plugin-typescript"
  # - serverless-plugin-optimize
  - serverless-offline

custom:
  # bundle:
  #   packager: yarn
  #   linting: false
  #   tsConfig: "tsconfig.json"
  webpack:
    webpackConfig: webpack.config.js
    packager: 'yarn'
    includeModules: true # This is required
    packagerOptions:
      scripts: # this is the magic
        - prisma generate 

  serverless-offline:
    httpPort: 9000
  warmup:      
    - production
    - staging

const slsw = require("serverless-webpack");

const path = require("path");
const webpack = require("webpack");
const nodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = {
  entry: slsw.lib.entries,
  // entry: ["./src/lambda.ts"],
  externals: [
    // { _http_common: "_http_common" },
    nodeExternals(), // this is required
  ],
  plugins: [
    new CopyWebpackPlugin({
      patterns: [{ from: "./prisma/schema.prisma" }],
    }), // without this the prisma generate above will not work

    // ...options.plugins,
    new webpack.IgnorePlugin({
      checkResource(resource) {
        const lazyImports = [
          "@nestjs/microservices",
          "@nestjs/platform-express",
          "@nestjs/websockets",
          "@nestjs/websockets/socket-module",
          "@nestjs/microservices/microservices-module",
          "@prisma/client",
          "cache-manager",
          "class-validator",
          "class-transformer",
        ];
        if (!lazyImports.includes(resource)) {
          return false;
        }
        try {
          require.resolve(resource);
        } catch (err) {
          return true;
        }
        return false;
      },
    }),
  ],
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: "ts-loader",
        include: [__dirname],
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: [".ts", ".js"],
  },
  // output: {
  //   libraryTarget: "commonjs2",
  //   path: path.join(__dirname, ".webpack"),
  //   filename: "src/lambda.js",
  // },
};

optimize: external: [‘@prisma/client’, ‘.prisma/client’]

@hoangtrieukd you save my life with these. confirm it works with these two line

custom: optimize: external: ['@prisma/client', '.prisma/client']

https://www.npmjs.com/package/serverless-plugin-optimize-prisma @softmarshmallow @bmcilw1

I’ve had to bend this plugin in a couple of hacky ways to make it work. It is not ready for NPM and I’m not willing to make it npm compatible until I’m able to clean up the patches a bit. You are welcome to use this branch if you need to. You can reference it directly in your package.json or fork and reference your own copy.

Link to my fork: https://github.com/Album-Health/serverless-plugin-optimize

Best of luck.