Skip to content

Commit 6b03a38

Browse files
committedJun 13, 2024·
fix(cjs): load json with namespace
fixes #587
1 parent 817d6b2 commit 6b03a38

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed
 

‎src/cjs/api/module-resolve-filename.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,13 @@ export const createResolveFilename = (
195195
let resolved = resolveRequest(requestAndQuery[0], parent, resolve);
196196

197197
// Only add query back if it's a file path (not a core Node module)
198-
if (path.isAbsolute(resolved)) {
198+
if (
199+
path.isAbsolute(resolved)
200+
201+
// These two have native loaders which don't support queries
202+
&& !resolved.endsWith('.json')
203+
&& !resolved.endsWith('.node')
204+
) {
199205
resolved += urlSearchParamsStringify(searchParams);
200206
}
201207

‎tests/specs/api.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { createPackageJson, createTsconfig, expectErrors } from '../fixtures.js'
1414
const tsFiles = {
1515
'file.ts': `
1616
import { foo } from './foo'
17-
export const message = \`\${foo} \${(typeof __filename === 'undefined' ? import.meta.url : __filename).split(/[\\\\/]/).pop()}\` as string
17+
import { json } from './json.json'
18+
export const message = \`\${foo} \${json} \${(typeof __filename === 'undefined' ? import.meta.url : __filename).split(/[\\\\/]/).pop()}\` as string
1819
export { async } from './foo'
1920
`,
2021
'foo.ts': `
@@ -36,6 +37,7 @@ const tsFiles = {
3637
'esm-syntax.js': 'export default "cts export"',
3738
'bar.ts': 'export type A = 1; export { bar } from "pkg"',
3839
'async.ts': 'export default "async"',
40+
'json.json': JSON.stringify({ json: 'json' }),
3941
'node_modules/pkg': {
4042
'package.json': createPackageJson({
4143
name: 'pkg',
@@ -54,7 +56,7 @@ const tsFiles = {
5456

5557
export default testSuite(({ describe }, node: NodeApis) => {
5658
describe('API', ({ describe }) => {
57-
describe('CommonJS', ({ test }) => {
59+
describe('CommonJS', ({ describe, test }) => {
5860
test('cli', async () => {
5961
await using fixture = await createFixture({
6062
'index.ts': 'import { message } from \'./file\';\n\nconsole.log(message, new Error().stack);',
@@ -104,12 +106,12 @@ export default testSuite(({ describe }, node: NodeApis) => {
104106
nodeOptions: [],
105107
});
106108

107-
expect(stdout).toBe('Fails as expected\nfoo bar file.ts\nUnregistered');
109+
expect(stdout).toBe('Fails as expected\nfoo bar json file.ts\nUnregistered');
108110
});
109111

110112
describe('tsx.require()', ({ test }) => {
111113
test('loads', async () => {
112-
await using fixture = await createFixture({
114+
const fixture = await createFixture({
113115
'require.cjs': `
114116
const path = require('node:path');
115117
const tsx = require(${JSON.stringify(tsxCjsApiPath)});
@@ -135,13 +137,14 @@ export default testSuite(({ describe }, node: NodeApis) => {
135137
`,
136138
...tsFiles,
137139
});
140+
console.log(fixture.path);
138141

139142
const { stdout } = await execaNode(fixture.getPath('require.cjs'), [], {
140143
nodePath: node.path,
141144
nodeOptions: [],
142145
});
143146

144-
expect(stdout).toMatch(/Fails as expected\nfoo bar file.ts\nfile.ts\?namespace=\d+\nUnpolluted global require/);
147+
expect(stdout).toMatch(/Fails as expected\nfoo bar json file.ts\nfile.ts\?namespace=\d+\nUnpolluted global require/);
145148
});
146149

147150
test('catchable', async () => {
@@ -197,7 +200,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
197200
nodeOptions: [],
198201
});
199202

200-
expect(stdout).toBe('foo bar file.ts\nfoo bar file.ts\nfoo bar file.ts\nUnregistered');
203+
expect(stdout).toBe('foo bar json file.ts\nfoo bar json file.ts\nfoo bar json file.ts\nUnregistered');
201204
});
202205

203206
test('namespace', async () => {
@@ -229,7 +232,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
229232
nodeOptions: [],
230233
});
231234

232-
expect(stdout).toBe('foo bar file.ts\nasync');
235+
expect(stdout).toBe('foo bar json file.ts\nasync');
233236
});
234237
});
235238
});
@@ -277,7 +280,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
277280
nodeOptions: [],
278281
});
279282

280-
expect(stdout).toBe('Fails as expected\nfoo bar file.ts?nocache');
283+
expect(stdout).toBe('Fails as expected\nfoo bar json file.ts?nocache');
281284
});
282285

283286
describe('register / unregister', ({ test, describe }) => {
@@ -323,7 +326,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
323326
nodePath: node.path,
324327
nodeOptions: [],
325328
});
326-
expect(stdout).toBe('Fails as expected 1\nfoo bar file.ts?2\nFails as expected 2\nfoo bar file.ts?4');
329+
expect(stdout).toBe('Fails as expected 1\nfoo bar json file.ts?2\nFails as expected 2\nfoo bar json file.ts?4');
327330
});
328331

329332
test('onImport', async () => {
@@ -347,7 +350,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
347350
nodePath: node.path,
348351
nodeOptions: [],
349352
});
350-
expect(stdout).toBe('file.ts\nfoo.ts\npromises\nbar.ts\nindex.js\nnode:process');
353+
expect(stdout).toBe('file.ts\nfoo.ts\njson.json\npromises\nbar.ts\nindex.js\nnode:process');
351354
});
352355

353356
test('namespace & onImport', async () => {
@@ -375,7 +378,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
375378
nodePath: node.path,
376379
nodeOptions: [],
377380
});
378-
expect(stdout).toBe('file.ts\nfoo.ts\nbar.ts\nindex.js');
381+
expect(stdout).toBe('file.ts\nfoo.ts\njson.json\nbar.ts\nindex.js');
379382
});
380383

381384
describe('tsconfig', ({ test }) => {
@@ -547,9 +550,9 @@ export default testSuite(({ describe }, node: NodeApis) => {
547550
});
548551

549552
if (node.supports.cjsInterop) {
550-
expect(stdout).toMatch(/Fails as expected 1\nfoo bar file\.ts\?tsx-namespace=\d+\ncts loaded\ncts export\nfoo bar file\.ts\?with-query=&tsx-namespace=\d+\nFails as expected 2/);
553+
expect(stdout).toMatch(/Fails as expected 1\nfoo bar json file\.ts\?tsx-namespace=\d+\ncts loaded\ncts export\nfoo bar json file\.ts\?with-query=&tsx-namespace=\d+\nFails as expected 2/);
551554
} else {
552-
expect(stdout).toMatch(/Fails as expected 1\nfoo bar file\.ts\?tsx-namespace=\d+\nSyntaxError\nSyntaxError\nfoo bar file\.ts\?with-query=&tsx-namespace=\d+\nFails as expected 2/);
555+
expect(stdout).toMatch(/Fails as expected 1\nfoo bar json file\.ts\?tsx-namespace=\d+\nSyntaxError\nSyntaxError\nfoo bar json file\.ts\?with-query=&tsx-namespace=\d+\nFails as expected 2/);
553556
}
554557
});
555558

@@ -583,7 +586,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
583586
nodePath: node.path,
584587
nodeOptions: [],
585588
});
586-
expect(stdout).toMatch(/Fails as expected 1\nfoo bar file\.ts\?tsx-namespace=\d+\nfoo bar file\.ts\?with-query=&tsx-namespace=\d+\nFails as expected 2/);
589+
expect(stdout).toMatch(/Fails as expected 1\nfoo bar json file\.ts\?tsx-namespace=\d+\nfoo bar json file\.ts\?with-query=&tsx-namespace=\d+\nFails as expected 2/);
587590
});
588591

589592
test('mts from commonjs', async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.