serverless-webpack icon indicating copy to clipboard operation
serverless-webpack copied to clipboard

fix: implemented the multiple webpack compile config

Open RishikeshDarandale opened this issue 5 years ago • 17 comments

What did you implement:

Implemented the fix for providing a way to provide the multiple webpack compile configuration in webpack configuration file. The sample config can be seen as below:

// webpack.config.js

module.exports = [
  {
    entry: './handler1.js',
    target: 'node',
    module: {
      loaders: [ ... ],
    },
  },
  {
    entry: './handler2.js',
    target: 'node',
    module: {
      loaders: [ ... ],
    },
  },
];

Similarly, webpack configuration can export an asynchronous object with multi compile configuration would work as expected.

Closes #439

How did you implement it:

This plugin already supports the multi compile configuration when package.individually is set to true. When there are multiple functions and package.individually is set, then provided single webpack configuration gets cloned for function count times.

Thus, providing multi compile configuration in webpack should be possible with some constraint/assumption.

  • validate function was expecting a single webapck configuration object.
  • Thus, added logic to check if the configuration object is an array or not. If yes, then each config is processed the same way it was done for single configuration object.

When multi webpack compile config is provided, to constraints will be imposed as below:

  • If package.individually is false, then output.path for each config provided should match.
  • If package.individually is true, then compile config should be provided for each function and the output.path of compile should end with function-name.

Above constraints make sure that post validate process such as compile, package, local invoke and offline functions behave as it is.

How can we verify it:

I have verified this with following examples:

  • multi-function with multi compile config provided as shown above.
  • single function with multi compile config provided as shown above.

Todos:

  • [x] Write tests
  • [x] Write documentation
  • [x] Fix linting errors
  • [x] Make sure code coverage hasn't dropped
  • [x] Provide verification config / commands / resources
  • [x] Enable "Allow edits from maintainers" for this PR
  • [x] Update the messages below

Is this ready for review?: YES Is it a breaking change?: NO

RishikeshDarandale avatar Sep 05 '19 07:09 RishikeshDarandale

I will give it a try to resolve the two constraints mentioned above with my solution. Mean time you can provide the feedback for existing changes. Thanks!

RishikeshDarandale avatar Sep 05 '19 13:09 RishikeshDarandale

@HyperBrain , Can you please review?

RishikeshDarandale avatar Sep 09 '19 06:09 RishikeshDarandale

@HyperBrain , any comment/update on this?

RishikeshDarandale avatar Sep 13 '19 14:09 RishikeshDarandale

@HyperBrain, any luck?

RishikeshDarandale avatar Sep 23 '19 07:09 RishikeshDarandale

@HyperBrain , Sorry to ping you one more time. Did you get a chance to look into this one?

RishikeshDarandale avatar Nov 07 '19 15:11 RishikeshDarandale

:+1: for this fix. I could use it as well. I've got a webpack.config that builds several different entries, with different outputs and plugins, and serverless with serverless-webpack chokes on module.exports = [server, web, serverless] with the following error:

  Error: Plugin could not be registered at 'before-compile'. Hook was not found.
  BREAKING CHANGE: There need to exist a hook at 'this.hooks'. To create a compatibility layer for this hook, hook into 'this._pluginCompat'.

Edit: Workaround, totally silly, but works.

If you have a webpack.config.js, with multiple exports as above, you can create a secondary webpack config, say webpack.serverless.js, that compiles the previous exports and returns the last like so:

const Webpack = require('webpack')
const config = require('./webpack.config.js')
const webCompiler = Webpack(config.shift(), (err, stats) => {})
const serverCompiler = Webpack(config.shift(), (err, stats) => {})
module.exports = config.pop()

Then in your serverless.yml:

 webpack:
    webpackConfig: ./webpack.serverless.js

This will allow you to group various entries with unique outputs and plugins in a single config, and run them (or some subset) prior to running the serverless webpack entry.

tyrauber avatar May 10 '20 14:05 tyrauber

@tyrauber , if you have your code somewhere on github, then I will take a look. If not passible to share the code, then share the snippet.

RishikeshDarandale avatar May 12 '20 17:05 RishikeshDarandale

Hi @RishikeshDarandale, here is a gist of a Serverless Vue Webpack 4 Config that details the issue. The problem comes from wanting to code split and share a base configuration and use webpack-merge to maintain them. Each entry - client, server, serverless - potentially needs different plugins or outputs. Unfortunately, serverless-webpack chokes on an array of exports, while webpack handles it fine.

tyrauber avatar May 12 '20 19:05 tyrauber

@miguel-a-calles-mba Can you please review this wrt master or release/5.3.2?

Please see here for more comments on #560 .

RishikeshDarandale avatar May 25 '20 06:05 RishikeshDarandale

@miguel-a-calles-mba , any update on this pull request?

RishikeshDarandale avatar May 29 '20 12:05 RishikeshDarandale

@RishikeshDarandale This PR is set for 5.4.0.

miguel-a-calles-mba avatar Jun 02 '20 01:06 miguel-a-calles-mba

sure @miguel-a-calles-mba

I have rebased my changes with release 5.4.0locally, but 5.4.0 has an package issue. This has to wait till it gets fixed or if you say that it's not an issue, then I can push my rebased changes!

RishikeshDarandale avatar Jun 02 '20 06:06 RishikeshDarandale

@RishikeshDarandale, I resolved the conflicts. Please verify the changes.

miguel-a-calles-mba avatar Jun 21 '20 21:06 miguel-a-calles-mba

Sorry I made a mistake while merging release/5.4.0 into master and removed it. The removal automatically closed that PR. It should now be rebased against the master and fix conflicts.

j0k3r avatar Jan 29 '21 19:01 j0k3r

@RishikeshDarandale any chances to rebase against the master, fix conflicts and tests? Thanks 🙏

j0k3r avatar May 10 '21 19:05 j0k3r

@j0k3r , let me find some time to check this one. It's a year old change now.

RishikeshDarandale avatar May 11 '21 04:05 RishikeshDarandale

Will you have time to update it?

j0k3r avatar Nov 21 '21 21:11 j0k3r