serverless-heaven / serverless-webpack

Serverless plugin to bundle your lambdas with Webpack
MIT License
1.72k stars 414 forks source link

support bundling multiple packages #503

Open stevemao opened 5 years ago

stevemao commented 5 years ago

I have a mono repo like this

packages

I have a module in package-2 that requires some module in package-1 like this:

require('../package-1/index')

And I'd like to exclude both node_modules of package-1 and package-2 when I build package-2.

I can externalise both with webpack:

// webpack.config.js
var nodeExternals = require('webpack-node-externals')

module.exports = {
  // we use webpack-node-externals to excludes all node deps.
  // You can manually set the externals too.
  externals: [nodeExternals(), nodeExternals({modulesDir: '../package-1/node_modules'})],
}

Would be good if I could do something like this:

custom:
  webpack:
    includeModules:
      packagePath: 
        - './package.json'
        - '../package-1/package.json'
olup commented 4 years ago

Hey any news about this ? Webpack outputs a package.json with empty dependencies numbers when they are from my aliased package with it's own package.json. Figured I need a way to tell webpack to look for version number in both local and linked package.json. Can we do this with the plugin ?

sthuber90 commented 4 years ago

Couldn't get it to work, so I switched to serverless-jetpack and serverless-scriptable-plugin

olup commented 4 years ago

Too bad, what was wrong ? Did you try to PR an implementation in packExternalMoules.js ? Do you have an example repo of your configuration serverless-jetpack and serverless-scriptable-plugin ?

sthuber90 commented 4 years ago

I also had the problem that not the right dependencies were package into during deployment. Furthermore the yarn.lockfile was ignored. I'm too unexperienced on the serverless plugins to be able to file a PR. And honestly I was also just looking for something that works out of the box or with not much configuration and the serverless-jetpack-plugin, in combination with the serverless-scriptable-plugin solved it for me.

All I had to do there, was to change my serverless.ym files (I have on serverless.yml in each package):

custom:
  scriptHooks:
    # These are all the places that jetpack could be invoked that would
    # ensure the TS files are built right before jetpack kicks in.
    # Hooks Cheat Sheet: https://gist.github.com/HyperBrain/50d38027a8f57778d5b0f135d80ea406#file-lifecycle-cheat-sheet-md
    before:invoke:local:invoke: yarn run build
    before:package:createDeploymentArtifacts: yarn run build
    before:package:function:package: yarn run build
    before:jetpack:package:package: yarn run build
  jetpack:
    base: "../.."
    preInclude:
      - "!**" # Start with absolutely nothing (typical in monorepo scenario)

package:
  individually: true

And yarn build just runs tsc -b tsconfig.json