moleculer
moleculer copied to clipboard
Ease bundlers job
Is your feature request related to a problem? Please describe. Currently when bundling a Moleculer app with esbuild, it ends up with error messages like this:
> node_modules/moleculer/src/serializers/proto/packets.proto.js:4:24: error: Could not resolve "protobufjs/minimal" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
4 │ var $protobuf = require("protobufjs/minimal");
╵ ~~~~~~~~~~~~~~~~~~~~
> node_modules/moleculer/src/serializers/avro.js:13:22: error: Could not resolve "avsc" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
13 │ const avro = require("avsc");
╵ ~~~~~~
> node_modules/moleculer/src/serializers/thrift/gen-nodejs/packets_types.js:9:21: error: Could not resolve "thrift" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9 │ let thrift = require("thrift");
╵ ~~~~~~~~
This is not the case for serializers like notepack
.
This can be easily tackled by declaring these modules to esbuild
as external
but it would be even easier for the user if the same try/catch
logic was applied for all serializers and thus decrease the friction of integrating moleculer
in existing production pipelines.
Describe the solution you'd like
Currently avsc
, protobufjs
and thrift
serializers require
are done twice:
- a first time to check the serializer presence (protected by a
try/catch
) - a later second time to use the serializer (not protected by a
try/catch
)
It seems easy to pass the value returned by the first require to the context where it is used a second time and thus avoid the "unprotected" second require.
Describe alternatives you've considered
Bundlers usually detect the fact that a require is not protected by looking at the AST.
esbuild
could also dig deeper and infer that the second require will not be reached if the first one does not succeed but this is a far cry from what bundlers usually check.
Additional context I can suggest a pull request for this, I just need to know if this seems a valid request to consider in the scope of the Moleculer project.
For the reason why we are using esbuild
in the context of a NodeJS project, please look at https://github.com/vercel/pkg/issues/782#issuecomment-842509782.
The protobuf and thrift generates codes which contains the second require. We can't wrap it with a try-catch block because the next generation will remove them again.
Hello @icebob, thank you for shedding some light on the subject !
The protobuf and thrift generates codes which contains the second require. We can't wrap it with a try-catch block because the next generation will remove them again.
Just to be sure I understand rightfully, I'll just paraphrase with my own words: this part of the code is generated by a process that is maintained outside of the scope of Moleculer
(typically by using something from the protobuf
and thrift
projects).
If it is not to much to ask, would you mind pointing me the code which is executing this generation process (just for my general knowledge)?
The src/serializers/proto/packets.proto.js
generated by pbjs
with npm run proto
command.
The src/serializers/thrift
code generated by thrift cli with npm run thrift
(you should install thrift manually).
Hello, I just saw I forgot this issue. I'll close it for now, feel free to reopen or open a new one it if the situation evolves.