Skip to content

Instantly share code, notes, and snippets.

@KidkArolis
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KidkArolis/9337b26dd32f1324f91a to your computer and use it in GitHub Desktop.
Save KidkArolis/9337b26dd32f1324f91a to your computer and use it in GitHub Desktop.
ES6 modules suggestion

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";

Questions

  • 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?

Summary

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
@KidkArolis
Copy link
Author

I guess the module syntax should stay :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment