Skip to content

Commit e891bd6

Browse files
committed
1 parent 0d10ae3 commit e891bd6

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

packages/atomic/scripts/build.mjs

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import chalk from 'chalk';
2+
import {readFileSync} from 'fs';
23
import {dirname, basename, relative} from 'path';
34
import {argv} from 'process';
45
import {
@@ -44,10 +45,32 @@ function loadTsConfig(configPath) {
4445
);
4546
}
4647

48+
function injectGlobalVariables() {
49+
if (typeof window === 'undefined') {
50+
return '';
51+
}
52+
const packageJson = JSON.parse(readFileSync('./package.json', 'utf8'));
53+
const version = packageJson.version || 'unknown';
54+
return `window.__ATOMIC_VERSION__ = '${version}';`;
55+
}
56+
4757
function emit(program) {
4858
const targetSourceFile = undefined;
49-
const cancellationToken = undefined;
50-
const writeFile = undefined;
59+
const cancellationToken = {
60+
throwIfCancellationRequested: () => {},
61+
};
62+
const writeFile = (
63+
fileName,
64+
data,
65+
writeByteOrderMark,
66+
onError,
67+
sourceFiles
68+
) => {
69+
if (fileName.endsWith('.js')) {
70+
data = injectGlobalVariables() + '\n' + data;
71+
}
72+
sys.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
73+
};
5174
const emitOnlyDtsFiles = false;
5275
const customTransformers = {
5376
before: transformers,
@@ -56,8 +79,8 @@ function emit(program) {
5679

5780
return program.emit(
5881
targetSourceFile,
59-
cancellationToken,
6082
writeFile,
83+
cancellationToken,
6184
emitOnlyDtsFiles,
6285
customTransformers
6386
);

packages/atomic/src/components/common/interface/interface-common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//import {setCoveoGlobal} from '@/src/global/environment';
2+
import {setCoveoGlobal} from '@/src/global/environment.js';
23
import {LogLevel} from '@coveo/headless';
34
import {i18n, TFunction} from 'i18next';
45
import Backend from 'i18next-http-backend';
56
import {html} from 'lit';
6-
import {setCoveoGlobal} from '../../../global/environment.js';
77
import {loadFocusVisiblePolyfill} from '../../../global/focus-visible.js';
88
import {loadDayjsLocale} from '../../../utils/dayjs-locales.js';
99
import {AnyBindings, AnyEngineType} from './bindings.js';

packages/atomic/src/global/environment.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,32 @@ declare global {
1212
}
1313

1414
function getWindow() {
15+
if (typeof window === 'undefined') {
16+
return undefined;
17+
}
1518
return window;
1619
}
1720

21+
function getAtomicVersion() {
22+
const win = getWindow();
23+
if (!win) {
24+
return 'unknown';
25+
}
26+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
27+
return (win as any).__ATOMIC_VERSION__ || 'unknown';
28+
}
29+
1830
export function getAtomicEnvironment(): AtomicEnvironment {
1931
return {
20-
version: process.env.VERSION!,
32+
version: getAtomicVersion(),
2133
headlessVersion: VERSION,
2234
};
2335
}
2436

2537
export function setCoveoGlobal(globalVariableName: string) {
26-
if (getWindow()[globalVariableName]) {
38+
const win = getWindow();
39+
if (!win || win[globalVariableName]) {
2740
return;
2841
}
29-
getWindow()[globalVariableName] = getAtomicEnvironment();
42+
win[globalVariableName] = getAtomicEnvironment();
3043
}

0 commit comments

Comments
 (0)