Skip to content

Commit

Permalink
fix: support ts-module (#23)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced compatibility with TypeScript modules for improved import
functionality.
  
- **Tests**
- Added comprehensive test cases to validate TypeScript module imports,
covering default imports, named exports, and module-level exports,
ensuring robust and reliable behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Jun 17, 2024
1 parent 5095b35 commit c032932
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function importModule(filepath: string, options?: ImportModuleOptio
// one: 1,
// [Symbol(Symbol.toStringTag)]: 'Module'
// }
if (obj?.__esModule === true && obj?.default?.__esModule === true) {
if (obj?.default?.__esModule === true && 'default' in obj?.default) {
// 兼容 cjs 模拟 esm 的导出格式
// {
// __esModule: true,
Expand All @@ -69,6 +69,18 @@ export async function importModule(filepath: string, options?: ImportModuleOptio
// },
// [Symbol(Symbol.toStringTag)]: 'Module'
// }
// 兼容 ts module
// {
// default: {
// [__esModule]: true,
// default: <ref *1> [Function: default_1] {
// [length]: 0,
// [name]: 'default_1',
// [prototype]: { [constructor]: [Circular *1] }
// }
// },
// [Symbol(Symbol.toStringTag)]: 'Module'
// }
obj = obj.default;
}
if (options?.importDefaultOnly) {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/ts-module/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const foo = 'bar';
export const one = 1;
5 changes: 5 additions & 0 deletions test/fixtures/ts-module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
foo: 'bar',
};

export const one = 1;
3 changes: 3 additions & 0 deletions test/fixtures/ts-module/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function() {
return { a: 1 };
}
34 changes: 34 additions & 0 deletions test/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,40 @@ describe('test/import.test.ts', () => {
assert.equal(obj.one, 1);
});

it('should work on ts-module', async () => {
let obj = await importModule(getFilepath('ts-module'));
assert.deepEqual(Object.keys(obj), [ 'default', 'one' ]);
assert.equal(obj.one, 1);
assert.deepEqual(obj.default, { foo: 'bar' });

obj = await importModule(getFilepath('ts-module'), { importDefaultOnly: true });
assert.deepEqual(obj, { foo: 'bar' });

obj = await importModule(getFilepath('ts-module/exports'));
assert.deepEqual(Object.keys(obj), [ 'foo', 'one' ]);
assert.equal(obj.foo, 'bar');
assert.equal(obj.one, 1);

obj = await importModule(getFilepath('ts-module/exports'), { importDefaultOnly: true });
assert.deepEqual(Object.keys(obj), [ 'foo', 'one' ]);
assert.equal(obj.foo, 'bar');
assert.equal(obj.one, 1);

obj = await importModule(getFilepath('ts-module/exports.ts'));
assert.deepEqual(Object.keys(obj), [ 'foo', 'one' ]);
assert.equal(obj.foo, 'bar');
assert.equal(obj.one, 1);

obj = await importModule(getFilepath('ts-module/mod'));
assert.deepEqual(Object.keys(obj), [ 'default' ]);
assert.equal(typeof obj.default, 'function');

obj = await importModule(getFilepath('ts-module/mod.ts'), {
importDefaultOnly: true,
});
assert.equal(typeof obj, 'function');
});

it('should support module.exports = null', async () => {
assert.equal(await importModule(getFilepath('cjs/module-exports-null.js'), {
importDefaultOnly: true,
Expand Down

0 comments on commit c032932

Please sign in to comment.