Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
fix: support query in specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Sep 17, 2023
1 parent a237b20 commit 6aa91b6
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 11 deletions.
15 changes: 10 additions & 5 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import {
type NodeError,
} from './utils.js';

const isJsonPattern = /\.json($|\?)/;
const isDirectoryPattern = /\/($|\?)/; // Not sure if queries are allowed in directories

type NextResolve = (
specifier: string,
context?: ResolveHookContext,
Expand Down Expand Up @@ -75,11 +78,12 @@ async function tryExtensions(
context: ResolveHookContext,
defaultResolve: NextResolve,
) {
const [specifierWithoutQuery, query] = specifier.split('?');
let throwError: Error | undefined;
for (const extension of extensions) {
try {
return await resolve(
specifier + extension,
specifierWithoutQuery + extension + (query ? `?${query}` : ''),
context,
defaultResolve,
true,
Expand All @@ -105,11 +109,12 @@ async function tryDirectory(
context: ResolveHookContext,
defaultResolve: NextResolve,
) {
const isExplicitDirectory = specifier.endsWith('/');
const isExplicitDirectory = isDirectoryPattern.test(specifier);//.endsWith('/');
const appendIndex = isExplicitDirectory ? 'index' : '/index';
const [specifierWithoutQuery, query] = specifier.split('?');

try {
return await tryExtensions(specifier + appendIndex, context, defaultResolve);
return await tryExtensions(specifierWithoutQuery + appendIndex + (query ? `?${query}` : ''), context, defaultResolve);
} catch (_error) {
if (!isExplicitDirectory) {
try {
Expand Down Expand Up @@ -145,7 +150,7 @@ export const resolve: resolve = async function (
}

// If directory, can be index.js, index.ts, etc.
if (specifier.endsWith('/')) {
if (isDirectoryPattern.test(specifier)) {
return await tryDirectory(specifier, context, defaultResolve);
}

Expand Down Expand Up @@ -243,7 +248,7 @@ export const load: LoadHook = async function (
});
}

if (url.endsWith('.json')) {
if (isJsonPattern.test(url)) {
if (!context.importAssertions) {
context.importAssertions = {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);

export const fileProtocol = 'file://';

export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)$/;
export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)($|\?)/;

const getFormatFromExtension = (fileUrl: string): ModuleFormat | undefined => {
const extension = path.extname(fileUrl);
Expand Down
14 changes: 14 additions & 0 deletions tests/specs/javascript/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type { NodeApis } from '../../utils/node-with-loader.js';
import nodeSupports from '../../utils/node-supports.js';
import { assertNotFound } from '../../utils/assertions.js';

const query = '?query=123';

export default testSuite(async ({ describe }, node: NodeApis) => {
describe('Load ESM', ({ describe }) => {
describe('.mjs extension', ({ describe }) => {
Expand Down Expand Up @@ -36,6 +38,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});

test('Import with query', async () => {
const nodeProcess = await node.import(importPath + query);
assertResults(nodeProcess);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});

test('TypeScript Import', async () => {
const nodeProcess = await node.import(importPath, { typescript: true });
assertResults(nodeProcess);
Expand Down Expand Up @@ -101,6 +109,12 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
assertResults(nodeProcess);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});

test('Import with query', async () => {
const nodeProcess = await node.import(importPath + query);
assertResults(nodeProcess);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});
});

describe('extensionless', ({ test }) => {
Expand Down
19 changes: 16 additions & 3 deletions tests/specs/json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import type { NodeApis } from '../utils/node-with-loader.js';
import { query } from '../utils/query.js';

const jsonFixture = {
'package.json': JSON.stringify({
Expand Down Expand Up @@ -28,6 +29,11 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
const nodeProcess = await node.importFile(fixture.path, './index.json');
expect(nodeProcess.stdout).toMatch('default: { loaded: \'json\' }');
});

test('Import with query', async () => {
const nodeProcess = await node.importFile(fixture.path, './index.json' + query);
expect(nodeProcess.stdout).toMatch('default: { loaded: \'json\' }');
});
});

describe('extensionless', ({ test }) => {
Expand All @@ -41,14 +47,16 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
const nodeProcess = await node.importFile(fixture.path, './index');
expect(nodeProcess.stdout).toMatch('default: { loaded: \'json\' }');
});

test('Import with query', async () => {
const nodeProcess = await node.importFile(fixture.path, './index' + query);
expect(nodeProcess.stdout).toMatch('default: { loaded: \'json\' }');
});
});

describe('directory', ({ test }) => {
test('Load', async ({ onTestFail }) => {
const nodeProcess = await node.loadFile(fixture.path, '.');
onTestFail(() => {
console.log(nodeProcess);
});
expect(nodeProcess.exitCode).toBe(0);
expect(nodeProcess.stdout).toBe('');
});
Expand All @@ -57,6 +65,11 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
const nodeProcess = await node.importFile(fixture.path, '.');
expect(nodeProcess.stdout).toMatch('default: { loaded: \'json\' }');
});

test('Import with query', async ({ onTestFail }) => {
const nodeProcess = await node.importFile(fixture.path, './' + query);
expect(nodeProcess.stdout).toMatch('default: { loaded: \'json\' }');
});
});

describe('ambiguous path', async ({ describe, onFinish }) => {
Expand Down
5 changes: 3 additions & 2 deletions tests/specs/typescript/mts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import semver from 'semver';
import type { NodeApis } from '../../utils/node-with-loader.js';
import nodeSupports from '../../utils/node-supports.js';
import { assertNotFound } from '../../utils/assertions.js';
import { query } from '../../utils/query.js';

export default testSuite(async ({ describe }, node: NodeApis) => {
describe('.mts extension', ({ describe }) => {
Expand All @@ -28,8 +29,8 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
assertResults(nodeProcess.stdout);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
test('Import', async ({ onTestFail }) => {
const nodeProcess = await node.import(importPath + query);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});
Expand Down
1 change: 1 addition & 0 deletions tests/specs/typescript/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import semver from 'semver';
import type { NodeApis } from '../../utils/node-with-loader.js';
import nodeSupports from '../../utils/node-supports.js';
import { assertNotFound } from '../../utils/assertions.js';
import { query } from '../../utils/query.js';

export default testSuite(async ({ describe }, node: NodeApis) => {
describe('.ts extension', ({ describe }) => {
Expand Down
1 change: 1 addition & 0 deletions tests/utils/node-with-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const nodeWithLoader = (
nodePath: options.nodePath,
cwd: options.cwd,
reject: false,
all: true,
},
);

Expand Down
1 change: 1 addition & 0 deletions tests/utils/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const query = '?query=123';

0 comments on commit 6aa91b6

Please sign in to comment.