serverless-webpack
serverless-webpack copied to clipboard
fix: implemented the multiple webpack compile config
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
isfalse
, thenoutput.path
for each config provided should match. - If
package.individually
istrue
, then compile config should be provided for each function and theoutput.path
of compile should end withfunction-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
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!
@HyperBrain , Can you please review?
@HyperBrain , any comment/update on this?
@HyperBrain, any luck?
@HyperBrain , Sorry to ping you one more time. Did you get a chance to look into this one?
:+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 , 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.
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.
@miguel-a-calles-mba Can you please review this wrt master
or release/5.3.2
?
Please see here for more comments on #560 .
@miguel-a-calles-mba , any update on this pull request?
@RishikeshDarandale This PR is set for 5.4.0.
sure @miguel-a-calles-mba
I have rebased my changes with release 5.4.0
locally, 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, I resolved the conflicts. Please verify the changes.
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.
@RishikeshDarandale any chances to rebase against the master, fix conflicts and tests? Thanks 🙏
@j0k3r , let me find some time to check this one. It's a year old change now.
Will you have time to update it?