Would you consider not using a default export?
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!
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 .
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 to clarify, do you want to export it both as default and normal function, or only as normal export?
I think it would work for us if immer used a normal export in addition to the default export.
@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.
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 :)
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.
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?
@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.
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.
I vote for export named function.
I prefer to use immer.produce.
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 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 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
produce is now and named export as well and released as 1.4.0
@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 Thanks... will try that
Just noticed you took care of this. Thanks!