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

fix: support export default null #22

Merged
merged 1 commit into from
Jun 17, 2024
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
4 changes: 2 additions & 2 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// commonjs
obj = require(moduleFilePath);
debug('[importModule] require %o => %o', filepath, obj);
if (obj?.__esModule === true && obj?.default) {
if (obj?.__esModule === true && 'default' in obj) {

Check warning on line 38 in src/import.ts

View check run for this annotation

Codecov / codecov/patch

src/import.ts#L38

Added line #L38 was not covered by tests
// 兼容 cjs 模拟 esm 的导出格式
// {
// __esModule: true,
Expand Down Expand Up @@ -72,7 +72,7 @@
obj = obj.default;
}
if (options?.importDefaultOnly) {
if (obj.default) {
if ('default' in obj) {
obj = obj.default;
}
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cjs/module-exports-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = null;
1 change: 1 addition & 0 deletions test/fixtures/esm/export-default-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default null;
24 changes: 24 additions & 0 deletions test/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,29 @@ describe('test/import.test.ts', () => {
assert.equal(obj.foo, 'bar');
assert.equal(obj.one, 1);
});

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

it('should support export default null', async () => {
assert.equal(await importModule(getFilepath('esm/export-default-null.js'), {
importDefaultOnly: true,
}), null);
assert.equal(await importModule(getFilepath('esm/export-default-null'), {
importDefaultOnly: true,
}), null);
assert.equal((await importModule(getFilepath('esm/export-default-null.js'), {
importDefaultOnly: false,
})).default, null);
});
});
});
Loading