Hey all ... need some help/guidance with bundling ...
# help
c
Hey all ... need some help/guidance with bundling in sst please? Trying to bundle in some json files into my stack, but even with the bundle prop/option set in the main app it doesn't seem to bundle in the config folder... Any help or direction would be greatly appreciated!
Copy code
bundle: {
            copyFiles: [{
                from: `src/config/`,
                to: `src/config/`
            }] 
        }
b
Looks like the implementation of
copyFiles
is in nodeBuilder, which doesn’t get called when running locally. Anyone found a solution for this?
t
The copyFiles is meant only when running the lambdas in production. Locally you shouldn't need to copy anything since it's all available
esbuild supports importing
json
directly and will bundle it so you shouldn't need to do anything special afaik
However I typically don't specify things as json and instead export it as a js object and import that
b
Yea, I know about the resolution setting in tsconfig, thing is here we’re loading the JSON in from a file, not importing it as a module. When the lambda is hit it loads a config file base on a request parameter - so the filename is a dynamic property. This is where we’re getting errors - it cant find the json file at the path we because the config files are in a
stage
named folder. The copyFiles operation drops the config files for the current stage in to the parent folder so we don’t end up with all of the stage configs in the deployed output.
Logging the path out at runtime his is what I’m seeing:
Copy code
<project-root>/.build/src/path/to/<stage>/config.json
.build/src
doesn’t contain the config files 😞
f
Hey @Brinsley @Carlos Ribeiro I created a sample repo that uses
copyFiles
to bundle a json config file with the Lambda function. https://github.com/fwang/sst-copy-files-test
On
sst start
, the json config is not copied b/c it exists locally. And on
sst deploy
the json config gets copied to the same location as local.
If this doesn’t work with your setup, can you show me your folder structure, and how you are referencing the file inside your Lambda function?
b
@Frank sure. I’ll check this out 🙂
Will it make a difference that you’re using js for lambda and not ts?
You legend, @Frank! I was using
path.resolve
with
__dirname
to grab the config location - this was what was killing it. Using the path relative to the root of the project saw it fixed. Appreciate the effort on this mate!
f
Nice!