Skip to content

Commit e144d74

Browse files
authored
fix: should load json file correctly (#702)
1 parent 2e16e3c commit e144d74

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

e2e/test-api/json.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect, it } from '@rstest/core';
2+
3+
it('should test json file correctly', async () => {
4+
const jsonPath = './test.json';
5+
// will external and load json file in runtime
6+
const json = await import(jsonPath);
7+
// will bundle json file during build
8+
const jsonA = await import('./test.json');
9+
10+
expect(json.value).toBe(123);
11+
expect(json).toEqual(jsonA);
12+
});

e2e/test-api/test.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"value": 123
3+
}

packages/core/src/runtime/worker/loadModule.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ const defineRstestDynamicImport =
8282
delete importAttributes.with.rstest;
8383
}
8484

85+
if (modulePath.endsWith('.json')) {
86+
// const json = await import(jsonPath);
87+
// should return { default: jsonExports, ...jsonExports }
88+
const importedModule = await import(modulePath, {
89+
with: { type: 'json' },
90+
});
91+
92+
return returnModule
93+
? asModule(importedModule.default, importedModule.default)
94+
: {
95+
...importedModule.default,
96+
default: importedModule.default,
97+
};
98+
}
99+
85100
const importedModule = await import(modulePath, importAttributes);
86101

87102
if (

0 commit comments

Comments
 (0)