diff --git a/browser.js b/browser.js new file mode 100644 index 000000000..9f9341efc --- /dev/null +++ b/browser.js @@ -0,0 +1,9 @@ +const browser = typeof window !== 'undefined'; +const webpack = !!process.env.__DISCORD_WEBPACK__; + +const Discord = require('./'); + +module.exports = Discord; +if (browser && webpack) window.Discord = Discord; // eslint-disable-line no-undef +// eslint-disable-next-line no-console +else if (!browser) console.warn('Warning: Attempting to use browser version of Discord.js in a non-browser environment!'); diff --git a/src/client/Client.js b/src/client/Client.js index ae40d6774..7e1df3dc5 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -1,4 +1,3 @@ -const os = require('os'); const EventEmitter = require('events'); const Constants = require('../util/Constants'); const Permissions = require('../util/Permissions'); @@ -265,7 +264,7 @@ class Client extends EventEmitter { * @readonly */ get browser() { - return os.platform() === 'browser'; + return typeof window !== 'undefined'; } /** diff --git a/src/client/websocket/WebSocketConnection.js b/src/client/websocket/WebSocketConnection.js index 436988a4d..5620e480e 100644 --- a/src/client/websocket/WebSocketConnection.js +++ b/src/client/websocket/WebSocketConnection.js @@ -1,4 +1,4 @@ -const browser = require('os').platform() === 'browser'; +const browser = typeof window !== 'undefined'; const EventEmitter = require('events'); const Constants = require('../../util/Constants'); const zlib = require('zlib'); diff --git a/src/index.js b/src/index.js index d4f05a60f..9d1f78fa0 100644 --- a/src/index.js +++ b/src/index.js @@ -57,5 +57,3 @@ module.exports = { VoiceChannel: require('./structures/VoiceChannel'), Webhook: require('./structures/Webhook'), }; - -if (require('os').platform() === 'browser') window.Discord = module.exports; // eslint-disable-line no-undef diff --git a/webpack.config.js b/webpack.config.js index 55d0e12f6..db75a5e23 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -12,6 +12,11 @@ const createConfig = options => { const plugins = [ new webpack.DefinePlugin({ 'global.GENTLY': false }), new webpack.optimize.ModuleConcatenationPlugin(), + new webpack.DefinePlugin({ + 'process.env': { + __DISCORD_WEBPACK__: '"true"', + }, + }), ]; if (options.minify) plugins.push(new UglifyJSPlugin({ uglifyOptions: { output: { comments: false } } })); @@ -19,7 +24,7 @@ const createConfig = options => { const filename = `./webpack/discord${process.env.VERSIONED === 'false' ? '' : '.' + version}${options.minify ? '.min' : ''}.js`; // eslint-disable-line return { - entry: './src/index.js', + entry: './browser.js', output: { path: __dirname, filename,