Skip to content

Commit 7c672f3

Browse files
authored
Merge pull request #179 from launchcodedev/do-not-load-config
Adds a doNotLoadConfig option in bundler packages
2 parents de4ac5e + 6a9b42f commit 7c672f3

File tree

16 files changed

+216
-7
lines changed

16 files changed

+216
-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.2",
34+
"@app-config/schema": "^2.8.2",
3435
"@app-config/utils": "^2.8.2"
3536
},
3637
"devDependencies": {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ exports[`loads validation function 1`] = `
1414
"(()=>{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
`;
17+
18+
exports[`loads with noBundledConfig 1`] = `
19+
"(()=>{var v=(typeof window==\\"undefined\\"?globalThis:window)||{},i=v._appConfig;if(typeof i==\\"undefined\\")throw new Error(\\"Config is not loaded in _appConfig\\");function m(){let s={};return function(p){\\"use strict\\";p.exports=n,p.exports.default=n;var P={type:\\"object\\",additionalProperties:!1,properties:{foo:{type:\\"string\\"}},$schema:\\"http://json-schema.org/draft-07/schema#\\"};function n(o,t){\\"use strict\\";if(t)var r=t.dataPath,h=t.parentData,g=t.parentDataProperty,y=t.rootData;else var r=\\"\\",h=void 0,g=void 0,y=o;var e=null,a=0;if(o&&typeof o==\\"object\\"&&!Array.isArray(o)){for(var f in o)if(f!==\\"foo\\"){var d={keyword:\\"additionalProperties\\",dataPath:r,schemaPath:\\"#/additionalProperties\\",params:{additionalProperty:f},message:\\"should NOT have additional properties\\"};e===null?e=[d]:e.push(d),a++}if(o.foo!==void 0&&typeof o.foo!=\\"string\\"){var l={keyword:\\"type\\",dataPath:r+\\"/foo\\",schemaPath:\\"#/properties/foo/type\\",params:{type:\\"string\\"},message:\\"should be string\\"};e===null?e=[l]:e.push(l),a++}}else{var u={keyword:\\"type\\",dataPath:r,schemaPath:\\"#/type\\",params:{type:\\"object\\"},message:\\"should be object\\"};e===null?e=[u]:e.push(u),a++}return n.errors=e,a===0}}(s),s.exports}var c=m();c(i);})();
20+
"
21+
`;
22+
23+
exports[`loads with noBundledConfig and no validation function 1`] = `
24+
"(()=>{var o=(typeof window==\\"undefined\\"?globalThis:window)||{},n=o._appConfig;if(typeof n==\\"undefined\\")throw new Error(\\"Config is not loaded in _appConfig\\");console.log(n);})();
25+
"
26+
`;

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,67 @@ it('loads currentEnvironment', () =>
121121
expect(res.outputFiles[0].text).toMatchSnapshot();
122122
},
123123
));
124+
125+
it('loads with noBundledConfig', () =>
126+
withTempFiles(
127+
{
128+
'.app-config.schema.yml': `
129+
type: object
130+
additionalProperties: false
131+
properties:
132+
foo: { type: string }
133+
`,
134+
'a.js': `
135+
import { config, validateConfig } from '@app-config/main';
136+
137+
validateConfig(config);
138+
`,
139+
},
140+
async (inDir) => {
141+
const res = await build({
142+
entryPoints: [inDir('a.js')],
143+
plugins: [
144+
createPlugin({ schemaLoadingOptions: { directory: inDir('.') }, noBundledConfig: true }),
145+
],
146+
bundle: true,
147+
minify: true,
148+
write: false,
149+
});
150+
151+
expect(res.outputFiles[0].text).toMatchSnapshot();
152+
},
153+
));
154+
155+
it('loads with noBundledConfig and no validation function', () =>
156+
withTempFiles(
157+
{
158+
'.app-config.schema.yml': `
159+
type: object
160+
additionalProperties: false
161+
properties:
162+
foo: { type: string }
163+
`,
164+
'a.js': `
165+
import { config } from '@app-config/main';
166+
167+
console.log(config);
168+
`,
169+
},
170+
async (inDir) => {
171+
const res = await build({
172+
entryPoints: [inDir('a.js')],
173+
plugins: [
174+
createPlugin({
175+
schemaLoadingOptions: { directory: inDir('.') },
176+
noBundledConfig: true,
177+
injectValidationFunction: false,
178+
}),
179+
],
180+
bundle: true,
181+
minify: true,
182+
write: false,
183+
});
184+
185+
expect(res.outputFiles[0].text).toMatchSnapshot();
186+
},
187+
));

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 { appConfigImportRegex, 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+
noBundledConfig?: boolean;
1213
}
1314

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

