Skip to content

Commit

Permalink
fix(browser support): correct browser detection for webpack (mqttjs#1135
Browse files Browse the repository at this point in the history
)

currently webpack based projects couldn't import mqtt.js because incorrect browser detection.
It's not a problem for react, where node-shims enabled, but a pain for angular (where enabling node-shims isn't supported officially)
To fix that issue we need to check if `process` exists (it will be not available in browser)
Also due to shims in webpack we need to check are we in process of bundling, or not.
  • Loading branch information
nosovk authored and Cédric von Allmen committed Nov 27, 2020
1 parent 7936ae2 commit 3374581
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/connect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var debug = require('debug')('mqttjs')

var protocols = {}

if (process.title !== 'browser') {
// eslint-disable-next-line camelcase
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ === 'function') {
protocols.mqtt = require('./tcp')
protocols.tcp = require('./tcp')
protocols.ssl = require('./tls')
Expand Down
4 changes: 2 additions & 2 deletions lib/connect/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var WSS_OPTIONS = [
'pfx',
'passphrase'
]
var IS_BROWSER = process.title === 'browser'

// eslint-disable-next-line camelcase
var IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'
function buildUrl (opts, client) {
var url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
if (typeof (opts.transformWsUrl) === 'function') {
Expand Down

0 comments on commit 3374581

Please sign in to comment.