Skip to content

Commit 116b33e

Browse files
committed
feat: injects _appConfigEnvironment into bundler packages
1 parent c17d97e commit 116b33e

File tree

6 files changed

+58
-34
lines changed

6 files changed

+58
-34
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`loads config correctly 1`] = `
4-
"(()=>{var r={foo:\\"bar\\"},e=(typeof window==\\"undefined\\"?globalThis:window)||{},t=e._appConfig||r;(typeof e._appConfig==\\"undefined\\"||!Object.isFrozen(e._appConfig))&&(e._appConfig=t);var a=t;console.log(a);})();
4+
"(()=>{var e=(typeof window==\\"undefined\\"?globalThis:window)||{},r={foo:\\"bar\\"},t=e._appConfig||r;(typeof e._appConfig==\\"undefined\\"||!Object.isFrozen(e._appConfig))&&(e._appConfig=t);var a=t;console.log(a);})();
55
"
66
`;
77

88
exports[`loads currentEnvironment 1`] = `
9-
"(()=>{var t={foo:\\"bar\\"},e=(typeof window==\\"undefined\\"?globalThis:window)||{},r=e._appConfig||t;(typeof e._appConfig==\\"undefined\\"||!Object.isFrozen(e._appConfig))&&(e._appConfig=r);function o(){return\\"test\\"}console.log(o());})();
9+
"(()=>{var e=(typeof window==\\"undefined\\"?globalThis:window)||{},t={foo:\\"bar\\"},r=e._appConfig||t;(typeof e._appConfig==\\"undefined\\"||!Object.isFrozen(e._appConfig))&&(e._appConfig=r);function o(){return e._appConfigEnvironment||\\"test\\"}console.log(o());})();
1010
"
1111
`;
1212

1313
exports[`loads validation function 1`] = `
14-
"(()=>{var v={foo:\\"bar\\"},n=(typeof window==\\"undefined\\"?globalThis:window)||{},m=n._appConfig||v;(typeof n._appConfig==\\"undefined\\"||!Object.isFrozen(n._appConfig))&&(n._appConfig=m);function P(){let s={};return function(p){\\"use strict\\";p.exports=i,p.exports.default=i;var b={type:\\"object\\",additionalProperties:!1,properties:{foo:{type:\\"string\\"}},$schema:\\"http://json-schema.org/draft-07/schema#\\"};function i(o,t){\\"use strict\\";if(t)var a=t.dataPath,g=t.parentData,h=t.parentDataProperty,y=t.rootData;else var a=\\"\\",g=void 0,h=void 0,y=o;var e=null,r=0;if(o&&typeof o==\\"object\\"&&!Array.isArray(o)){for(var f in o)if(f!==\\"foo\\"){var d={keyword:\\"additionalProperties\\",dataPath:a,schemaPath:\\"#/additionalProperties\\",params:{additionalProperty:f},message:\\"should NOT have additional properties\\"};e===null?e=[d]:e.push(d),r++}if(o.foo!==void 0&&typeof o.foo!=\\"string\\"){var l={keyword:\\"type\\",dataPath:a+\\"/foo\\",schemaPath:\\"#/properties/foo/type\\",params:{type:\\"string\\"},message:\\"should be string\\"};e===null?e=[l]:e.push(l),r++}}else{var c={keyword:\\"type\\",dataPath:a,schemaPath:\\"#/type\\",params:{type:\\"object\\"},message:\\"should be object\\"};e===null?e=[c]:e.push(c),r++}return i.errors=e,r===0}}(s),s.exports}var u=P();u({foo:12});})();
14+
"(()=>{var n=(typeof window==\\"undefined\\"?globalThis:window)||{},v={foo:\\"bar\\"},m=n._appConfig||v;(typeof n._appConfig==\\"undefined\\"||!Object.isFrozen(n._appConfig))&&(n._appConfig=m);function P(){let p={};return function(s){\\"use strict\\";s.exports=i,s.exports.default=i;var b={type:\\"object\\",additionalProperties:!1,properties:{foo:{type:\\"string\\"}},$schema:\\"http://json-schema.org/draft-07/schema#\\"};function i(o,t){\\"use strict\\";if(t)var a=t.dataPath,g=t.parentData,h=t.parentDataProperty,y=t.rootData;else var a=\\"\\",g=void 0,h=void 0,y=o;var e=null,r=0;if(o&&typeof o==\\"object\\"&&!Array.isArray(o)){for(var f in o)if(f!==\\"foo\\"){var d={keyword:\\"additionalProperties\\",dataPath:a,schemaPath:\\"#/additionalProperties\\",params:{additionalProperty:f},message:\\"should NOT have additional properties\\"};e===null?e=[d]:e.push(d),r++}if(o.foo!==void 0&&typeof o.foo!=\\"string\\"){var l={keyword:\\"type\\",dataPath:a+\\"/foo\\",schemaPath:\\"#/properties/foo/type\\",params:{type:\\"string\\"},message:\\"should be string\\"};e===null?e=[l]:e.push(l),r++}}else{var c={keyword:\\"type\\",dataPath:a,schemaPath:\\"#/type\\",params:{type:\\"object\\"},message:\\"should be object\\"};e===null?e=[c]:e.push(c),r++}return i.errors=e,r===0}}(p),p.exports}var u=P();u({foo:12});})();
1515
"
1616
`;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`injectHtml adds app-config to an empty script tag 1`] = `
88
<meta charset=\\"utf-8\\">
99
<title>Webpack App</title>
1010
<meta name=\\"viewport\\" content=\\"width=device-width,initial-scale=1\\">
11-
<script id=\\"app-config\\">window._appConfig={\\"foo\\":42}</script>
11+
<script id=\\"app-config\\">window._appConfig={\\"foo\\":42}, window._appConfigEnvironment=\\"test\\"</script>
1212
</head>
1313
<body><script src=\\"/main.js\\"></script></body>
1414
</html>
@@ -23,7 +23,7 @@ exports[`injectHtml adds app-config to an existing script tag 1`] = `
2323
<meta charset=\\"utf-8\\">
2424
<title>Webpack App</title>
2525
<meta name=\\"viewport\\" content=\\"width=device-width,initial-scale=1\\">
26-
<script id=\\"app-config\\">window._appConfig={\\"foo\\":88}</script>
26+
<script id=\\"app-config\\">window._appConfig={\\"foo\\":88}, window._appConfigEnvironment=\\"test\\"</script>
2727
</head>
2828
<body><script src=\\"/main.js\\"></script></body>
2929
</html>
@@ -38,7 +38,7 @@ exports[`injectHtml uses provided loadConfig options 1`] = `
3838
<meta charset=\\"utf-8\\">
3939
<title>Webpack App</title>
4040
<meta name=\\"viewport\\" content=\\"width=device-width,initial-scale=1\\">
41-
<script id=\\"app-config\\">window._appConfig={\\"foo\\":true}</script>
41+
<script id=\\"app-config\\">window._appConfig={\\"foo\\":true}, window._appConfigEnvironment=\\"test\\"</script>
4242
</head>
4343
<body><script src=\\"/main.js\\"></script></body>
4444
</html>
@@ -53,7 +53,7 @@ exports[`injectHtml validates provided app-config 1`] = `
5353
<meta charset=\\"utf-8\\">
5454
<title>Webpack App</title>
5555
<meta name=\\"viewport\\" content=\\"width=device-width,initial-scale=1\\">
56-
<script id=\\"app-config\\">window._appConfig={}</script>
56+
<script id=\\"app-config\\">window._appConfig={}, window._appConfigEnvironment=\\"test\\"</script>
5757
</head>
5858
<body><script src=\\"/main.js\\"></script></body>
5959
</html>

