Closed mpontus closed 1 year ago
Hello @mpontus
can you please create a github project with a simple example? In order to understand better what you are trying to achieve!
thanks!
Hi again. Sorry for the long wait.
I created this example to demonstrate usage of node-dependency-injection in a project building with rollup: https://github.com/mpontus/rollup-node-dependency-injection
While building the project, rollup transforms the source code and external dependencies into a single javascript file in CommonJS format. The output bundle dist/index.cjs.js
only contains code that has been explicitly imported from the entry file or its descendants. This does not include the service file src/services.yaml
, which is only referenced by path as an argument to YamlFileLoader
.
Because of this, attempting to run the bundle results in ServiceFileNotFoundException
. This problem can be mitigated by copying the service file to the output directory as part of the build process. However, because bundling flattens the structure of the source code, classes referenced from the service file will not be found at their original paths and are not guaranteed to be present in the bundle at all.
Of course it's not at all surprising that paths inside the service file would only be useful if they reflect the structure of the code being evaluated, which will not be the case if a bundler is used to strip the structure away. I don't think there is a solution for this problem that only changes loaders' behavior at runtime. However, it may be possible to have the bundler interpret the service configuration file: transforming it or generating code for container configuration.
I don’t know if this issue is related with ndi itself or is related with other transpilation issue.
Hi. I really enjoy the declarative syntax this library provides, specifically when used with YAML configuration files.
I've been trying to use it in a project built with AWS CDK which has the ability to transpile TS code and deploy it as a serverless function to AWS Lambda environment. Under the hood, @aws-cdk/aws-lambda-nodejs is using esbuild to bundle source code and external modules into a single JS file which is then published to an S3 bucket. It functions similarly to other bundlers, such as [webpack] or [parcel], commonly used to build projects for web browsers.
I'm facing a roadblock, where
YamlFileLoader#load
fails to find YAML configuration after being deployed to AWS. This happens because bundlers collect code referenced in therequire
-tree, so only the files referenced in the entry file and its descendants make it into the deployed bundle, and filesystem operations on files outside the bundle will fail at runtime.It's possible to use loaders to
require
arbitrary files as text, which could then be passed toYamlFileLoader
for parsing.YamlFileLoader
does not currently have functionality for loading contents of the YAML file directly, but even if it would not alleviate the problem completely, the class files referenced in the service configuration file would also need to be made available at runtime.I'd like to ask for your opinion on what it would take to implement support for declarative configuration when bundlers are used to build the project. I see two potential solutions, but perhaps this discussion will offer more:
Implement a more complex loader for specific bundlers that will be aware of the shape of the service configuration file and will include referenced classes into the bundle during processing.
Have
JsFileLoader
accept JSON-object of the shape of the service configuration file, which may have a class object as aclass
property instead of a path to the file defining the class.Looking forward to hearing your thoughts on this and thank you for your amazing work on this library!