Open geovanisouza92 opened 6 years ago
Hi @geovanisouza92 , the whole entries mechanism is needed to pickup the right entry again after the compile has been finished and associate the entry name returned from Webpack together with the compile results back to the right serverless function that the compile is for.
I remember that someone copied his custom entries modification into a GitHub issue, prepending the source-map-install and that worked. I do not remember which issue that was but I believe, that he did not add arrays to the entries object, but did something else. I will try to find that again and we can see if that would work here too. If it works, this should be added to the documentation, if it does not work, we have to figure out how to control the entry point name, to be able to use it on the other side of the compile to have the sls function association working. Optimally, this should not break transpiling loaders like TS or Elm.
I'll provide feedback as soon as I've found the comment.
Hi @HyperBrain, thanks for the reply!
Well, from the template I generated the project, it already include arrays at entry.
EDIT: What I mean in my suggestion above is that, picking the key will result in the same result as picking the value and manipulating it. For instance:
On my serverless.yml I have a ping
function, pointing to src/functions/ping.handler
as handler function. This is translated in serverless-webpack
to an entry like "src/function/ping": "src/function/ping.ts"
; the value (src/function/ping.ts
) have its extension (.ts) removed; the allEntryFunctions
is generated from serverless handlers, in this case src/function/ping.handler
where, again, the "extension" (.handler) is stripped out, remaining src/functions/ping
as key to serverless function config.
So, as both places use the same src/functions/ping
, maybe we could just use that, ignoring which module(s) compose that entry on webpack itself.
Am I missing some thing?
Indeed, as far as I remember, the compilation output references the entry name (key), so the same src/functions/ping
could still be used to match everything.
@geovanisouza92 Thanks for the "edit" š . The most important point is your very last sentence. As long as the reverse mapping from the Webpack compile stats to the serverless function is unambigous, the behavior should not change. I think doing a prototype in a PR to evaluate if it is feasible or not, would be a good step forward, so that we can decide.
So, I started to dig through this implementation and made it work on my fork, but came across a validation that disallow customization like the one I proposed. I didn't noticed this behavior earlier because I was using and older version of serverless-webpack, and since version release v4.1.0 it includes a change described on #272. My suggestion here is that customization should be allowed, but warned, instead throwing a validation error.
In fact, looking at the code I noticed that:
slsw.lib.entries
(which misses the point, if customization is disallowed);entries
, which could possibly be changed by webpack.config.js
, as my use case described above.My changes so far include:
lib.entries
after webpackConfig
was required;Possible changes:
webpack.config.js
. Example:P.S.: Maybe an alternative is to adjust forceInclude
allowing relative paths in addition to module names.
Hi @geovanisouza92 , thanks for the good and detailed proposal š
As you found correctly, the manual intervention of the entries is blocked since v4. The reason for that is, that for stability everything should be handled by slsw.lib.entries, especially for individual packaging. Turning that into a warning will allow users to break their deployments silently and will decrease the overall plugin stability. So the manual configuration has been deprecated and is not supported anymore.
Additionally, allowing the user to set arbitrary will add much technical debt, because the plugin would have to handle each possible case (which can even change when webpack versions change).
My suggestion here is, to keep the error, but add other fully controlled mechanisms to add arbitrary modules to a function. The whole approach of the configuration in Serverless and its service definition is declarative and so should be the plugin. Your sample at the end would offer the functionality while keeping the declarativeness of the configuration. We should continue to force to set slsw.lib.entries as entries.
What if we just add the possibility to declare additional modules that will be pre- or appended on a function and service level in the serverless.yml. This will solve the issue completely and is easy to understand for users. Any function based config would override the service configuration if any exists. The declaration should never be able to "exchange" the main function module, because that also guarantees the stability of the plugin. An example would be:
# service based config
custom:
webpack:
entry:
pre:
- source-map-support.js
post:
- myAdditionalModule.js
# function override
functions:
foo:
handler: foo.handler
bar:
handler: bar.handler
webpack:
entry:
pre:
- ./injected-dependency.js
post:
- ./foo.js
All configurations are optional.
IMO this is the only change/extension needed, to have it fully configurable in a deterministic way. What do you think?
+1000 to entry prepend/append entries per function.
I was thinking today about using forceInclude
to achieve this, but a separate config is better IMO.
I'll work on that later.
So, I've updated my fork following this idea of pre/post (actually prepend/append).
Should we support a "global" configuration on custom.webpack.entry
? In that case, what should be the precedence?
Yes. As I wrote, the hierarchy is global <- function, i.e. the global config is applied, then the function config if it is present. The effective array contents should then be merged to pertain the order (global then function)
As the plugin configuration is abstracted into the Configuration
object, it should set empty arrays as defaults (the same as for the forceXXXX configs) and make the entry object available as getter (so that it is read-only).
ANy progress on this? Or a workaround?
any workaround?
How does this operate if the functions are using differing runtimes? Eg. I have one function running node and one running python. I can't supply slsw.lib.entries, because webpack can't compile a python function
This is a Bug Report
Description
I'm customizing entries on webpack to inject common funcionality, like source-map support and tracing modules. If I package functions individually, it does not work.
I tracked the issue to this line where the entry value is an array, and the path module is applied incorrectly.
If I understood correctly:
entry
receives the relative filename of each handlerentryFile
receivesentry
without extensionentryFile
is used to pick the right entry function fromallEntryFunctions
allEntryFunctions
is generated from the entries, wherehandlerFile
is generated from function handlers onserverless.yml
.So, we could just use the
key
that is the entrypoints based on serverless "handler" configurations, and the problem will be solved.For bug reports:
When I enabled individual packaging and use multiple modules for each entry, it throw the error "Path must be a string. Received [ './source-map-install.js', (redacted) ]"
The commands
sls invoke local
andsls webpack
working fine.serverless.yml
webpack.config.js
Additional Data