Only allow using default export or named exports, but not both within the same module. That way we can:
// import the default export
import marked from "marked";
import moment from "moment";
// import all of the named exports
import async from "async";
import fs from "fs";
// or some named exports
import {parallel, series} from "async";
// if something like underscore uses named exports you consume it with
import _ from "lodash";
// but if it uses the default export and exports an object, you consume it the same way
import _ from "lodash";
// not sure which option and why would the module authors/consumers will prefer
There's one more interesting pattern I've seen commonly used that is currently not possible with the named exports and that is having a default export be a function, but also have other functions attached to it, e.g.
import request from "request";
request.post(request(...));
import when from "when";
when.map([when(1), when(2)]);
For that to still work the only choice is using the default exports and using it as we use CJS today. NOTE, that allowing both default exports and named exports doesn't completely solve the problem
// awkward
import when from "when";
module whenModule from "when";
whenModule.map([when(1), when(2)])
// not exactly the same either
import when from "when";
import {all, map} from "when";
- what's the incentive for module authors/consumers to choose named exports over exporting a default object?
- what other use cases are there for having both named and default exports in the same module?
Only allowing either default or named exports, but not both:
- reduces the number of possible import/export combinations
- intuitively reuses the same syntax (
import
keyword) for modules using named exports and those using the default exports
I guess the
module
syntax should stay :)