DavidWells / serverless-manifest-plugin

Output manifest of endpoints, resources, outputs, etc. of a serverless service
https://youtu.be/sq4rMN5fwUQ
MIT License
25 stars 7 forks source link

Not compatible with serverless-webpack #19

Open orr-levinger opened 3 years ago

orr-levinger commented 3 years ago

I am using this plugin for a while now to set env variables. recently i added the serverless-webpack to reduce my bundle size i am using typescrypt so my webpack.config looks like this:

/ eslint-disable @typescript-eslint/no-var-requires / const path = require('path'); const slsw = require('serverless-webpack'); const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

const isLocal = slsw.lib.webpack.isLocal;

module.exports = {
  mode: isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  devtool: 'source-map',
  resolve: {
    extensions: [ '.js', '.jsx', '.json', '.ts', '.tsx' ]
  },
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js'
  },
  target: 'node',
  module: {
    rules: [
      {
        // Include ts, tsx, js, and jsx files.
        test: /\.(ts|js)x?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'cache-loader',
            options: {
              cacheDirectory: path.resolve('.webpackCache')
            }
          },
          'babel-loader'
        ]
      }
    ]
  },
  plugins: [new ForkTsCheckerWebpackPlugin()]
};

i am getting this error from the manifest plugin:

 Error --------------------------------------------------

  Error: File "src/functions/auth/pre-sign-up.js" not found. /Users/orrlevinger/projects/BlindChat/blind-chat-backend/src/functions/auth/pre-sign-up.js missing
      at getFunctionPath (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/serverless-manifest-plugin/src/index.js:535:11)
      at Object.keys.reduce.urls.apiGateway (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/serverless-manifest-plugin/src/index.js:317:30)
      at Array.reduce (<anonymous>)
      at getFormattedData (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/serverless-manifest-plugin/src/index.js:213:52)
      at /Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/serverless-manifest-plugin/src/index.js:85:30
      at tryCatcher (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromise0 (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/bluebird/js/release/promise.js:649:10)
      at Promise._settlePromises (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/bluebird/js/release/promise.js:729:18)
      at _drainQueueStep (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/bluebird/js/release/async.js:93:12)
      at _drainQueue (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/bluebird/js/release/async.js:102:5)
      at Immediate.Async.drainQueues [as _onImmediate] (/Users/orrlevinger/projects/BlindChat/blind-chat-backend/node_modules/bluebird/js/release/async.js:15:14)
      at processImmediate (internal/timers.js:461:21)
DavidWells commented 3 years ago

If you are using typescript you want to use the srcPath option https://github.com/DavidWells/serverless-manifest-plugin/pull/18/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R44

This should resolve the file

cptflammin commented 3 years ago

Hey, getting the same problem here with Typescript, tried this workaround but srcPath does not seem to solve the problem as I feel the individual packing for lambda functions causes troubles:

  1. the plugin looks for .webpack/src/functions/user/ instead of .webpack/confirmuserSignup/src/functions/user/
  2. it might be in addition that at the time when serverless-manifest-plugin looks for .js file the .webpack directory is not present anymore (erased) in the deployment process

using serverless framework template 'aws-nodejs-typescript'

# serverless.yaml
plugins:
  - serverless-offline
  - serverless-appsync-plugin
  - serverless-iam-roles-per-function
  - serverless-webpack
  - serverless-export-env
  - serverless-manifest-plugin
  - serverless-prune-plugin
custom:
  manifest:
      srcPath: .webpack
functions:
  confirmUserSignup:
    handler: src/functions/user/save-user-profile-at-signup.saveUserProfileAtSignup

Webpack config

const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

module.exports = {
  context: __dirname,
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  devtool: slsw.lib.webpack.isLocal ? 'eval-cheap-module-source-map' : 'source-map',
  resolve: {
    extensions: ['.mjs', '.json', '.ts'],
    symlinks: false,
    cacheWithContext: false,
    plugins: [
      new TsconfigPathsPlugin({
        configFile: './tsconfig.paths.json',
      }),
    ],
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  optimization: {
    concatenateModules: false,
  },
  target: 'node',
  externals: [nodeExternals()],
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      {
        test: /\.(tsx?)$/,
        loader: 'ts-loader',
        exclude: [
          [
            path.resolve(__dirname, 'node_modules'),
            path.resolve(__dirname, '.serverless'),
            path.resolve(__dirname, '.webpack'),
          ],
        ],
        options: {
          transpileOnly: true,
          experimentalWatchApi: true,
        },
      },
    ],
  },
  plugins: [],
};

Output:

.webpack
|- confirmuserSignup
    |- src
         |- functions
             |- user
                  |- save-user-profile-at-signup.js

Error: File "src/functions/user/save-user-profile-at-signup.js" not found. .webpack/src/functions/user/save-user-profile-at-signup.js missing