app-config-inject/src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ConfigLoadingOptions,
66
} from '@app-config/config';
77
import { SchemaLoadingOptions } from '@app-config/schema';
8+
import type { Json } from '@app-config/utils';
89

910
export { cli } from './cli';
1011

@@ -26,15 +27,21 @@ export async function injectHtml(
2627
throw new Error('No <script id="app-config"> was found in the given HTML!');
2728
}
2829

29-
let config;
30+
let config: Json;
31+
let environment: string | undefined;
3032

3133
if (validate) {
32-
({ fullConfig: config } = await loadValidatedConfig(configOptions, schemaOptions));
34+
({ fullConfig: config, environment } = await loadValidatedConfig(configOptions, schemaOptions));
3335
} else {
34-
({ fullConfig: config } = await loadUnvalidatedConfig(configOptions));
36+
({ fullConfig: config, environment } = await loadUnvalidatedConfig(configOptions));
3537
}
3638

37-
scriptTag.set_content(`window._appConfig=${JSON.stringify(config)}`);
39+
const configString = JSON.stringify(config);
40+
const environmentString = environment ? JSON.stringify(environment) : 'undefined';
41+
42+
scriptTag.set_content(
43+
`window._appConfig=${configString}, window._appConfigEnvironment=${environmentString}`,
44+
);
3845

3946
return parsed.toString();
4047
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
exports[`generateModuleText creates config module 1`] = `
44
"
5+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
6+
57
const config = {\\"foo\\":\\"bar\\"};
68
79
export { config };
810
export default config;
911
1012
export function currentEnvironment() {
11-
return \\"test\\";
13+
return globalNamespace._appConfigEnvironment || \\"test\\";
1214
}
1315
"
1416
`;
1517

1618
exports[`generateModuleText creates config module with esm validation function 1`] = `
1719
"
20+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
21+
1822
const configValue = {\\"foo\\":\\"bar\\"};
1923
20-
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
21-
2224
// if the global was already defined, use it
2325
const config = (globalNamespace._appConfig || configValue);
2426
@@ -46,17 +48,17 @@ exports[`generateModuleText creates config module with esm validation function 1
4648
export const validateConfig = /*#__PURE__*/ genValidateConfig();
4749
4850
export function currentEnvironment() {
49-
return \\"test\\";
51+
return globalNamespace._appConfigEnvironment || \\"test\\";
5052
}
5153
"
5254
`;
5355

5456
exports[`generateModuleText creates config module with global namespace 1`] = `
5557
"
58+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
59+
5660
const configValue = {\\"foo\\":\\"bar\\"};
5761
58-
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
59-
6062
// if the global was already defined, use it
6163
const config = (globalNamespace._appConfig || configValue);
6264
@@ -72,17 +74,17 @@ exports[`generateModuleText creates config module with global namespace 1`] = `
7274
export default config;
7375
7476
export function currentEnvironment() {
75-
return \\"test\\";
77+
return globalNamespace._appConfigEnvironment || \\"test\\";
7678
}
7779
"
7880
`;
7981

