Allow whole file javascript variable exports and check for typeof function
See original GitHub issueThis is a Feature Proposal
Allow whole file javascript variable exports.
Description
- Currently entire yml and json files can be referenced in the serverless.yml file like so:
custom: ${file(../myCustomFile.yml)} # You can reference the entire file
However, there are some specific limitations and requirements when it comes to javascript files I find unnecessary.
Attempting to reference a javascript file standalone like…
custom: ${file(../myCustomFile.js)}
…throws an obscure error about split
not being defined. This is because the currently implementation assumes that :
followed by a property will exist.
While this can be overcome by exporting a named module in your javascript file, it is inconsistent with other supported forms of referencing variables.
- The other expectation for referencing variables in javascript files that is not well documented is that it needs to export a function. A simple
typeof
check with a reasonable error message can make this more clear.
This issue requests that referencing entire javascript files be supported and that a reasonable error message be shown if that file does not export a function as expected.
Similar or dependent issues:
- NA
Additional Data
- Serverless Framework Version you’re using: 1.16.0
- Operating System: macOS
- Stack Trace:
TypeError: Cannot read property 'split' of undefined
at Variables.getValueFromFile (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:235:26)
at Variables.getValueFromSource (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:173:19)
at property.match.forEach (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:95:40)
at Array.forEach (native)
at Variables.populateProperty (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:86:43)
at deepMapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:66:45)
at deepMapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:61:14)
at deepMapValuesIteratee (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:51:25)
at /home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13379:38
at /home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:4944:15
at baseForOwn (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:3001:24)
at Function.mapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13378:7)
at deepMapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:59:39)
at deepMapValuesIteratee (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:51:25)
at /home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13379:38
at /home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:4944:15
at baseForOwn (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:3001:24)
at Function.mapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13378:7)
at deepMapValues (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:59:39)
at Variables.populateObject (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:64:5)
at Variables.populateService (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:40:17)
at Serverless.run (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/Serverless.js:92:27)
at serverless.init.then (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/bin/serverless:30:50)
From previous event:
at runCallback (timers.js:672:20)
at tryOnImmediate (timers.js:645:5)
at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
at __dirname (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/bin/serverless:18:28)
at Object.<anonymous> (/home/travis/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/bin/serverless:34:4)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:390:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:505:3
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Yes. It still works and wouldn’t be a breaking change. Just added a branch to handle the additional case
Thanks for confirming @infiniteluke 👍 That’s great! We’ll take a look at the PR ASAP.