Skip to content

Commit

Permalink
fix: embed version at compile time (#237)
Browse files Browse the repository at this point in the history
Removes requiring version at runtime with require: 3615f15#diff-258035e5968f6bf645400d417f310218d7d9a9a10606a3c34e7f55db193f58f3R3

Instead we're using the rollup replace plugin to read the version at build time and replace __VERSION__ placeholder with the actual version number.

As a result other packages depending on this one don't need to understand require calls and also we're not exposing the whole package.json file at runtime.

To verify my claim above you can build the package and check the build directory after this change vs before this change.
  • Loading branch information
kwasniew authored Jan 16, 2025
1 parent df7b6f0 commit b2e385d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 13 deletions.
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
}),
typescript({
compilerOptions: {
lib: ['es5', 'es6', 'dom'],
Expand Down
5 changes: 2 additions & 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,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,
};
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__`;
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

0 comments on commit b2e385d

Please sign in to comment.