8082
exports[`generateModuleText creates config module with validation function 1`] = `
8183
"
84+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
85+
8286
const configValue = {\\"foo\\":\\"bar\\"};
8387
84-
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
85-
8688
// if the global was already defined, use it
8789
const config = (globalNamespace._appConfig || configValue);
8890
@@ -110,7 +112,7 @@ exports[`generateModuleText creates config module with validation function 1`] =
110112
export const validateConfig = /*#__PURE__*/ genValidateConfig();
111113
112114
export function currentEnvironment() {
113-
return \\"test\\";
115+
return globalNamespace._appConfigEnvironment || \\"test\\";
114116
}
115117
"
116118
`;

app-config-utils/src/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ export function generateModuleText(
4141
},
4242
): string {
4343
const privateName = '_appConfig';
44+
const privateEnvName = '_appConfigEnvironment';
4445
const config = JSON.stringify(fullConfig);
4546

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

4851
if (useGlobalNamespace) {
49-
generatedText = `
52+
generatedText += `
5053
const configValue = ${config};
5154
52-
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
53-
5455
// if the global was already defined, use it
5556
const config = (globalNamespace.${privateName} || configValue);
5657
@@ -66,7 +67,7 @@ export function generateModuleText(
6667
export default config;
6768
`;
6869
} else {
69-
generatedText = `
70+
generatedText += `
7071
const config = ${config};
7172
7273
export { config };
@@ -78,7 +79,7 @@ export function generateModuleText(
7879
if (esmValidationCode) {
7980
const [code, imports] = validationFunctionCode(true);
8081

81-
generatedText = `${generatedText}
82+
generatedText += `
8283
${imports}
8384
8485
${/* nest the generated commonjs module here */ ''}
@@ -94,7 +95,7 @@ export function generateModuleText(
9495
} else {
9596
const code = validationFunctionCode();
9697

97-
generatedText = `${generatedText}
98+
generatedText += `
9899
${/* nest the generated commonjs module here */ ''}
99100
function genValidateConfig(){
100101
const validateConfigModule = {};
@@ -108,9 +109,11 @@ export function generateModuleText(
108109
}
109110
}
110111

111-
generatedText = `${generatedText}
112+
const environmentString = environment ? JSON.stringify(environment) : 'undefined';
113+
114+
generatedText += `
112115
export function currentEnvironment() {
113-
return ${environment ? JSON.stringify(environment) : 'undefined'};
116+
return globalNamespace.${privateEnvName} || ${environmentString};
114117
}
115118
`;
116119

app-config-webpack/src/index.test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('frontend-webpack-project example', () => {
8888
),
8989
).toBe(true);
9090

91-
expect(modules.some(({ source }) => source?.includes('_appConfig'))).toBe(false);
91+
expect(modules.some(({ source }) => source?.includes('_appConfig '))).toBe(false);
9292

9393
done();
9494
});
@@ -175,7 +175,11 @@ describe('frontend-webpack-project example', () => {
175175
expect(
176176
modules.some(({ source }) => source?.includes('export function currentEnvironment()')),
177177
).toBe(true);
178-
expect(modules.some(({ source }) => source?.includes('return "test";'))).toBe(true);
178+
expect(
179+
modules.some(({ source }) =>
180+
source?.includes('return globalNamespace._appConfigEnvironment || "test";'),
181+
),
182+
).toBe(true);
179183

180184
done();
181185
});
@@ -198,7 +202,11 @@ describe('frontend-webpack-project example', () => {
198202
expect(
199203
modules.some(({ source }) => source?.includes('export function currentEnvironment()')),
200204
).toBe(true);
201-
expect(modules.some(({ source }) => source?.includes('return "foobar";'))).toBe(true);
205+
expect(
206+
modules.some(({ source }) =>
207+
source?.includes('return globalNamespace._appConfigEnvironment || "foobar";'),
208+
),
209+
).toBe(true);
202210

203211
done();
204212
});
@@ -223,7 +231,11 @@ describe('frontend-webpack-project example', () => {
223231
expect(
224232
modules.some(({ source }) => source?.includes('export function currentEnvironment()')),
225233
).toBe(true);
226-
expect(modules.some(({ source }) => source?.includes('return "foobar";'))).toBe(true);
234+
expect(
235+
modules.some(({ source }) =>
236+
source?.includes('return globalNamespace._appConfigEnvironment || "foobar";'),
237+
),
238+
).toBe(true);
227239

228240
done();
229241
},

0 commit comments

Comments
 (0)