liady / webpack-node-externals

Easily exclude node modules in Webpack
MIT License
1.3k stars 62 forks source link

Serverless re running the npm install during deployment when using node-externals #84

Open programoholic opened 3 years ago

programoholic commented 3 years ago

I am working on a node application using serverless architecture. We are using serverless-webpack plugin to build the application. Here is the webpack.config.js :

module.exports = {
  mode: isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  externals: [nodeExternals()],
  devtool: 'inline-cheap-module-source-map',
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.ts', '.tsx']
  },
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js'
  },
  optimization: {
    minimize: true,
    minimizer: [new TerserPlugin()],
  },
  target: 'node',
  module: {
    rules: [{
        test: /\.ts$/,
        exclude: /node_modules/,
        loader: 'ts-loader',
        options: {
          // disable type checker - we will use it in fork plugin
          transpileOnly: true
        }
      },
      {
        test: /\.(graphql|gql)$/,
        exclude: /node_modules/,
        loader: 'graphql-tag/loader',
      }
    ]
  },
  // plugins: [new ForkTsCheckerWebpackPlugin()]
};

and serverless.yml file. :

service: app
provider:
  name: aws
  region: ${env:AWS_REGION}
  stage: ${env:STAGE}
  logRetentionInDays: 90
  role: ${env:ROLE}
  versionFunctions: false
  runtime: nodejs12.x

package:
  individually: true

plugins:
- serverless-webpack
- serverless-offline

custom:
  webpack:
    webpackConfig: './webpack.config.js'
    packager: 'npm'
    includeModules: true

functions:
  ######## GraphQl ###########
  graphql:
    role: ${env:ROLE}
    runtime: nodejs12.x
    timeout: 30
    handler: aws/lambda/common/api/graphql.graphqlHandler

    events:
      - http:
          path: graphql
          method: post

      - http:
          path: graphql
          method: get

When I am running sls deploy --config serverless.yml to deploy the application , everything is working and the lambda is getting deployed successfully. Only thing is , during the process of deployment , it is using the package-lock.json and installing the used packages again :

Serverless: Package lock found - Using locked versions

Serverless: Packing external modules:  graphql-compose@^7.19.3, linewrap@^0.2.1, d64@^1.0.0, graphql-scalars@^1.2.6, graphql-tools@^6.0.12

Serverless: Packaging service...

My question is how to stop rerunning the npm install again during deployment instead it should use the existing node_moudles while packing the external modules. Can anybody help me on this. ?