Skip to content

Commit f096f04

Browse files
committed
feat(#102): adds 'regex' option on webpack plugin to change what file requests to intercept
1 parent 74444fa commit f096f04

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ describe('frontend-webpack-project example', () => {
110110
});
111111
});
112112

113+
it('uses custom app config regex', async () => {
114+
process.env.APP_CONFIG = JSON.stringify({ externalApiUrl: 'https://localhost:3999' });
115+
116+
await new Promise<void>((done, reject) => {
117+
webpack([createOptions({ intercept: /@app-config\/main/ })], (err, stats) => {
118+
if (err) reject(err);
119+
if (stats.hasErrors()) reject(stats.toString());
120+
121+
const { children } = stats.toJson();
122+
const [{ modules = [] }] = children || [];
123+
124+
expect(
125+
modules.some(({ source }) =>
126+
source?.includes('const configValue = {"externalApiUrl":"https://localhost:3999"};'),
127+
),
128+
).toBe(true);
129+
130+
done();
131+
});
132+
});
133+
});
134+
113135
it('throws validation errors', async () => {
114136
process.env.APP_CONFIG = JSON.stringify({ externalApiUrl: 'not a uri' });
115137

app-config-webpack/src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,28 @@ export interface Options {
1212
noGlobal?: boolean;
1313
loading?: ConfigLoadingOptions;
1414
schemaLoading?: SchemaLoadingOptions;
15+
intercept?: RegExp;
1516
}
1617

1718
export default class AppConfigPlugin {
1819
headerInjection: boolean;
1920
noGlobal: boolean;
2021
loadingOptions?: ConfigLoadingOptions;
2122
schemaLoadingOptions?: SchemaLoadingOptions;
22-
23-
constructor({ headerInjection = false, noGlobal = false, loading, schemaLoading }: Options = {}) {
23+
intercept: RegExp;
24+
25+
constructor({
26+
headerInjection = false,
27+
noGlobal = false,
28+
loading,
29+
schemaLoading,
30+
intercept,
31+
}: Options = {}) {
2432
this.headerInjection = headerInjection;
2533
this.noGlobal = noGlobal;
2634
this.loadingOptions = loading;
2735
this.schemaLoadingOptions = schemaLoading;
36+
this.intercept = intercept ?? AppConfigPlugin.regex;
2837
}
2938

3039
static loader = loader;
@@ -45,7 +54,7 @@ export default class AppConfigPlugin {
4554
async (resolve?: { request: string }) => {
4655
if (!resolve) return;
4756

48-
if (regex.test(resolve.request)) {
57+
if (this.intercept.test(resolve.request)) {
4958
const { filePaths } = await loadValidatedConfig(
5059
this.loadingOptions,
5160
this.schemaLoadingOptions,

0 commit comments

Comments
 (0)