Open joeyvanlierop opened 3 years ago
The errors disappear when commenting out the following lines in the main grant.js
file:
function grant ({handler, ...rest}) {
if (handler === 'express') {
var version = 4
}
// else if (handler === 'koa') {
// var version =
// parseInt(require('koa/package.json').version.split('.')[0]) >= 2 ? 2 : 1
// }
// else if (handler === 'hapi') {
// try {
// var pkg = require('@hapi/hapi/package.json')
// }
// catch (err) {
// var pkg = require('hapi/package.json')
// }
// var version = parseInt(pkg.version.split('.')[0]) >= 17 ? 17 : 16
// }
return version
? require(`./lib/handler/${handler}-${version}`)(rest)
: require(`./lib/handler/${handler}`)(rest)
}
grant.express = (options) => {
var version = 4
var handler = require(`./lib/handler/express-${version}`)
return options ? handler(options) : handler
}
// grant.koa = (options) => {
// var version =
// parseInt(require('koa/package.json').version.split('.')[0]) >= 2 ? 2 : 1
// var handler = require(`./lib/handler/koa-${version}`)
// return options ? handler(options) : handler
// }
// grant.hapi = (options) => {
// try {
// var pkg = require('@hapi/hapi/package.json')
// }
// catch (err) {
// var pkg = require('hapi/package.json')
// }
// var version = parseInt(pkg.version.split('.')[0]) >= 17 ? 17 : 16
// var handler = require(`./lib/handler/hapi-${version}`)
// return options ? handler(options) : handler
// }
;[
'fastify', 'curveball',
'node', 'aws', 'azure', 'gcloud', 'vercel'
].forEach((provider) => {
grant[provider] = (options) => {
var handler = require(`./lib/handler/${provider}`)
return options ? handler(options) : handler
}
})
grant.default = grant
module.exports = grant
I am using serverless-bundle which uses serverless-webpack under the hood and I am thinking that webpack might be interacting oddly with grant. Just a thought.
Thanks for the detailed bug report :+1:
Yes, it is an issue with the bundler because Grant loads optional dependencies at runtime where the bundler tries to resolve all require
statements eagerly regardless of logical code structure.
Not sure if or how this will be possible to fix because by design Grant was never meant to be bundled, but lets keep this issue open for now. Probably with an updated title regarding the bundling process.
I did a few improvements on the imports, now all package.json
imports are wrapped in try catch blocks so they should produce warnings only. On top of that I did a few tests on my own and I was able to suppress the Webpack warnings using the following configuration:
var path = require('path')
var webpack = require('webpack')
// suppresses the warnings in the console
var ignore = new webpack.IgnorePlugin({
resourceRegExp: /(koa|hapi)\/package\.json/
})
module.exports = {
entry: './examples/grant.js',
output: {
path: path.resolve(__dirname, './dist'),
filename: 'webpack.bundle.js',
},
target: 'node',
plugins: [ignore],
mode: 'production',
}
I am using the aws handler and when running using serverless-offline, I am getting the following errors:
Here is a condensed version of the code I am using:
Neither of the errors seem to have any impact, however I am wondering if you have noticed this before. I couldn't find any similar issues so this might just be a side-effect of using serverless-offline or something similar. If there is anything else I can do to help please let me know. Thanks and happy holidays 🎆 🎉