Skip to content

Commit b2e385d

Browse files
authored
fix: embed version at compile time (#237)
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.
1 parent df7b6f0 commit b2e385d

File tree

8 files changed

+28
-13
lines changed

8 files changed

+28
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules/
33
.vscode/
44
coverage/
55
.idea
6+
.DS_Store

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@babel/runtime": "^7.23.1",
4747
"@rollup/plugin-commonjs": "^25.0.5",
4848
"@rollup/plugin-node-resolve": "^15.2.3",
49+
"@rollup/plugin-replace": "^6.0.2",
4950
"@rollup/plugin-terser": "^0.4.4",
5051
"@rollup/plugin-typescript": "^11.1.5",
5152
"@types/jest": "^29.5.5",

rollup.config.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import nodeResolve from '@rollup/plugin-node-resolve';
33
import commonjs from '@rollup/plugin-commonjs';
44
import terser from '@rollup/plugin-terser';
55
import nodePolyfills from 'rollup-plugin-node-polyfills';
6+
import replace from '@rollup/plugin-replace';
7+
import fs from 'fs';
8+
9+
const version = JSON.parse(fs.readFileSync('./package.json', 'UTF-8')).version;
610

711
export default {
812
input: './src/index.ts',
@@ -25,6 +29,10 @@ export default {
2529
}
2630
],
2731
plugins: [
32+
replace({
33+
'__VERSION__': version,
34+
preventAssignment: true
35+
}),
2836
typescript({
2937
compilerOptions: {
3038
lib: ['es5', 'es6', 'dom'],

src/index.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { FetchMock } from 'jest-fetch-mock';
2-
// eslint-disable-next-line @typescript-eslint/no-var-requires
3-
const packageJSON = require('../package.json');
42
import 'jest-localstorage-mock';
53
import * as data from './test/testdata.json';
64
import IStorageProvider from './storage-provider';
@@ -1383,7 +1381,8 @@ test('Should add `x-unleash` headers', async () => {
13831381
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
13841382

13851383
const expectedHeaders = {
1386-
'x-unleash-sdk': `unleash-js@${packageJSON.version}`,
1384+
// will be replaced at build time with the actual version
1385+
'x-unleash-sdk': 'unleash-js@__VERSION__',
13871386
'x-unleash-connection-id': expect.stringMatching(uuidFormat),
13881387
'x-unleash-appname': appName,
13891388
};

src/index.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import {
1010
notNullOrUndefined,
1111
urlWithContextAsQuery,
1212
} from './util';
13-
14-
// eslint-disable-next-line @typescript-eslint/no-var-requires
15-
const packageJSON = require('../package.json');
13+
import { sdkVersion } from './version';
1614

1715
const DEFINED_FIELDS = [
1816
'userId',
@@ -476,7 +474,7 @@ export class UnleashClient extends TinyEmitter {
476474
const headers = {
477475
[this.headerName]: this.clientKey,
478476
Accept: 'application/json',
479-
'x-unleash-sdk': `unleash-js@${packageJSON.version}`,
477+
'x-unleash-sdk': sdkVersion,
480478
'x-unleash-connection-id': this.connectionId,
481479
'x-unleash-appname': this.context.appName,
482480
};

src/metrics.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Simplified version of: https://github.com/Unleash/unleash-client-node/blob/main/src/metrics.ts
22

33
import { notNullOrUndefined } from './util';
4-
// eslint-disable-next-line @typescript-eslint/no-var-requires
5-
const packageJSON = require('../package.json');
4+
import { sdkVersion } from './version';
65

76
export interface MetricsOptions {
87
onError: OnError;
@@ -127,7 +126,7 @@ export default class Metrics {
127126
[this.headerName]: this.clientKey,
128127
Accept: 'application/json',
129128
'Content-Type': 'application/json',
130-
'x-unleash-sdk': `unleash-js@${packageJSON.version}`,
129+
'x-unleash-sdk': sdkVersion,
131130
'x-unleash-connection-id': this.connectionId,
132131
'x-unleash-appname': this.appName,
133132
};

src/version.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sdkVersion = `unleash-js@__VERSION__`;

yarn.lock

+11-3
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@
642642
is-module "^1.0.0"
643643
resolve "^1.22.1"
644644

645+
"@rollup/plugin-replace@^6.0.2":
646+
version "6.0.2"
647+
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-6.0.2.tgz#2f565d312d681e4570ff376c55c5c08eb6f1908d"
648+
integrity sha512-7QaYCf8bqF04dOy7w/eHmJeNExxTYwvKAmlSAH/EaWWUzbT0h5sbF6bktFoX/0F/0qwng5/dWFMyf3gzaM8DsQ==
649+
dependencies:
650+
"@rollup/pluginutils" "^5.0.1"
651+
magic-string "^0.30.3"
652+
645653
"@rollup/plugin-terser@^0.4.4":
646654
version "0.4.4"
647655
resolved "https://registry.yarnpkg.com/@rollup/plugin-terser/-/plugin-terser-0.4.4.tgz#15dffdb3f73f121aa4fbb37e7ca6be9aeea91962"
@@ -2977,9 +2985,9 @@ rollup-pluginutils@^2.8.1:
29772985
estree-walker "^0.6.1"
29782986

29792987
rollup@^3.29.4:
2980-
version "3.29.4"
2981-
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.4.tgz#4d70c0f9834146df8705bfb69a9a19c9e1109981"
2982-
integrity sha512-oWzmBZwvYrU0iJHtDmhsm662rC15FRXmcjCk1xD771dFDx5jJ02ufAQQTn0etB2emNk4J9EZg/yWKpsn9BWGRw==
2988+
version "3.29.5"
2989+
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.29.5.tgz#8a2e477a758b520fb78daf04bca4c522c1da8a54"
2990+
integrity sha512-GVsDdsbJzzy4S/v3dqWPJ7EfvZJfCHiDqe80IyrF59LYuP+e6U1LJoUqeuqRbwAWoMNoXivMNeNAOf5E22VA1w==
29832991
optionalDependencies:
29842992
fsevents "~2.3.2"
29852993

0 commit comments

Comments
 (0)