immer icon indicating copy to clipboard operation
immer copied to clipboard

Would you consider not using a default export?

Open scriby opened this issue 8 years ago • 18 comments

The build environment at work doesn't play well with default exports. I'm wondering if you would consider exporting the produce function as a normal function and requiring calling code to use

import {produce} from 'immer';

Thanks for taking a look!

scriby avatar Apr 06 '18 01:04 scriby

Did you try import { default as produce } from ...?

Op vr 6 apr. 2018 03:46 schreef scriby [email protected]:

The build environment at work doesn't play well with default exports. I'm wondering if you would consider exporting the produce function as a normal function and requiring calling code to use

import {produce} from 'immer';

Thanks for taking a look!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mweststrate/immer/issues/136, or mute the thread https://github.com/notifications/unsubscribe-auth/ABvGhChZ7sO0Es25wLItBN0VRvqLRvChks5tlskBgaJpZM4TJZcH .

mweststrate avatar Apr 06 '18 06:04 mweststrate

Thanks for the suggestion!

It's not so much an issue of the import, but just that our build system completely forbids code to use default exports. It's one of those massive company things where you don't have much flexibility.

I don't think there's really anything intrinsically wrong with using a default export, but it does get a little stranger to use once the module also exports other things as well (like setAutoFreeze).

scriby avatar Apr 06 '18 16:04 scriby

@scriby to clarify, do you want to export it both as default and normal function, or only as normal export?

mweststrate avatar May 01 '18 15:05 mweststrate

I think it would work for us if immer used a normal export in addition to the default export.

scriby avatar May 01 '18 16:05 scriby

@scriby feel free to open a PR. Would you be ok that the typings for flow / TS don't support this? I would rather keep this an undocumented feature to avoid confusing by other consumers.

mweststrate avatar May 09 '18 18:05 mweststrate

I've currently got a patch in our source control for both immer and the typings to add produce as an export. It's not too much harder to maintain both of those than just one of them, so I'd probably just keep maintaining both of them if no changes can be made here.

I don't think I follow why adding a regular export would be confusing. In my opinion import produce, {setAutoFreeze, setUseProxies} from 'immer'; is more confusing than import {produce, setAutoFreeze, setUseProxies} from 'immer';. I don't see that allowing it to work either way causes harm.

If you're not convinced feel free to close this one out :)

scriby avatar May 12 '18 06:05 scriby

On node version v9.11.1 using the --experimental-modules flag I attempt the example stating import produce from "immer", as you describe I tried import { default as produce } from "immer" but this results in an object of form { default: [Function: produce], setAutoFreeze: [Function: setAutoFreeze], setUseProxies: [Function: setUseProxies] } and a TypeError: produce is not a function. I am used to imports of the form import { agoody } from "goodies" or import wrappingFunction from "library".

There might be some error in my approach, and by not using a build system.

Rob-pw avatar Jun 06 '18 19:06 Rob-pw

I want to use immer through CDN. I have to write immer.default, it is not clear.

const baseState = [
    {
        todo: "Learn typescript",
        done: true
    },
    {
        todo: "Try immer",
        done: false
    }
]

const nextState = immer.default(baseState, draftState => {
    draftState.push({todo: "Tweet about it"})
    draftState[1].done = true
})

Could we make it better?

magic-akari avatar Jun 08 '18 08:06 magic-akari

@Rob-pw I can't follow your question. Which variant is working and which isn't?

@hufan-akari No, that is how the module system works, and this keeps it consistent. import x from y means const x = require('y').default in commonJS. Just put const produce = immer.default somewhere in the top of your file and use that.

mweststrate avatar Jun 08 '18 08:06 mweststrate

In this case import x from y means const x = require('y').default doesn't seem to hold, otherwise we would not need to declare a const produce = immer.default afterwards, which works but is a tad awkward.

import { produce } from immer would be ideal for me also.

Rob-pw avatar Jun 09 '18 11:06 Rob-pw

I vote for export named function. I prefer to use immer.produce.

magic-akari avatar Jun 10 '18 06:06 magic-akari

I am using Angular 2.4.8 import produce from 'immer' & import * as prodce from 'immer' & import { default as produce } from 'immer' throws compile time error... can any one help me ?

abidev avatar Jul 24 '18 20:07 abidev

@abidev Best set up a minimal example repository. Hard to deduct much from a single line. You could mention for example what compile time error you get.....

mweststrate avatar Jul 24 '18 22:07 mweststrate

@mweststrate Thanks for your time... https://github.com/abidev/immer-test, simple repo with just one ts file, Whenever i import immer... ngc just shouts TypeError: Cannot read property text of undefined just do npm run build after uncommenting any of import style mentioned above

abidev avatar Jul 24 '18 23:07 abidev

produce is now and named export as well and released as 1.4.0

mweststrate avatar Jul 25 '18 11:07 mweststrate

@abidev I think there might something with your local setup, or best clear your node_modules, upgrade node (I used 9.3.0), etc. For me your repository worked just fine:

> [email protected] build /home/michel/Desktop/tmp/immer-test
> ngc -p ./src/tsconfig.json

➜  immer-test git:(master) 

For any follow up comments, please open a new issue, as your question is unrelated to this issue.

mweststrate avatar Jul 25 '18 13:07 mweststrate

@mweststrate Thanks... will try that

abidev avatar Jul 25 '18 14:07 abidev

Just noticed you took care of this. Thanks!

scriby avatar Sep 28 '18 17:09 scriby