HubSpot / offline

Automatically display online/offline indication to your users
http://github.hubspot.com/offline/docs/welcome
MIT License
8.65k stars 847 forks source link

CommonJS/AMD support #127

Open anthonator opened 9 years ago

anthonator commented 9 years ago

Similar to the comments after #92 was closed. Add CommonJS/AMD wrapper to support us browserify/webpack users.

jsmylnycky commented 9 years ago

+1. Would love to be able to use this with webpack easier.

fskinner commented 9 years ago

+1

peacechen commented 8 years ago

+1

Global vars are for the birds

rawls238 commented 8 years ago

https://github.com/HubSpot/offline/pull/75/files

I think this PR should have done the trick? Let me know if there's anything else needed to close this.

peacechen commented 8 years ago

The issue is that the library sets window.Offline at the bottom of the code. To properly support modules and avoid globals, the code should be wrapped in an IIFE and never reference window directly. Here's an example that detects the environment it's being loaded in and exports either a module or a global --

http://bob.yexley.net/umd-javascript-that-runs-anywhere/

That references a repo of template examples at https://github.com/umdjs/umd

You probably want to use returnExports.js so that it'll continue to work for existing global users.

ghost commented 8 years ago

+1!

geoffcallender commented 8 years ago

If it's AMD loaded then it will probably run after DOMContentLoaded better - so this issue will have to be catered for: https://github.com/HubSpot/offline/issues/209 .

throrin19 commented 8 years ago

+1 it's really bad because now many people create front code with browserify to have commonJS support or RequireJS for AMD support

nneal commented 7 years ago

+1

I was able to get this working through

const Offline = require('exports?Offline!offline-js/offline'); or import Offline from 'exports?Offline!offline-js/offline'; at the top of my file and

"exports-loader": "0.6.3", in my package.json

hope that helps!

throrin19 commented 7 years ago

With browserify, I use offline.js like this and it works (but it's too bad to pass by window) :

require('offline-js/js/offline');

window.Offline.check();
FDiskas commented 7 years ago

:factory: Uncaught ReferenceError: exports is not defined