Skip to content

Commit 9bd2546

Browse files
committed
fix(json): handle keys with special characters
1 parent e97fe67 commit 9bd2546

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/esm/hook/load.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ let load: LoadHook = async (
102102
},
103103
);
104104

105-
const jsonExportKeys = Object.keys(JSON.parse(code));
106-
transformed.code += `\n0 && (module.exports = {${jsonExportKeys.join(',')}});`;
105+
transformed.code += `\n0 && (module.exports = ${
106+
JSON.stringify(Object.fromEntries(Object.keys(JSON.parse(code)).map(key => [key, 0])))
107+
});`;
107108

108109
return {
109110
shortCircuit: true,
@@ -300,8 +301,9 @@ let load: LoadHook = async (
300301
},
301302
);
302303

303-
const jsonExportKeys = Object.keys(JSON.parse(code));
304-
transformed.code += `\n0 && (module.exports = {${jsonExportKeys.join(',')}});`;
304+
transformed.code += `\n0 && (module.exports = ${
305+
JSON.stringify(Object.fromEntries(Object.keys(JSON.parse(code)).map(key => [key, 0])))
306+
});`;
305307

306308
return {
307309
format: 'commonjs',

tests/fixtures.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export const files = {
141141
import '../json/'
142142
`,
143143

144-
'json/index.json': JSON.stringify({ loaded: 'json' }),
144+
'json/index.json': JSON.stringify({ 'loaded-file': 'json' }),
145145

146146
'cjs/index.cjs': sourcemap.tag`
147147
const assert = require('node:assert');

tests/specs/smoke.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
161161
expect(p.failed).toBe(false);
162162
expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(fixture.getPath('import-from-js.js'))}"`);
163163
expect(p.stdout).toMatch(`"js":{"cjsContext":${isCommonJs},"default":1,"named":2}`);
164-
expect(p.stdout).toMatch('"json":{"default":{"loaded":"json"},"loaded":"json"}');
164+
expect(p.stdout).toMatch('"json":{"default":{"loaded-file":"json"},"loaded-file":"json"}');
165165

166166
if (supports.exportModuleExports && !isCommonJs) {
167167
expect(p.stdout).toMatch('"cjs":{"default":{"named":"named"},"module.exports":{"named":"named"},"named":"named"}');
@@ -422,7 +422,7 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
422422
expect(p.failed).toBe(false);
423423
expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(fixture.getPath('import-from-ts.ts'))}"`);
424424
expect(p.stdout).toMatch(`"js":{"cjsContext":${isCommonJs},"default":1,"named":2}`);
425-
expect(p.stdout).toMatch('"json":{"default":{"loaded":"json"},"loaded":"json"}');
425+
expect(p.stdout).toMatch('"json":{"default":{"loaded-file":"json"},"loaded-file":"json"}');
426426
if (supports.exportModuleExports && !isCommonJs) {
427427
expect(p.stdout).toMatch('"cjs":{"default":{"named":"named"},"module.exports":{"named":"named"},"named":"named"}');
428428
} else {

0 commit comments

Comments
 (0)