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

feat: support importing package with ts export #10

Merged
merged 2 commits into from
May 18, 2022
Merged
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
54 changes: 26 additions & 28 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ const sourcemaps = installSourceMapSupport();
const tsconfig = getTsconfig();
const tsconfigRaw = tsconfig?.config;

type Resolved = {
url: string;
format: ModuleFormat;
};

type resolve = (
specifier: string,
context: {
conditions: string[];
parentURL: string | undefined;
},
defaultResolve: resolve,
) => MaybePromise<{
url: string;
format: ModuleFormat;
}>;
) => MaybePromise<Resolved>;

const hasExtensionPattern = /\.\w+$/;

Expand Down Expand Up @@ -69,25 +71,9 @@ export const resolve: resolve = async function (
}
}

if (tsExtensionsPattern.test(specifier)) {
const resolved = await defaultResolve(specifier, context, defaultResolve);
const format = getFormatFromExtension(resolved.url) ?? await getPackageType(resolved.url);

return {
...resolved,
format,
};
}

if (specifier.endsWith('.json')) {
return {
...(await defaultResolve(specifier, context, defaultResolve)),
format: 'json',
};
}

let resolved: Resolved;
try {
return await defaultResolve(specifier, context, defaultResolve);
resolved = await defaultResolve(specifier, context, defaultResolve);
} catch (error) {
if (error instanceof Error) {
if ((error as any).code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
Expand All @@ -114,6 +100,23 @@ export const resolve: resolve = async function (

throw error;
}

if (resolved.url.endsWith('.json')) {
return {
...resolved,
format: 'json',
};
}

if (tsExtensionsPattern.test(resolved.url)) {
const format = getFormatFromExtension(resolved.url) ?? await getPackageType(resolved.url);
return {
...resolved,
format,
};
}

return resolved;
};

type load = (
Expand Down Expand Up @@ -149,12 +152,7 @@ export const load: load = async function (

const loaded = await defaultLoad(url, context, defaultLoad);

if (
!loaded.source

// node_modules don't need to be transformed
|| url.includes('/node_modules/')
) {
if (!loaded.source) {
return loaded;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/specs/typescript/dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { testSuite, expect } from 'manten';
import type { NodeApis } from '../../utils/node-with-loader';

export default testSuite(async ({ describe }, node: NodeApis) => {
describe('Dependencies', ({ describe }) => {
describe('TypeScript dependency', ({ test }) => {
const output = '{"default":"ts default export","namedExport":"ts named export"}';

test('Import', async () => {
const nodeProcess = await node.import('package-module/ts.ts');
expect(nodeProcess.stdout).toBe(output);
});

test('Import', async () => {
const nodeProcess = await node.import('package-typescript-export');
expect(nodeProcess.stdout).toBe(output);
});
});
});
});
1 change: 1 addition & 0 deletions tests/specs/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
runTestSuite(import('./mts'), node);
runTestSuite(import('./cts'), node);
runTestSuite(import('./tsconfig'), node);
runTestSuite(import('./dependencies'), node);
});
});