Skip to content

Commit

Permalink
chore: update script library (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 authored Dec 26, 2024
1 parent 8cc038c commit 374d8aa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/analytics-browser/src/lib-prefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LIBPREFIX = 'amplitude-ts';
3 changes: 2 additions & 1 deletion packages/analytics-browser/src/plugins/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BeforePlugin, BrowserConfig, Event } from '@amplitude/analytics-types';
import { UUID } from '@amplitude/analytics-core';
import { getLanguage } from '@amplitude/analytics-client-common';
import { VERSION } from '../version';
import { LIBPREFIX } from '../lib-prefix';

const BROWSER_PLATFORM = 'Web';
const IP_ADDRESS = '$remote';
Expand All @@ -14,7 +15,7 @@ export class Context implements BeforePlugin {
// @ts-ignore
config: BrowserConfig;
userAgent: string | undefined;
library = `amplitude-ts/${VERSION}`;
library = `${LIBPREFIX}/${VERSION}`;

constructor() {
/* istanbul ignore else */
Expand Down
36 changes: 36 additions & 0 deletions scripts/build/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { terser } from 'rollup-plugin-terser';
import gzip from 'rollup-plugin-gzip';
import execute from 'rollup-plugin-execute';
import { exec } from 'child_process';
import fs from 'fs';

// The paths are relative to process.cwd(), which are packages/*
const base = '../..';
Expand Down Expand Up @@ -54,6 +55,40 @@ export const umd = {
],
};

const updateLibPrefix = (isUndo) => {
const path = 'src/lib-prefix.ts'
if (!fs.existsSync(path)) {
// Supported in rollup 4, we're currently rollup 2
// this.error(`File not found: ${path}`);
return;
}

let content = fs.readFileSync(path, 'utf-8');
let updatedContent;
if (isUndo) {
updatedContent = content.replace(/amplitude-ts-sdk-script/g, 'amplitude-ts');
} else {
updatedContent = content.replace(/amplitude-ts/g, 'amplitude-ts-sdk-script');
}

fs.writeFileSync(path, updatedContent, 'utf-8');
// Supported in rollup 4, we're currently rollup 2
// this.info(`File updated: ${path}`);
}


const updateLibPrefixPlugin = () => {
return {
name: 'update-lib-prefix',
buildStart() {
updateLibPrefix(false);
},
buildEnd() {
updateLibPrefix(true);
}
};
}

export const iife = {
input: 'src/snippet-index.ts',
output: {
Expand All @@ -63,6 +98,7 @@ export const iife = {
sourcemap: true,
},
plugins: [
updateLibPrefixPlugin(),
typescript({
module: 'es6',
noEmit: false,
Expand Down

0 comments on commit 374d8aa

Please sign in to comment.