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

Noncritical errors when bundling with webpack #212

Open
joeyvanlierop opened this issue Dec 28, 2020 · 3 comments
Open

Noncritical errors when bundling with webpack #212

joeyvanlierop opened this issue Dec 28, 2020 · 3 comments

Comments

@joeyvanlierop
Copy link

joeyvanlierop commented Dec 28, 2020

I am using the aws handler and when running using serverless-offline, I am getting the following errors:

Module not found: Error: Can't resolve 'hapi/package.json' in 'C:\...node_modules\grant'
 @ C:/.../node_modules/grant/grant.js 15:16-44 42:14-42
 @ C:/.../api/functions.ts'
ERROR in C:/.../node_modules/grant/grant.js
Module not found: Error: Can't resolve 'koa/package.json' in 'C:\...\node_modules\grant'
 @ C:/.../node_modules/grant/grant.js 15:16-44 42:14-42
 @ C:/.../api/functions.ts
Error from chokidar (C:\node_modules): Error: EBUSY: resource busy or locked, lstat 'C:\hiberfil.sys'
Error from chokidar (C:\node_modules): Error: EBUSY: resource busy or locked, lstat 'C:\pagefile.sys'
Error from chokidar (C:\node_modules): Error: EBUSY: resource busy or locked, lstat 'C:\swapfile.sys'

Here is a condensed version of the code I am using:

import { APIGatewayProxyHandler } from "aws-lambda";
import grant from "grant";

const grantHandler = grant.aws({
  config: {
    defaults: {
      origin: "http://localhost:4000",
      transport: "state",
    },
    github: {
      key: "xxx",
      secret: "xxx",
      scope: ["user"],
      redirect_uri: "http://localhost:4000/dev/connect/github/callback",
    },
  },
  session: { secret: "grant" },
});

export const login: APIGatewayProxyHandler = async (event) => {
  const { redirect, response } = await grantHandler(event);
  return redirect || createResponse(200, JSON.stringify(response));
};

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 🎆 🎉

@joeyvanlierop
Copy link
Author

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.

@joeyvanlierop joeyvanlierop changed the title Noncritical errors when using the aws handler with serverless-offline Noncritical errors when using aws handler Dec 28, 2020
@simov
Copy link
Owner

simov commented Dec 29, 2020

Thanks for the detailed bug report 👍

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.

@joeyvanlierop joeyvanlierop changed the title Noncritical errors when using aws handler Noncritical errors when bundling with webpack Dec 31, 2020
@simov
Copy link
Owner

simov commented Mar 20, 2021

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',
}

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

No branches or pull requests

2 participants