not-an-aardvark / snoowrap

A JavaScript wrapper for the reddit API
MIT License
1.01k stars 125 forks source link

Incompatible with Vite #347

Open SinBirb opened 2 years ago

SinBirb commented 2 years ago

Hi, thanks for the nice wrapper.

Unfortunately it's incompatible with Vite,, because snoowrap uses the node libraries global and url e.g. here:

global[MODULE_NAME] = this._previousSnoowrap;

The main Vite/Vue developer says, packages shouldn't require node globals: https://github.com/vitejs/vite/issues/728#issuecomment-767168681

That makes sense, considering node libraries are only available in the browser by the use of bundlers like webpack.

KenEucker commented 2 years ago

Hey there, @SinBirb. I thought I would add a comment here to say that if you compile the code that uses snoowrap with webpack for UMD, it should work with vite.

I have an API that uses snoowrap which is compiled to UMD and used in the browser in my project which is built by vite.

iMrDJAi commented 2 years ago

You don't use this module on a browser environment directly, you should compile it or use the compiled version.

iMrDJAi commented 2 years ago

@SinBirb I managed to get it working with Vite on my fork, and now it's used officially as a dev server.

The fix wasn't hard tho, I used this config to ignore node builtins in browser:

var path = require('path')
var noop = path.resolve('./noop.js')

/** @type {import('vite').UserConfig} */
module.exports = {
  resolve: {
    alias: {
      fs: noop,
      path: noop,
      stream: noop,
      url: noop,
      ws: noop
    }
  }
}

Note that noop.js exports undefined, so I had to mess with imports and write further checks to ensure that node builtins won't be used on browser environments.

Snoowrap 2.0 will support Vite, work in progress...