Skip to content

Commit 8ff1af8

Browse files
committed
feat(#102): adds noGlobal option in webpack config
1 parent fad84d1 commit 8ff1af8

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ describe('frontend-webpack-project example', () => {
8686
});
8787
});
8888

89+
it('doesnt use window._appConfig if using noGlobal', async () => {
90+
process.env.APP_CONFIG = JSON.stringify({ externalApiUrl: 'https://localhost:3999' });
91+
92+
await new Promise<void>((done, reject) => {
93+
webpack([createOptions({ noGlobal: true })], (err, stats) => {
94+
if (err) reject(err);
95+
if (stats.hasErrors()) reject(stats.toString());
96+
97+
const { children } = stats.toJson();
98+
const [{ modules = [] }] = children || [];
99+
100+
expect(
101+
modules.some(({ source }) =>
102+
source?.includes('const config = {"externalApiUrl":"https://localhost:3999"};'),
103+
),
104+
).toBe(true);
105+
106+
expect(modules.some(({ source }) => source?.includes('_appConfig'))).toBe(false);
107+
108+
done();
109+
});
110+
});
111+
});
112+
89113
it('throws validation errors', async () => {
90114
process.env.APP_CONFIG = JSON.stringify({ externalApiUrl: 'not a uri' });
91115

app-config-webpack/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ const loader = require.resolve('./loader');
99

1010
export interface Options {
1111
headerInjection?: boolean;
12+
noGlobal?: boolean;
1213
loading?: ConfigLoadingOptions;
1314
schemaLoading?: SchemaLoadingOptions;
1415
}
1516

1617
export default class AppConfigPlugin {
1718
headerInjection: boolean;
19+
noGlobal: boolean;
1820
loadingOptions?: ConfigLoadingOptions;
1921
schemaLoadingOptions?: SchemaLoadingOptions;
2022

21-
constructor({ headerInjection = false, loading, schemaLoading }: Options = {}) {
23+
constructor({ headerInjection = false, noGlobal = false, loading, schemaLoading }: Options = {}) {
2224
this.headerInjection = headerInjection;
25+
this.noGlobal = noGlobal;
2326
this.loadingOptions = loading;
2427
this.schemaLoadingOptions = schemaLoading;
2528
}

app-config-webpack/src/loader.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const loader: wp.loader.Loader = function AppConfigLoader() {
99
if (this.cacheable) this.cacheable();
1010

1111
const callback = this.async()!;
12-
const { headerInjection = false, loading, schemaLoading }: Options = getOptions(this) || {};
12+
const { headerInjection = false, noGlobal = false, loading, schemaLoading }: Options =
13+
getOptions(this) || {};
1314

1415
loadValidatedConfig(loading, schemaLoading)
1516
.then(({ fullConfig, filePaths, validationFunctionCode }) => {
@@ -18,18 +19,29 @@ const loader: wp.loader.Loader = function AppConfigLoader() {
1819
}
1920

2021
const generateText = (config: string) => {
21-
let generatedText = `
22-
const configValue = ${config};
22+
let generatedText: string;
2323

24-
const globalNamespace = window || globalThis || {};
24+
if (noGlobal) {
25+
generatedText = `
26+
const config = ${config};
2527
26-
// if the global was already defined, use it (and define it if not)
27-
const config = globalNamespace.${privateName} =
28-
(globalNamespace.${privateName} || configValue);
28+
export { config };
29+
export default config;
30+
`;
31+
} else {
32+
generatedText = `
33+
const configValue = ${config};
34+
35+
const globalNamespace = window || globalThis || {};
36+
37+
// if the global was already defined, use it (and define it if not)
38+
const config = globalNamespace.${privateName} =
39+
(globalNamespace.${privateName} || configValue);
2940
30-
export { config };
31-
export default config;
32-
`;
41+
export { config };
42+
export default config;
43+
`;
44+
}
3345

3446
if (validationFunctionCode) {
3547
generatedText = `${generatedText}

0 commit comments

Comments
 (0)