Skip to content

Commit f176414

Browse files
committed
feat: uses resolveFilepath for $jsModule to resolve relative files
1 parent cb7ffea commit f176414

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

app-config-js/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
},
3232
"dependencies": {
3333
"@app-config/core": "^2.2.0",
34-
"@app-config/extension-utils": "^2.2.0"
34+
"@app-config/extension-utils": "^2.2.0",
35+
"@app-config/node": "^2.2.0"
3536
},
3637
"devDependencies": {
3738
"@app-config/test-utils": "^2.2.0"

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LiteralSource } from '@app-config/core';
2+
import { FileSource } from '@app-config/node';
23
import { withTempFiles } from '@app-config/test-utils';
34
import jsModuleDirective from './index';
45

@@ -65,6 +66,24 @@ describe('$jsModule directive', () => {
6566
$jsModule: inDir('foo.js'),
6667
});
6768

69+
expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar');
70+
},
71+
));
72+
73+
it('loads file relative to app-config', () =>
74+
withTempFiles(
75+
{
76+
'config.yml': `
77+
$jsModule: ./foo.js
78+
`,
79+
'foo.js': `
80+
module.exports.__esModule = true;
81+
module.exports.default = 'bar';
82+
`,
83+
},
84+
async (inDir) => {
85+
const source = new FileSource(inDir('config.yml'));
86+
6887
expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar');
6988
},
7089
));

app-config-js/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ParsingExtension } from '@app-config/core';
22
import { forKey, validateOptions } from '@app-config/extension-utils';
3+
import { resolveFilepath } from '@app-config/node';
34

45
/* eslint-disable @typescript-eslint/no-unsafe-call */
56
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
@@ -10,8 +11,10 @@ export default function jsModuleDirective(): ParsingExtension {
1011
'$jsModule',
1112
validateOptions(
1213
(SchemaBuilder) => SchemaBuilder.stringSchema(),
13-
(value) => async (parse) => {
14-
let loaded: any = await import(value);
14+
(value) => async (parse, _, context) => {
15+
const resolvedPath = resolveFilepath(context, value);
16+
17+
let loaded: any = await import(resolvedPath);
1518

1619
if (!loaded) {
1720
return parse(loaded, { shouldFlatten: true });

app-config-js/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"references": [
1010
{ "path": "../app-config-test-utils" },
1111
{ "path": "../app-config-core" },
12+
{ "path": "../app-config-node" },
1213
{ "path": "../app-config-extension-utils" }
1314
]
1415
}

0 commit comments

Comments
 (0)