3234
build.onLoad({ filter: /.*/, namespace: '@app-config/esbuild' }, async () => {
35+
if (noBundledConfig) {
36+
const { validationFunctionCode } = await loadSchema(schemaLoadingOptions);
37+
38+
const code = generateModuleText(undefined, {
39+
environment: undefined,
40+
useGlobalNamespace: true,
41+
validationFunctionCode: injectValidationFunction ? validationFunctionCode : undefined,
42+
esmValidationCode: true,
43+
});
44+
45+
return {
46+
loader: 'js',
47+
contents: code,
48+
resolveDir: path.parse(process.cwd()).root,
49+
watchFiles: [],
50+
};
51+
}
52+
3353
const { fullConfig, environment, validationFunctionCode, filePaths } =
3454
await loadValidatedConfig(loadingOptions, schemaLoadingOptions);
3555

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.2",
38+
"@app-config/schema": "^2.8.2",
3839
"@app-config/utils": "^2.8.2"
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 { appConfigImportRegex, 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+
noBundledConfig?: boolean;
1112

1213
/** @deprecated use useGlobalNamespace instead */
1314
readGlobal?: boolean;
@@ -48,11 +49,23 @@ async function loadConfig(
4849
loadingOptions,
4950
schemaLoadingOptions,
5051
injectValidationFunction = true,
52+
noBundledConfig,
5153
useGlobalNamespace,
5254
readGlobal,
5355
}: Options,
5456
currentFilePaths: string[],
5557
) {
58+
if (noBundledConfig) {
59+
const { validationFunctionCode } = await loadSchema(schemaLoadingOptions);
60+
61+
return generateModuleText(undefined, {
62+
environment: undefined,
63+
useGlobalNamespace: true,
64+
validationFunctionCode: injectValidationFunction ? validationFunctionCode : undefined,
65+
esmValidationCode: true,
66+
});
67+
}
68+
5669
const { fullConfig, environment, validationFunctionCode, filePaths } = await loadValidatedConfig(
5770
loadingOptions,
5871
schemaLoadingOptions,

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/__snapshots__/index.test.ts.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ exports[`generateModuleText creates config module with global namespace 1`] = `
7777
"
7878
`;
7979

80+
exports[`generateModuleText creates config module with noBundledConfig 1`] = `
81+
"
82+
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};
83+
84+
const config = globalNamespace._appConfig;
85+
86+
if (typeof config === 'undefined') {
87+
throw new Error('Config is not loaded in _appConfig');
88+
}
89+
90+
export { config };
91+
export default config;
92+
93+
export function currentEnvironment() {
94+
return globalNamespace._appConfigEnvironment || \\"test\\";
95+
}
96+
"
97+
`;
98+
8099
exports[`generateModuleText creates config module with validation function 1`] = `
81100
"
82101
const globalNamespace = (typeof window === 'undefined' ? globalThis : window) || {};

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ describe('generateModuleText', () => {
5959
).toMatchSnapshot();
6060
});
6161

62+
it('creates config module with noBundledConfig', () => {
63+
expect(
64+
generateModuleText(undefined, {
65+
environment: 'test',
66+
useGlobalNamespace: true,
67+
esmValidationCode: false,
68+
validationFunctionCode: undefined,
69+
}),
70+
).toMatchSnapshot();
71+
});
72+
6273
it('creates config module with validation function', () => {
6374
expect(
6475
generateModuleText(

0 commit comments

Comments
 (0)