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

Export default for isolatation package #136

Open
ZLY201 opened this issue Nov 16, 2022 · 9 comments
Open

Export default for isolatation package #136

ZLY201 opened this issue Nov 16, 2022 · 9 comments
Assignees
Labels
enhancement New feature or request

Comments

@ZLY201
Copy link
Member

ZLY201 commented Nov 16, 2022

Is your feature request related to a problem? Please describe.

Now:

import { OrderedSet } from '@js-sdsl/ordered-set';

Describe the solution you'd like

I want:

import OrderedSet from '@js-sdsl/ordered-set';

Additional context

import { OrderedSet } from '@js-sdsl/ordered-set';

It doesn't make sense because there is only one member has been exported which named OrderedSet.

Does it possible? @noname0310

I think the problem will be require doesn't support default.

@ZLY201 ZLY201 changed the title export default for isolatation package Export default for isolatation package Nov 16, 2022
@ZLY201 ZLY201 added the enhancement New feature or request label Nov 16, 2022
@noname0310
Copy link
Collaborator

I think it will be possible only if we solve the cjs export.

@noname0310
Copy link
Collaborator

How about applying export default only to esm?

@ZLY201

@ZLY201
Copy link
Member Author

ZLY201 commented Dec 3, 2022

How about applying export default only to esm?

@ZLY201

What do you mean?

@noname0310
Copy link
Collaborator

provide export default only to users who use esm.

@ZLY201
Copy link
Member Author

ZLY201 commented Dec 5, 2022

Do you mean:

import OrderedSet from 'ordered-set';
const { OrderedSet } = require('ordered-set');

@noname0310
Copy link
Collaborator

yes

@ZLY201
Copy link
Member Author

ZLY201 commented Dec 5, 2022

yes

Why cjs cannot transform to const OrderedSet = require('ordered-set')?

@noname0310
Copy link
Collaborator

noname0310 commented Dec 5, 2022

The problem is when the named export exists together with default export.

If you use named export, the value you received in require becomes an object.

However, it is generally impossible to be an object and a constructor of an object(a.k.a class) at the same time, so if you do default export at the same time as named export, one side is ignored.

// lib.ts
class Foo { }
class Bar { }
class Baz { }

export { Foo, Bar };
export default Baz; // this will be ignored in CJS
// index.ts

// This value will be { "Foo": [object Object], "Bar": [object Object] } 
const lib = require('./lib.js');

// Because of the Destructuring, it seems like it is the same as a named export. But what the `require` returned was just an object
const { Foo, Bar } = require('./lib.js');

@ZLY201
Copy link
Member Author

ZLY201 commented Dec 6, 2022

I think there must be some way to transform export default xxx to exports = xxx.

I'll promise the single file like OrderedSet.ts will export only one class member.

Consider this tool: https://www.npmjs.com/package/ts-transform-default-export.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants