Skip to content

Commit 80e29a9

Browse files
authored
fix: make import mqtt from 'mqtt' work in browsers (#1734)
1 parent 71e6da7 commit 80e29a9

File tree

6 files changed

+81
-38
lines changed

6 files changed

+81
-38
lines changed

.airtap.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

README.md

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,8 @@ The callback is called when the packet has been removed.
801801
Closes the Store.
802802

803803
<a name="browser"></a>
804+
<a name="webpack"></a>
805+
<a name="vite"></a>
804806

805807
## Browser
806808

@@ -812,7 +814,15 @@ You can find all mqtt bundles versions in `dist` folder:
812814
- `mqtt.min.js` - iife format, minified
813815
- `mqtt.esm.js` - esm format minified
814816

815-
In order to import them use one of the following:
817+
Starting from MQTT.js > 5.2.0 you can import mqtt in your code like this:
818+
819+
```js
820+
import mqtt from 'mqtt'
821+
```
822+
823+
This will be automatically handled by your bundler.
824+
825+
Otherwise you can choose to use a specific bundle like:
816826

817827
```js
818828
import * as mqtt from 'mqtt/dist/mqtt'
@@ -830,30 +840,6 @@ See <http://unpkg.com> for the full documentation on version ranges.
830840

831841
**Be sure to only use this bundle with `ws` or `wss` URLs in the browser. Others URL types will likey fail**
832842

833-
<a name="webpack"></a>
834-
835-
### Webpack
836-
837-
If you are using webpack simply import MQTT.js in one of the following ways:
838-
839-
```js
840-
import * as mqtt from 'mqtt/dist/mqtt'
841-
import * as mqtt from 'mqtt/dist/mqtt.min'
842-
import mqtt from 'mqtt/dist/mqtt.esm'
843-
```
844-
845-
<a name="vite"></a>
846-
847-
### Vite
848-
849-
If you are using vite simply import MQTT.js like this:
850-
851-
```js
852-
import * as mqtt from 'mqtt/dist/mqtt'
853-
import * as mqtt from 'mqtt/dist/mqtt.min'
854-
import mqtt from 'mqtt/dist/mqtt.esm'
855-
```
856-
857843
<a name="qos"></a>
858844

859845
## About QoS

examples/vite-example/src/App.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<script setup>
22
import { ref } from 'vue'
3-
import { connect } from 'mqtt/dist/mqtt.min'
3+
import mqtt from 'mqtt'
4+
5+
console.log('mqtt', mqtt)
46
57
const connected = ref(false)
68
7-
const client = connect('wss://test.mosquitto.org:8081');
9+
const client = mqtt.connect('wss://test.mosquitto.org:8081');
810
911
const messages = ref([])
1012

package-lock.json

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"url": "git://github.com/mqttjs/MQTT.js.git"
2222
},
2323
"main": "./build/mqtt.js",
24+
"module": "./dist/mqtt.esm.js",
2425
"bin": {
2526
"mqtt_pub": "./build/bin/pub.js",
2627
"mqtt_sub": "./build/bin/sub.js",
@@ -35,7 +36,13 @@
3536
"src/"
3637
],
3738
"exports": {
38-
".": "./build/mqtt.js",
39+
".": {
40+
"browser": {
41+
"import": "./dist/mqtt.esm.js",
42+
"default": "./dist/mqtt.js"
43+
},
44+
"default": "./build/mqtt.js"
45+
},
3946
"./package.json": "./package.json",
4047
"./*.map": "./build/*.js.map",
4148
"./dist/*": "./dist/*.js",
@@ -95,7 +102,7 @@
95102
"node": ">=16.0.0"
96103
},
97104
"browser": {
98-
"./mqtt.js": "./build/mqtt.js",
105+
"./mqtt.js": "./dist/mqtt.js",
99106
"fs": false,
100107
"tls": false,
101108
"net": false
@@ -110,6 +117,7 @@
110117
"help-me": "^4.2.0",
111118
"lru-cache": "^10.0.1",
112119
"minimist": "^1.2.8",
120+
"mqtt": "^5.2.0",
113121
"mqtt-packet": "^9.0.0",
114122
"number-allocator": "^1.0.14",
115123
"readable-stream": "^4.4.2",

test/browser/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import mqtt from '../../dist/mqtt.esm.js';
2+
import mqtt from '../../'; // this will resolve to mqtt/dist/mqtt.esm.js
33

44
// needed to test no-esm version /dist/mqtt.js
55
/** @type { import('../../src/mqtt').MqttClient }*/

0 commit comments

Comments
 (0)