-
Notifications
You must be signed in to change notification settings - Fork 80
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
Not working for typescript #37
Comments
@TrySound thoughts? |
As you mentioned @theKashey we are using rollup to generate the bundles. It sounds like there is an issue with the |
This is the old issue with typescript itself and default exports. My solution is getting rid from default exports in the next major release. They are quite useless. I'm not alone. |
@theKashey cjs bundle is matched to esm bundle by the way, it's typescript fault. |
I've added the following function as a workaround which I call at the start of my tests:
|
I would be keen to find a way forward in our generated code. Ideally it would work with TS out of the box. Any thoughts @TrySound? |
This is the best and not only for typescript |
Actually there is |
It should be default since v3 but "TS has such big ecosystem". |
Could you make a PR? 🤘
…On Mon, 22 Oct 2018 at 6:27 pm, Bogdan Chadkin ***@***.***> wrote:
It should be default since v3 but "TS has such big ecosystem".
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#37 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACFN7RU3hwk_4gHF4hKLyNKm9gx3Cr9rks5unXNKgaJpZM4XT298>
.
|
To typescript? I'm not. Users should enable it and fix all modules in DefinitelyTyped. |
Or do you mean named export? |
Please don't use |
@theKashey That's the point. It should be default. Ecosystem is broken now. It's just doesn't work for many packages. |
@TrySound I misunderstood you. I thought we could add a simple rollup plugin and be done with this issue It sounds like you have some insight into this @theKashey, any thoughts on how to proceed? |
Well another option is named only exports. |
The best option I could provide, would be a :badjookeeee: option - rewrite memoize-one to TS, and provide .flow types in a separate, ie "revert" how it works today. |
@theKashey What is the point of esModuleInterop if it doesn't work with any typings at all? What for they provided broken configuration? |
@theKashey It's not the best option. It's a hack for hacky ecosystem. |
Flow is not opinionated and that's why it's so beautiful. |
@theKashey Okay, how such export should look like? Like this |
Technically - Object.defineProperty(exports, "__esModule", {
value: true
});
exports = memoizeOne;
exports.default = memoizeOne; |
Can we add that to the cjs build?
…On Wed, 24 Oct 2018 at 8:39 am, Anton Korzunov ***@***.***> wrote:
Technically -
Object.defineProperty(exports, "__esModule", {
value: true
});
exports = memoizeOne;exports.default = memoizeOne;
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#37 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACFN7bOnyuPoMRFOsLAqgVk_cHClZly_ks5un4yTgaJpZM4XT298>
.
|
That could be a little problem. |
Babel moved from this hack two major versions ago. |
I'm running into this problem as well. Not sure if it's TypeScript per-se or Jest. But the crux of it is that the CJS module needs to include a named export called "default". Pretty much like @theKashey showed. I think that would fix it. |
Just tried. Can't be done. (Seriously I think the only way out is to release a new major version that breaks the way CJS currently works and forces it to use |
This is a problem for me on Typescript, it works
and
but with server side rendering, one will work on nodejs and one on in the browser, bot not both at the same time. So if I'd want to do
which works both server and browser side, then This is the only library of like 50 that behaves this differently between imports. |
esModuleInterop will fix the problem. |
@oonsamyi Why not enable interop on your side? |
When I add Now I turned off // project/fixDefaultImports.ts
jest.mock('memoize-one', () => ({
default: jest.requireActual('memoize-one'),
})); // project/jest.config.js
module.exports = {
setupFilesAfterEnv: ['<rootDir>/enzyme.config.js', '<rootDir>/fixDefaultImports.ts'],
...
} But it is hack... Summary, I would like to work with default typescript config. P.S. React resolves this problem next way: |
This is not an option with typescript. You loose safety without strictNullChecks.
This is inconsistent design. There should be either default or named export.
React does not distribute esm and does not export function as default/module.exports.
This is too, but this is how ts behaves by default. import * as memoizeOne from 'memoize-one'; I'd say babel as compiler will solve all your issues. |
then classnames would be broken :) |
I'm happy to add a named export if it is easier for people |
@alexreardon Can I make a pull request? |
Any news? |
Let's go |
Wasn't it solved in a new version? |
It looks like still there is a problem. When I'm trying to use it like that:
I've got an error:
|
@mits87 Try to enable esModuleInterop in TS config |
Actually I can't use the
and after enabled
or
|
References alexreardon#37 https://github.com/rollup/rollup/blob/master/build-plugins/emit-module-package-file.js https://unpkg.com/nanoevents@5.1.5/package.json Node supports unflagged esm since v13. In this diff I changed default import to memoizeOne named one. ``` import { memoizeOne } from 'memoize-one'; const { memoizeOne } = require('memoize-one'); const { memoizeOne } = window.memoizeOne; ``` The main reason is TS support without fighting with default commonjs/esm interop which forces to import a function as a namespace. ``` import * as memoizeOne from 'memoize-one'; ``` This is incorrect. Module namespace is an object due spec. Libraries like nanoevents and nanoid ended with named export aswell. See details here ai/nanoid#128
Let's kill default import. |
Any news? I have a strange problem. I was using memoize-one without esModuleInterop and it works great with import like this:
App compiles with webpack and ts-loader, jest with ts-jest, typescript version is 3.7.5. |
@tamazlykar I faced this problem with webpack when some commonjs module imported esm version of library. Make sure you do not transpile modules by babel-loader. |
i'm totally open to adding a export const memoizeOne = memoizeOne;
export const memoize = memoizeOne; import { memoizeOne } from 'memoize-one';
// OR
import { memoize } from 'memoize-one'; |
Just to make peoples lives easier ^ |
👍 Plus every time you |
@alexreardon With the revert in v5.2.1 is this an issue again? I am still having the error in jest with ts: |
Actually the problem is not with typescript - the problem is with rollup.
https://unpkg.com/memoize-one@4.0.2/dist/memoize-one.cjs.js contains
which should be imported as
import * as memoizeOne
while https://unpkg.com/memoize-one@4.0.2/dist/memoize-one.esm.js contains
which should be imported as
import memoizeOne
Result - the code for nodejs (~jest) require one behaviour, while the code for webpack(uses esm bundle) requires another.
It is not a problem for js/flow, as long babel is doing some around magic default imports, but typescript is quite strict about it.
The cjs bundle should match esm bundle.
(and fixing this would introduce a breaking change)
The text was updated successfully, but these errors were encountered: