Skip to content

Commit fa55188

Browse files
committed
feat(#174): adds a doNotLoadConfig option in bundler packages
1 parent 71876f2 commit fa55188

File tree

11 files changed

+90
-7
lines changed

11 files changed

+90
-7
lines changed

app-config-esbuild/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"dependencies": {
3333
"@app-config/config": "^2.8.1",
34+
"@app-config/schema": "^2.8.1",
3435
"@app-config/utils": "^2.8.1"
3536
},
3637
"devDependencies": {

app-config-esbuild/src/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ import type { Plugin } from 'esbuild';
22
import path from 'path';
33
import { ConfigLoadingOptions, loadValidatedConfig } from '@app-config/config';
44
import { generateModuleText, packageNameRegex } from '@app-config/utils';
5-
import type { SchemaLoadingOptions } from '@app-config/schema';
5+
import { loadSchema, SchemaLoadingOptions } from '@app-config/schema';
66

77
export interface Options {
88
useGlobalNamespace?: boolean;
99
loadingOptions?: ConfigLoadingOptions;
1010
schemaLoadingOptions?: SchemaLoadingOptions;
1111
injectValidationFunction?: boolean;
12+
doNotLoadConfig?: boolean;
1213
}
1314

1415
export const createPlugin = ({
1516
useGlobalNamespace = true,
1617
loadingOptions,
1718
schemaLoadingOptions,
1819
injectValidationFunction = true,
20+
doNotLoadConfig = false,
1921
}: Options = {}): Plugin => ({
2022
name: '@app-config/esbuild',
2123
setup(build) {
@@ -25,6 +27,24 @@ export const createPlugin = ({
2527
}));
2628

2729
build.onLoad({ filter: /.*/, namespace: '@app-config/esbuild' }, async () => {
30+
if (doNotLoadConfig) {
31+
const { validationFunctionCode } = await loadSchema(schemaLoadingOptions);
32+
33+
const code = generateModuleText('no-config', {
34+
environment: undefined,
35+
useGlobalNamespace: true,
36+
validationFunctionCode: injectValidationFunction ? validationFunctionCode : undefined,
37+
esmValidationCode: true,
38+
});
39+
40+
return {
41+
loader: 'js',
42+
contents: code,
43+
resolveDir: path.parse(process.cwd()).root,
44+
watchFiles: [],
45+
};
46+
}
47+
2848
const { fullConfig, environment, validationFunctionCode, filePaths } =
2949
await loadValidatedConfig(loadingOptions, schemaLoadingOptions);
3050

app-config-esbuild/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"exclude": ["node_modules"],
99
"references": [
1010
{ "path": "../app-config-config" },
11+
{ "path": "../app-config-schema" },
1112
{ "path": "../app-config-node" },
1213
{ "path": "../app-config-utils" },
1314
{ "path": "../app-config-test-utils" }

app-config-rollup/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
"dependencies": {
3737
"@app-config/config": "^2.8.1",
38+
"@app-config/schema": "^2.8.1",
3839
"@app-config/utils": "^2.8.1"
3940
},
4041
"peerDependencies": {},

app-config-rollup/src/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import type { Plugin } from 'rollup';
22
import { generateModuleText, packageNameRegex } from '@app-config/utils';
33
import { ConfigLoadingOptions, loadValidatedConfig } from '@app-config/config';
4-
import type { SchemaLoadingOptions } from '@app-config/schema';
4+
import { loadSchema, SchemaLoadingOptions } from '@app-config/schema';
55

66
export interface Options {
77
useGlobalNamespace?: boolean;
88
loadingOptions?: ConfigLoadingOptions;
99
schemaLoadingOptions?: SchemaLoadingOptions;
1010
injectValidationFunction?: boolean;
11+
doNotLoadConfig?: boolean;
1112

1213
/** @deprecated use useGlobalNamespace instead */
1314
readGlobal?: boolean;
@@ -21,6 +22,7 @@ export default function appConfigRollup({
2122
loadingOptions,
2223
schemaLoadingOptions,
2324
injectValidationFunction,
25+
doNotLoadConfig = false,
2426

2527
readGlobal,
2628
}: Options = {}): Plugin & { currentFilePaths?: string[] } {
@@ -38,6 +40,17 @@ export default function appConfigRollup({
3840
},
3941
async load(id) {
4042
if (packageNameRegex.exec(id) || appConfigImportRegex.exec(id)) {
43+
if (doNotLoadConfig) {
44+
const { validationFunctionCode } = await loadSchema(schemaLoadingOptions);
45+
46+
return generateModuleText('no-config', {
47+
environment: undefined,
48+
useGlobalNamespace: true,
49+
validationFunctionCode: injectValidationFunction ? validationFunctionCode : undefined,
50+
esmValidationCode: false,
51+
});
52+
}
53+
4154
const { fullConfig, environment, validationFunctionCode, filePaths } =
4255
await loadValidatedConfig(loadingOptions, schemaLoadingOptions);
4356

app-config-rollup/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"exclude": ["node_modules"],
99
"references": [
1010
{ "path": "../app-config-config" },
11+
{ "path": "../app-config-schema" },
1112
{ "path": "../app-config-utils" },
1213
{ "path": "../app-config-test-utils" }
1314
]

app-config-utils/src/index.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function isPrimitive(obj: Json): obj is JsonPrimitive {
2626
}
2727

2828
export function generateModuleText(
29-
fullConfig: Json,
29+
fullConfig: Json | 'no-config',
3030
{
3131
environment,
3232
useGlobalNamespace,
@@ -42,11 +42,25 @@ export function generateModuleText(
4242
): string {
4343
const privateName = '_appConfig';
4444
const privateEnvName = '_appConfigEnvironment';
45-
const config = JSON.stringify(fullConfig);
4645

4746
let generatedText = '';
4847

49-
if (useGlobalNamespace) {
48+
if (fullConfig === 'no-config') {
49+
generatedText += `
50+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
51+
52+
const config = globalNamespace.${privateName};
53+
54+
if (typeof config === 'undefined') {
55+
throw new Error('Config is not loaded in ${privateName}');
56+
}
57+
58+
export { config };
59+
export default config;
60+
`;
61+
} else if (useGlobalNamespace) {
62+
const config = JSON.stringify(fullConfig);
63+
5064
generatedText += `
5165
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
5266
@@ -67,6 +81,8 @@ export function generateModuleText(
6781
export default config;
6882
`;
6983
} else {
84+
const config = JSON.stringify(fullConfig);
85+
7086
generatedText += `
7187
const config = ${config};
7288

app-config-webpack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
},
3333
"dependencies": {
3434
"@app-config/config": "^2.8.1",
35+
"@app-config/schema": "^2.8.1",
3536
"@app-config/utils": "^2.8.1",
3637
"loader-utils": "2"
3738
},

app-config-webpack/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface Options {
1515
schemaLoadingOptions?: SchemaLoadingOptions;
1616
intercept?: RegExp;
1717
injectValidationFunction?: boolean;
18+
doNotLoadConfig?: boolean;
1819

1920
/** @deprecated use useGlobalNamespace instead */
2021
noGlobal?: boolean;
@@ -30,6 +31,7 @@ export default class AppConfigPlugin implements Options {
3031
loadingOptions?: ConfigLoadingOptions;
3132
schemaLoadingOptions?: SchemaLoadingOptions;
3233
injectValidationFunction: boolean;
34+
doNotLoadConfig: boolean;
3335
intercept: RegExp;
3436

3537
constructor(options: Options = {}) {
@@ -38,6 +40,7 @@ export default class AppConfigPlugin implements Options {
3840
this.loadingOptions = options.loadingOptions ?? options.loading;
3941
this.schemaLoadingOptions = options.schemaLoadingOptions ?? options.schemaLoading;
4042
this.injectValidationFunction = options.injectValidationFunction ?? true;
43+
this.doNotLoadConfig = options.doNotLoadConfig ?? false;
4144
this.intercept = options.intercept ?? AppConfigPlugin.regex;
4245
}
4346

@@ -66,6 +69,7 @@ export default class AppConfigPlugin implements Options {
6669
loadingOptions: this.loadingOptions,
6770
schemaLoadingOptions: this.schemaLoadingOptions,
6871
injectValidationFunction: this.injectValidationFunction,
72+
doNotLoadConfig: this.doNotLoadConfig,
6973

7074
// deprecated options
7175
noGlobal: !this.useGlobalNamespace,

app-config-webpack/src/loader.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getOptions, parseQuery } from 'loader-utils';
22
import { loadValidatedConfig } from '@app-config/config';
33
import { generateModuleText, packageNameRegex } from '@app-config/utils';
4+
import { loadSchema } from '@app-config/schema';
45
import type { Options } from './index';
56

67
type LoaderContext = Parameters<typeof getOptions>[0];
@@ -19,6 +20,29 @@ const loader = function AppConfigLoader(this: Loader) {
1920
const loadingOptions = options.loadingOptions ?? options.loading ?? {};
2021
const schemaLoadingOptions = options.schemaLoadingOptions ?? options.schemaLoading;
2122
const injectValidationFunction = options.injectValidationFunction ?? true;
23+
const doNotLoadConfig = options.doNotLoadConfig ?? false;
24+
25+
if (doNotLoadConfig) {
26+
loadSchema(schemaLoadingOptions)
27+
.then(({ validationFunctionCode }) => {
28+
callback(
29+
null,
30+
generateModuleText('no-config', {
31+
environment: undefined,
32+
useGlobalNamespace: true,
33+
validationFunctionCode: injectValidationFunction ? validationFunctionCode : undefined,
34+
esmValidationCode: false,
35+
}),
36+
);
37+
})
38+
.catch((err) => {
39+
this.emitWarning(new Error(`There was an error when trying to load @app-config`));
40+
41+
callback(err);
42+
});
43+
44+
return;
45+
}
2246

2347
loadValidatedConfig(loadingOptions, schemaLoadingOptions)
2448
.then(({ fullConfig, environment, filePaths, validationFunctionCode }) => {

0 commit comments

Comments
 (0)