Skip to content

Commit 1a303c6

Browse files
committed
fix: respects useGlobalNamespace
1 parent 116b33e commit 1a303c6

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

app-config-utils/src/__snapshots__/index.test.ts.snap

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22

33
exports[`generateModuleText creates config module 1`] = `
44
"
5-
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
6-
75
const config = {\\"foo\\":\\"bar\\"};
86
97
export { config };
108
export default config;
119
12-
export function currentEnvironment() {
13-
return globalNamespace._appConfigEnvironment || \\"test\\";
14-
}
15-
"
10+
export function currentEnvironment() {
11+
return \\"test\\";
12+
}
13+
"
1614
`;
1715

1816
exports[`generateModuleText creates config module with esm validation function 1`] = `
1917
"
20-
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
21-
18+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
19+
2220
const configValue = {\\"foo\\":\\"bar\\"};
2321
2422
// if the global was already defined, use it
@@ -47,16 +45,16 @@ exports[`generateModuleText creates config module with esm validation function 1
4745
4846
export const validateConfig = /*#__PURE__*/ genValidateConfig();
4947
50-
export function currentEnvironment() {
51-
return globalNamespace._appConfigEnvironment || \\"test\\";
52-
}
53-
"
48+
export function currentEnvironment() {
49+
return globalNamespace._appConfigEnvironment || \\"test\\";
50+
}
51+
"
5452
`;
5553

5654
exports[`generateModuleText creates config module with global namespace 1`] = `
5755
"
58-
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
59-
56+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
57+
6058
const configValue = {\\"foo\\":\\"bar\\"};
6159
6260
// if the global was already defined, use it
@@ -73,16 +71,16 @@ exports[`generateModuleText creates config module with global namespace 1`] = `
7371
export { config };
7472
export default config;
7573
76-
export function currentEnvironment() {
77-
return globalNamespace._appConfigEnvironment || \\"test\\";
78-
}
79-
"
74+
export function currentEnvironment() {
75+
return globalNamespace._appConfigEnvironment || \\"test\\";
76+
}
77+
"
8078
`;
8179

8280
exports[`generateModuleText creates config module with validation function 1`] = `
8381
"
84-
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
85-
82+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
83+
8684
const configValue = {\\"foo\\":\\"bar\\"};
8785
8886
// if the global was already defined, use it
@@ -111,8 +109,8 @@ exports[`generateModuleText creates config module with validation function 1`] =
111109
112110
export const validateConfig = /*#__PURE__*/ genValidateConfig();
113111
114-
export function currentEnvironment() {
115-
return globalNamespace._appConfigEnvironment || \\"test\\";
116-
}
117-
"
112+
export function currentEnvironment() {
113+
return globalNamespace._appConfigEnvironment || \\"test\\";
114+
}
115+
"
118116
`;

app-config-utils/src/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ export function generateModuleText(
4444
const privateEnvName = '_appConfigEnvironment';
4545
const config = JSON.stringify(fullConfig);
4646

47-
let generatedText = `
48-
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
49-
`;
47+
let generatedText = '';
5048

5149
if (useGlobalNamespace) {
5250
generatedText += `
51+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
52+
5353
const configValue = ${config};
5454
5555
// if the global was already defined, use it
@@ -111,11 +111,19 @@ export function generateModuleText(
111111

112112
const environmentString = environment ? JSON.stringify(environment) : 'undefined';
113113

114-
generatedText += `
115-
export function currentEnvironment() {
116-
return globalNamespace.${privateEnvName} || ${environmentString};
117-
}
118-
`;
114+
if (useGlobalNamespace) {
115+
generatedText += `
116+
export function currentEnvironment() {
117+
return globalNamespace.${privateEnvName} || ${environmentString};
118+
}
119+
`;
120+
} else {
121+
generatedText += `
122+
export function currentEnvironment() {
123+
return ${environmentString};
124+
}
125+
`;
126+
}
119127

120128
return generatedText;
121129
}

0 commit comments

Comments
 (0)