Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(esm): support custom conditions #675

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/esm/hook/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { PackageJson } from 'type-fest';
import { readJsonFile } from '../../utils/read-json-file.js';
import { mapTsExtensions } from '../../utils/map-ts-extensions.js';
import type { NodeError } from '../../types.js';
import { tsconfigPathsMatcher, allowJs } from '../../utils/tsconfig.js';
import { tsconfigPathsMatcher, allowJs, customConditions } from '../../utils/tsconfig.js';
import {
requestAcceptsQuery,
fileUrlPrefix,
Expand Down Expand Up @@ -240,6 +240,10 @@ export const resolve: ResolveHook = async (
return nextResolve(specifier, context);
}

if (customConditions?.length) {
context.conditions = context.conditions.concat(customConditions);
}

let requestNamespace = getNamespace(specifier) ?? (
// Inherit namespace from parent
context.parentURL && getNamespace(context.parentURL)
Expand Down
4 changes: 4 additions & 0 deletions src/utils/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export let tsconfigPathsMatcher: undefined | ReturnType<typeof createPathsMatche
// eslint-disable-next-line import-x/no-mutable-exports
export let allowJs = false;

// eslint-disable-next-line import-x/no-mutable-exports
export let customConditions: string[] | undefined;

export const loadTsconfig = (
configPath?: string,
) => {
Expand Down Expand Up @@ -52,4 +55,5 @@ export const loadTsconfig = (
fileMatcher = createFilesMatcher(tsconfig);
tsconfigPathsMatcher = createPathsMatcher(tsconfig);
allowJs = tsconfig?.config.compilerOptions?.allowJs ?? false;
customConditions = tsconfig?.config.compilerOptions?.customConditions;
};
78 changes: 76 additions & 2 deletions tests/specs/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import type { NodeApis } from '../utils/tsx.js';
import {
expectErrors, jsxCheck, createPackageJson, createTsconfig,
expectErrors,
jsxCheck,
createPackageJson,
createTsconfig,
} from '../fixtures.js';
import { packageTypes } from '../utils/package-types.js';

Expand All @@ -13,7 +16,12 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
const fixture = await createFixture({
...expectErrors,

'package.json': createPackageJson({ type: packageType }),
'package.json': createPackageJson({
type: packageType,
dependencies: {
'custom-condition-package': '*',
},
}),

'tsconfig.json': createTsconfig({
compilerOptions: {
Expand Down Expand Up @@ -41,6 +49,13 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
extends: 'doesnt-exist',
}),

'tsconfig-customConditions.json': createTsconfig({
extends: './tsconfig.json',
compilerOptions: {
customConditions: ['source'],
},
}),

'index.tsx': `
${jsxCheck};

Expand Down Expand Up @@ -85,6 +100,10 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
console.log('imported');
`,

'custom-condition.js': `
import 'custom-condition-package';
`,

'node_modules/tsconfig-should-not-apply': {
'package.json': createPackageJson({
exports: {
Expand All @@ -109,6 +128,21 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
);
`,
},

'node_modules/custom-condition-package': {
'package.json': createPackageJson({
exports: {
source: './src/index.js',
import: './dist/index.js',
},
}),
'src/index.js': `
console.log('imported from source');
`,
'dist/index.js': `
console.log('imported from dist');
`,
},
});
onFinish(async () => await fixture.rm());

Expand Down Expand Up @@ -173,6 +207,46 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
expect(pTsconfigAllowJs.stderr).toBe('');
expect(pTsconfigAllowJs.stdout).toBe('imported');
});

if (packageType === 'module') {
test('no customConditions in tsconfig.json for esm', async ({
onTestFail,
}) => {
const pTsconfigCustomConditions = await tsx(
['custom-condition.js'],
fixture.path,
);
onTestFail((error) => {
console.error(error);
console.log(pTsconfigCustomConditions);
});
expect(pTsconfigCustomConditions.failed).toBe(false);
expect(pTsconfigCustomConditions.stderr).toBe('');
expect(pTsconfigCustomConditions.stdout).toBe('imported from dist');
});

test('customConditions in tsconfig.json for esm', async ({
onTestFail,
}) => {
const pTsconfigCustomConditions = await tsx(
[
'--tsconfig',
'tsconfig-customConditions.json',
'custom-condition.js',
],
fixture.path,
);
onTestFail((error) => {
console.error(error);
console.log(pTsconfigCustomConditions);
});
expect(pTsconfigCustomConditions.failed).toBe(false);
expect(pTsconfigCustomConditions.stderr).toBe('');
expect(pTsconfigCustomConditions.stdout).toBe(
'imported from source',
);
});
}
});
});
}
Expand Down