diff --git a/e2e/test-api/json.test.ts b/e2e/test-api/json.test.ts new file mode 100644 index 000000000..37d617637 --- /dev/null +++ b/e2e/test-api/json.test.ts @@ -0,0 +1,12 @@ +import { expect, it } from '@rstest/core'; + +it('should test json file correctly', async () => { + const jsonPath = './test.json'; + // will external and load json file in runtime + const json = await import(jsonPath); + // will bundle json file during build + const jsonA = await import('./test.json'); + + expect(json.value).toBe(123); + expect(json).toEqual(jsonA); +}); diff --git a/e2e/test-api/test.json b/e2e/test-api/test.json new file mode 100644 index 000000000..a5be1e7ee --- /dev/null +++ b/e2e/test-api/test.json @@ -0,0 +1,3 @@ +{ + "value": 123 +} diff --git a/packages/core/src/runtime/worker/loadModule.ts b/packages/core/src/runtime/worker/loadModule.ts index 4a24e3ba1..212a1c970 100644 --- a/packages/core/src/runtime/worker/loadModule.ts +++ b/packages/core/src/runtime/worker/loadModule.ts @@ -82,6 +82,21 @@ const defineRstestDynamicImport = delete importAttributes.with.rstest; } + if (modulePath.endsWith('.json')) { + // const json = await import(jsonPath); + // should return { default: jsonExports, ...jsonExports } + const importedModule = await import(modulePath, { + with: { type: 'json' }, + }); + + return returnModule + ? asModule(importedModule.default, importedModule.default) + : { + ...importedModule.default, + default: importedModule.default, + }; + } + const importedModule = await import(modulePath, importAttributes); if (