Skip to content

Commit 3d6c3be

Browse files
authored
feat: esm version dist/mqtt.esm.js and replace browserify with esbuild (#1731)
1 parent 69c51c6 commit 3d6c3be

File tree

8 files changed

+1199
-4545
lines changed

8 files changed

+1199
-4545
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/dist/
55
/build/
66

7-
*.js
7+
*.js
8+
*.mjs

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,20 @@ Closes the Store.
804804

805805
## Browser
806806

807-
MQTT.js is bundled using [browserify](http://browserify.org/). You can find the browser build in the `dist` folder.
807+
MQTT.js is bundled using [esbuild](https://esbuild.github.io/). It is tested working with all bundlers like Webpack, Vite and React.
808+
809+
You can find all mqtt bundles versions in `dist` folder:
810+
811+
- `mqtt.js` - iife format, not minified
812+
- `mqtt.min.js` - iife format, minified
813+
- `mqtt.esm.js` - esm format minified
814+
815+
In order to import them use one of the following:
808816

809817
```js
818+
import * as mqtt from 'mqtt/dist/mqtt'
810819
import * as mqtt from 'mqtt/dist/mqtt.min'
820+
import mqtt from 'mqtt/dist/mqtt.esm'
811821
```
812822

813823
<a name="cdn"></a>
@@ -824,10 +834,12 @@ See <http://unpkg.com> for the full documentation on version ranges.
824834

825835
### Webpack
826836

827-
If you are using webpack simply import MQTT.js like this:
837+
If you are using webpack simply import MQTT.js in one of the following ways:
828838

829839
```js
840+
import * as mqtt from 'mqtt/dist/mqtt'
830841
import * as mqtt from 'mqtt/dist/mqtt.min'
842+
import mqtt from 'mqtt/dist/mqtt.esm'
831843
```
832844

833845
<a name="vite"></a>
@@ -837,7 +849,9 @@ import * as mqtt from 'mqtt/dist/mqtt.min'
837849
If you are using vite simply import MQTT.js like this:
838850

839851
```js
852+
import * as mqtt from 'mqtt/dist/mqtt'
840853
import * as mqtt from 'mqtt/dist/mqtt.min'
854+
import mqtt from 'mqtt/dist/mqtt.esm'
841855
```
842856

843857
<a name="qos"></a>

esbuild.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { build } = require('esbuild')
2+
const { polyfillNode } = require('esbuild-plugin-polyfill-node');
3+
const { rimraf } = require('rimraf')
4+
const fs = require('fs')
5+
6+
const outdir = 'dist'
7+
8+
/**
9+
* @type {import('esbuild').BuildOptions}
10+
*/
11+
const options = {
12+
entryPoints: ['build/mqtt.js'],
13+
bundle: true,
14+
outfile: `${outdir}/mqtt.js`,
15+
format: 'iife',
16+
platform: 'browser',
17+
globalName: 'mqtt',
18+
define: {
19+
'global': 'window'
20+
},
21+
plugins: [
22+
polyfillNode({
23+
polyfills: [
24+
'readable-stream'
25+
]
26+
}),
27+
],
28+
}
29+
30+
async function run() {
31+
const start = Date.now()
32+
await rimraf(outdir)
33+
await build(options)
34+
35+
options.minify = true
36+
options.outfile = `${outdir}/mqtt.min.js`
37+
await build(options)
38+
39+
40+
options.outfile = `${outdir}/mqtt.esm.js`
41+
options.format = 'esm'
42+
43+
await build(options)
44+
45+
console.log(`Build time: ${Date.now() - start}ms`)
46+
console.log('Build output:')
47+
48+
// log generated files with their size in KB
49+
const files = fs.readdirSync(outdir)
50+
for (const file of files) {
51+
const stat = fs.statSync(`${outdir}/${file}`)
52+
console.log(`- ${file} ${Math.round(stat.size / 1024 * 100) / 100} KB`)
53+
}
54+
}
55+
56+
run().catch((e) => {
57+
console.error(e)
58+
process.exit(1)
59+
})

0 commit comments

Comments
 (0)