Skip to content

Inject package version during build instead of importing it from pack… #434

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

Merged
4 commits merged into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions __TESTS__/unit/analytics/analytics.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import pkg from '../../../src/package.json';
import {createNewImageWithAnalytics} from "./testUtils/createNewImageWithAnalytics";

// Since packageVersion value is only set during build, we need to mock it during tests
jest.mock("../../../src/internal/utils/packageVersion", () => ({packageVersion: pkg.version}));

describe('Add analytics to a regular URL', () => {
it('Works with default values', () => {
const cldImage = createNewImageWithAnalytics('sample');
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"build:docs": "node ./scripts/buildDocs.js",
"build:entryPoints": "ts-node ./scripts/createEntrypoints.ts",
"build:copyCleanPackageJSON": "ts-node scripts/copyPackageJsonToSrc.ts",
"build:injectPackageVersion": "ts-node ./scripts/injectPackageVersionToDistFiles.ts",
"lint": "eslint src __TESTS__ --color --ext .ts",
"start": "rollup -c rollup.dev.config.js -w",
"bundlewatch": "bundlewatch --config ./bundlewatch.config.js",
Expand All @@ -36,6 +37,7 @@
"@release-it/bumper": "^2.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^7.1.1",
"@rollup/plugin-replace": "^3.0.0",
"@types/jest": "^25.2.3",
"@types/mock-fs": "^4.10.0",
"@types/node": "^15.14.2",
Expand Down Expand Up @@ -71,6 +73,7 @@
"prettier": "^2.3.1",
"prismjs": "^1.14.0",
"release-it": "^14.2.0",
"replace-in-file": "^6.2.0",
"rimraf": "^3.0.2",
"rollup": "^2.3.2",
"rollup-plugin-babel": "^4.4.0",
Expand Down
5 changes: 5 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import resolve from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript';
import json from '@rollup/plugin-json';
import commonjs from 'rollup-plugin-commonjs';
import replace from '@rollup/plugin-replace';
import {version} from './package.json';

export default [{
input: 'src/index.ts',
Expand All @@ -15,6 +17,9 @@ export default [{
plugins: [
json(),
resolve(),
replace({
'PACKAGE_VERSION_INJECTED_DURING_BUILD': version
}),
typescript({ target: "es5"}),
commonjs()
]
Expand Down
7 changes: 6 additions & 1 deletion rollup.dev.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import resolve from '@rollup/plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import serve from 'rollup-plugin-serve'
import serve from 'rollup-plugin-serve';
import typescript from "rollup-plugin-typescript";
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
import {version} from './package.json';

export default {
input: 'src/index.ts',
Expand All @@ -19,6 +21,9 @@ export default {
contentBase: 'playground'
}),
resolve(),
replace({
'PACKAGE_VERSION_INJECTED_DURING_BUILD': version
}),
babel({
exclude: 'node_modules/**' // only transpile our source code
})
Expand Down
1 change: 1 addition & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ npm run build:ESM
npm run build:UMD
npm run build:entryPoints
npm run build:docs
npm run build:injectPackageVersion

#cp CHANGELOG.md to dist
cp ./CHANGELOG.md ./dist
Expand Down
28 changes: 28 additions & 0 deletions scripts/injectPackageVersionToDistFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint @typescript-eslint/no-var-requires: 0 */

/**
* This script goes over dist files and replaces the package version placeholder to be the actual package version.
*/
const replace = require('replace-in-file');
const {version} = require('../package.json');
const {resolve} = require('path');

const distFolder = resolve('./dist');
const options = {
files: [`${distFolder}/**/*.js`, `${distFolder}/**/*.ts`],
from: /PACKAGE_VERSION_INJECTED_DURING_BUILD/g,
to: version
};

// Internationally not catching errors so that build will fail on error
const fileNamesReplaced = replace
.sync(options)
.filter((fileData: typeof replace.ReplaceResult) => fileData.hasChanged)
.map((fileData: typeof replace.ReplaceResult) => fileData.file);

// files is not empty
if (fileNamesReplaced.length) {
console.log('Successfully injected package version to dist files:', fileNamesReplaced);
} else {
throw 'Failed to inject package version to dist files because no matching files where found';
}
11 changes: 11 additions & 0 deletions src/internal/utils/packageVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const packageVersion = 'PACKAGE_VERSION_INJECTED_DURING_BUILD';

/**
* Export package version (injected during our build).
* Reason for this is that If we import values from from package.json,
* it will cause an error for users who do not have an 'import from json' plugin
* as part of their build process / bundler.
*/
export {
packageVersion
};
4 changes: 2 additions & 2 deletions src/sdkAnalytics/getSDKAnalyticsSignature.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {encodeVersion} from "./encodeVersion";
import {getAnalyticsOptions} from "./getAnalyticsOptions";
import {ITrackedPropertiesThroughAnalytics} from "./interfaces/ITrackedPropertiesThroughAnalytics";
import pkg from '../package.json';
import {packageVersion} from "../internal/utils/packageVersion";

/**
* @private
Expand Down Expand Up @@ -29,7 +29,7 @@ function ensureShapeOfTrackedProperties(trackedAnalytics?: Partial<ITrackedPrope
const defaults = {
techVersion: getNodeVersion(),
sdkCode: 'T', // Base code
sdkSemver : pkg.version.split('-')[0], // remove -beta, -alpha or other tagged versions from the version string
sdkSemver : packageVersion.split('-')[0], // remove -beta, -alpha or other tagged versions from the version string
responsive: false,
placeholder: false,
lazyload: false,
Expand Down