diff --git a/.gitignore b/.gitignore index c274848..fba9326 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ .vscode/ coverage/ .idea +.DS_Store diff --git a/package.json b/package.json index 2202bd3..3f37f1d 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@babel/runtime": "^7.23.1", "@rollup/plugin-commonjs": "^25.0.5", "@rollup/plugin-node-resolve": "^15.2.3", + "@rollup/plugin-replace": "^6.0.2", "@rollup/plugin-terser": "^0.4.4", "@rollup/plugin-typescript": "^11.1.5", "@types/jest": "^29.5.5", diff --git a/rollup.config.mjs b/rollup.config.mjs index 6b176b4..9fb2001 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -3,6 +3,10 @@ import nodeResolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import terser from '@rollup/plugin-terser'; import nodePolyfills from 'rollup-plugin-node-polyfills'; +import replace from '@rollup/plugin-replace'; +import fs from 'fs'; + +const version = JSON.parse(fs.readFileSync('./package.json', 'UTF-8')).version; export default { input: './src/index.ts', @@ -25,6 +29,10 @@ export default { } ], plugins: [ + replace({ + '__VERSION__': version, + preventAssignment: true + }), typescript({ compilerOptions: { lib: ['es5', 'es6', 'dom'], diff --git a/src/index.test.ts b/src/index.test.ts index 8c4ef3c..fb3677d 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,6 +1,4 @@ import { FetchMock } from 'jest-fetch-mock'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const packageJSON = require('../package.json'); import 'jest-localstorage-mock'; import * as data from './test/testdata.json'; import IStorageProvider from './storage-provider'; @@ -1383,7 +1381,8 @@ test('Should add `x-unleash` headers', async () => { /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; const expectedHeaders = { - 'x-unleash-sdk': `unleash-js@${packageJSON.version}`, + // will be replaced at build time with the actual version + 'x-unleash-sdk': 'unleash-js@__VERSION__', 'x-unleash-connection-id': expect.stringMatching(uuidFormat), 'x-unleash-appname': appName, }; diff --git a/src/index.ts b/src/index.ts index 7e82219..748f00e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,9 +10,7 @@ import { notNullOrUndefined, urlWithContextAsQuery, } from './util'; - -// eslint-disable-next-line @typescript-eslint/no-var-requires -const packageJSON = require('../package.json'); +import { sdkVersion } from './version'; const DEFINED_FIELDS = [ 'userId', @@ -476,7 +474,7 @@ export class UnleashClient extends TinyEmitter { const headers = { [this.headerName]: this.clientKey, Accept: 'application/json', - 'x-unleash-sdk': `unleash-js@${packageJSON.version}`, + 'x-unleash-sdk': sdkVersion, 'x-unleash-connection-id': this.connectionId, 'x-unleash-appname': this.context.appName, }; diff --git a/src/metrics.ts b/src/metrics.ts index 29e2c9f..c769163 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -1,8 +1,7 @@ // Simplified version of: https://github.com/Unleash/unleash-client-node/blob/main/src/metrics.ts import { notNullOrUndefined } from './util'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const packageJSON = require('../package.json'); +import { sdkVersion } from './version'; export interface MetricsOptions { onError: OnError; @@ -127,7 +126,7 @@ export default class Metrics { [this.headerName]: this.clientKey, Accept: 'application/json', 'Content-Type': 'application/json', - 'x-unleash-sdk': `unleash-js@${packageJSON.version}`, + 'x-unleash-sdk': sdkVersion, 'x-unleash-connection-id': this.connectionId, 'x-unleash-appname': this.appName, }; diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 0000000..896c627 --- /dev/null +++ b/src/version.ts @@ -0,0 +1 @@ +export const sdkVersion = `unleash-js@__VERSION__`; diff --git a/yarn.lock b/yarn.lock index cf404e4..7dce0f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -642,6 +642,14 @@ is-module "^1.0.0" resolve "^1.22.1" +"@rollup/plugin-replace@^6.0.2": + version "6.0.2" + resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-6.0.2.tgz#2f565d312d681e4570ff376c55c5c08eb6f1908d" + integrity sha512-7QaYCf8bqF04dOy7w/eHmJeNExxTYwvKAmlSAH/EaWWUzbT0h5sbF6bktFoX/0F/0qwng5/dWFMyf3gzaM8DsQ== + dependencies: + "@rollup/pluginutils" "^5.0.1" + magic-string "^0.30.3" + "@rollup/plugin-terser@^0.4.4": version "0.4.4" resolved "https://registry.yarnpkg.com/@rollup/plugin-terser/-/plugin-terser-0.4.4.tgz#15dffdb3f73f121aa4fbb37e7ca6be9aeea91962" @@ -2977,9 +2985,9 @@ rollup-pluginutils@^2.8.1: estree-walker "^0.6.1" rollup@^3.29.4: - version "3.29.4" - resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.4.tgz#4d70c0f9834146df8705bfb69a9a19c9e1109981" - integrity sha512-oWzmBZwvYrU0iJHtDmhsm662rC15FRXmcjCk1xD771dFDx5jJ02ufAQQTn0etB2emNk4J9EZg/yWKpsn9BWGRw== + version "3.29.5" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.5.tgz#8a2e477a758b520fb78daf04bca4c522c1da8a54" + integrity sha512-GVsDdsbJzzy4S/v3dqWPJ7EfvZJfCHiDqe80IyrF59LYuP+e6U1LJoUqeuqRbwAWoMNoXivMNeNAOf5E22VA1w== optionalDependencies: fsevents "~2.3.2"