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

fix: fix browser build for Webpack 5 #11717

Merged
merged 4 commits into from Apr 28, 2022
Merged

Conversation

vkarpov15
Copy link
Collaborator

Re: #11584

Summary

With #11584, there were a few issues that popped up in the browser build:

  1. Missing Buffer
  2. Missing process
  3. Looks like Babel was failing on isAsyncFunction()

Examples

@vkarpov15 vkarpov15 self-assigned this Apr 27, 2022
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! 👍

// for a simple check.
try {
asyncFunctionPrototype = Object.getPrototypeOf(async function() {});
} catch (err) {}

module.exports = function isAsyncFunction(v) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could avoid a potential performance penalty by checking the value of asyncFunctionPrototype only once.

module.exports =  asyncFunctionPrototype !== null
? function isAsyncFunction(v) {
  return (
    typeof v === 'function' &&
    Object.getPrototypeOf(v) === asyncFunctionPrototype
  );
}
: function () { return false; };

@Uzlopak
Copy link
Collaborator

Uzlopak commented Apr 27, 2022

What is the actual purpose of the browser build?

@vkarpov15
Copy link
Collaborator Author

@Uzlopak the browser build is supposed to let you run basic schema validation in the browser. The syntax is slightly different: there's no models, you need to define a schema when you create a document. But you can at least define and use Mongoose schemas in the browser. For example:

<html>
  <head>
    <script src="/dist/browser.umd.js"></script>
    <script>
      const schema = new mongoose.Schema({
        name: { type: String, required: true },
        quest: { type: String, match: /Holy Grail/i, required: true },
        favoriteColor: { type: String, enum: ['Red', 'Blue'], required: true }
      });

      doc = new mongoose.Document({
        name: 'Sir Lancelot of Camelot',
        quest: 'To seek the Holy Grail',
        favoriteColor: 'Blue'
      }, schema);
      console.log(doc.validateSync()); // null

      doc.set({
        name: 'Sir Galahad of Camelot',
        quest: 'To seek the Holy Grail',
        favoriteColor: 'Yellow'
      });

      console.log(doc.validateSync()); // ValidationError
    </script>
  </head>
  <body></body>
</html>

@vkarpov15 vkarpov15 added this to the 6.3.2 milestone Apr 28, 2022
@vkarpov15 vkarpov15 merged commit 94d96b8 into master Apr 28, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/fix-webpack-build branch April 28, 2022 17:51
@AbdelrahmanHafez
Copy link
Collaborator

I'm genuinely curious if anyone out there is relying on that.

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

Successfully merging this pull request may close these issues.

None yet

3 participants