Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access static files (templates) #32

Closed
hlegendre opened this issue Feb 2, 2017 · 11 comments
Closed

Access static files (templates) #32

hlegendre opened this issue Feb 2, 2017 · 11 comments

Comments

@hlegendre
Copy link

Hi,

I am trying to deploy a serverless function that requires access to liquid templates.
I managed to include the static files in the bundle through the "includePath" config parameters but I am unable to get a path to them.

This is because the node process calling the handler is not in the same folder as the handler itself. Hence I cannot find a path to the handler (that I can then prefix to the path of my templates) to fs.readFile them... I tried using __dirname, but Browserify changes it while bundling to the path at the time of compilation...

Would you have any idea of how to make it work, please ?

PS: I explored the https://github.com/JohnPostlethwait/stringify way but I cannot define additional transforms to browserify in serverless-plugin-optimize, can I ?

BR,

Hugues

@goncaloneves
Copy link
Contributor

@hlegendre did you try relative paths?

@hlegendre
Copy link
Author

@goncaloneves yes. That's what I tried first. But the node process is launched from /var/task whereas the handler.js file is in a deep folder _optimize/XXX/. Therefore any relative path is relative to /var/task and not the handler.js containing folder... And I don't know beforehand this folder.

@goncaloneves
Copy link
Contributor

goncaloneves commented Feb 2, 2017 via email

@hlegendre
Copy link
Author

I don't think the structure of my project has an impact since your plugin optimizes everything (except the textual templates I include as well) into a single js file. The output is therefore something along

/_optimize/xxx/handler.js
/_optimize/xxx/templateFolder/*.liquid

where xxx is the name of the function.

I did not want to rely on opening the liquid files through /var/task/_optimize/xxx/templateFolder/yyy.liquid because I need to specify the name of the function in the routine opening them and this would not allow me to use templates with several function (I would not know which xxx to chose).

I was hoping there would be a way to get the path to handler.js directly from the lambda function but process.cwd() is /var/task and __dirname gets replaced by Browserify by the local path at the time of bundling.

I guess there is no apparent way to do it. I'll probably try something dirty like exploring the /var/task folder which should contain only on subFolder, named as my function...

Regarding the PR, I do not know much about Browserify so I am not sure I would be able to help but I'll have a look.

Thanks anyway.

@goncaloneves
Copy link
Contributor

goncaloneves commented Feb 3, 2017 via email

@hlegendre
Copy link
Author

Since everything is "optimized" by your plugin, I get a single handler.js file which is then deployed to /var/task/_optimize/${functionName}/handler.js

Any additional folders I chose to copy using the includePaths options gets copied to /var/task/_optimize/${functionName}/ as well. But the handler.js file is "required" (or launched) by AWS Lambda from the /var/task/ folder. Therefore any fs.readFile with a relative path looks for the file from this folder and not from the handler.js folder.

I would not have problems with require directives since node handles this well (path is relative to the script file having the call and not the CWD). I have problems with fs.readFile.

I managed to make it work by using path.resolve(process.env.LAMBDA_TASK_ROOT, '_optimize', process.env.AWS_LAMBDA_FUNCTION_NAME, p) to get the full path when I am on AWS Lambda.

@goncaloneves
Copy link
Contributor

Hugues that's perfect. 3 issues opened about this and you brought the solution. 👍

I will add this information to the readme file.

@hlegendre
Copy link
Author

you're welcome !

@ro-savage
Copy link

For anyone looking at this, remember that if you are using subfolders the asset will get deployed to them and that needs to be taken into account.

E.g.

  myLambda:
    handler: mySubFolder/myLambda.handler
    optimize:
      includePaths: ['mySubFolder/myFile.json']
path.resolve(process.env.LAMBDA_TASK_ROOT, '_optimize', process.env.AWS_LAMBDA_FUNCTION_NAME, 'mySubFolder/myFile.json')

@GoChartingAdmin
Copy link

GoChartingAdmin commented Feb 13, 2018

Do we include the below line in serverless.yml?

path.resolve(process.env.LAMBDA_TASK_ROOT, '_optimize', process.env.AWS_LAMBDA_FUNCTION_NAME, 'mySubFolder/myFile.json')

And what if my function depends on multiple json files?

@ro-savage
Copy link

@GoChartingAdmin, that is the line you include when importing it into the serverless function.

e.g.

const path = require('path')
const myJson = require(path.resolve(process.env.LAMBDA_TASK_ROOT, '_optimize', process.env.AWS_LAMBDA_FUNCTION_NAME, 'mySubFolder/myFile.json'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants