From a24cf14654cb0fa74da1be2671dfaf57071fec40 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Fri, 19 Apr 2024 08:52:04 +0200 Subject: [PATCH] fix: wrong mqttjs version printed (#1847) --- esbuild.js | 24 +++++++++++++++++++++--- src/lib/client.ts | 3 ++- src/lib/shared.ts | 3 +++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/esbuild.js b/esbuild.js index b77c12b71..c05b0bfb1 100644 --- a/esbuild.js +++ b/esbuild.js @@ -16,9 +16,6 @@ const options = { format: 'iife', platform: 'browser', globalName: 'mqtt', - define: { - 'process.env.npm_package_version': JSON.stringify(version), - }, sourcemap: false, // this can be enabled while debugging, if we decide to keep this enabled we should also ship the `src` folder to npm plugins: [ polyfillNode({ @@ -34,6 +31,27 @@ const options = { navigator: true, // Needed for WeChat, ref #1789 } }), + { + name: 'resolve-package-json', + setup(build) { + // when importing 'package.json' we want to provide a custom object like { version: '1.2.3' } + + build.onResolve({ filter: /package\.json$/ }, args => { + return { + path: args.path, + namespace: 'package-json' + } + }) + + build.onLoad({ filter: /.*/, namespace: 'package-json' }, args => { + return { + contents: JSON.stringify({ version }), + loader: 'json' + } + } + ) + } + }, ], } diff --git a/src/lib/client.ts b/src/lib/client.ts index 9bad1baf7..d3c108a0a 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -31,6 +31,7 @@ import { ErrorWithReasonCode, GenericCallback, IStream, + MQTTJS_VERSION, StreamBuilder, TimerVariant, VoidCallback, @@ -396,7 +397,7 @@ export interface MqttClientEventCallbacks { * (see Connection#connect) */ export default class MqttClient extends TypedEventEmitter { - public static VERSION = process.env.npm_package_version + public static VERSION = MQTTJS_VERSION /** Public fields */ diff --git a/src/lib/shared.ts b/src/lib/shared.ts index 1fb16c7b5..dca82823b 100644 --- a/src/lib/shared.ts +++ b/src/lib/shared.ts @@ -82,3 +82,6 @@ export const nextTick = : (callback: () => void) => { setTimeout(callback, 0) } + +// eslint-disable-next-line @typescript-eslint/no-var-requires +export const MQTTJS_VERSION = require('../../package.json').version