Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: embed version at compile time #237

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
.vscode/
coverage/
.idea
.DS_Store
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -25,6 +29,10 @@ export default {
}
],
plugins: [
replace({
'__VERSION__': version,
preventAssignment: true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is required by the warning from the package. more info here: https://www.npmjs.com/package/@rollup/plugin-replace

}),
typescript({
compilerOptions: {
lib: ['es5', 'es6', 'dom'],
Expand Down
4 changes: 1 addition & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -1383,7 +1381,7 @@ 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}`,
'x-unleash-sdk': `unleash-js@__VERSION__`,
'x-unleash-connection-id': expect.stringMatching(uuidFormat),
'x-unleash-appname': appName,
};
Expand Down
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
};
Expand Down
5 changes: 2 additions & 3 deletions src/metrics.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
};
Expand Down
1 change: 1 addition & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sdkVersion = `unleash-js@__VERSION__`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

centralizing this to avoid typos in multiple places

14 changes: 11 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down
Loading