Let me pretend this site supports markdown for this therapy session. Ever had a project with an `index.js` in every directory that looks like this?

import a from './a'
import b from './b'
import z from './z'

export default { a, b, ..., z }

If you do this, please stop.

1. `export default` for an object is not the same as having a named export for each of the property names in that object. In this form, someone has to first `import` the module's default export and then destructure it. This screws up tree-shaking and someone expecting a CommonJS module is bound to try destructuring it up front using a `const {} = require()`, which will blow up too unless you've configured a builder to "promote" default exports for ES6 modules. The way this is set up pleases literally no one.
2. I have to edit a file like this every time I add a new module to a directory. If you desperately want an experience where you can `import` against a directory and use named exports for files, you can configure a builder to generate those files for you. Stop making other people deal with this shit just because you don't like having more `import` statements at the top of your modules.

Bonus for you guys: I saw a team justify reducing the number of `import` lines with these index files because they also have a Java-inspired habit of exporting exactly one `default` symbol and nothing else from each module. Which means there are 30+ modules that each contain exactly one function. Half of the work days was file-hopping, and they didn't like the idea of combining modules with related functionality.

  • 1
    how can i find forgiveness for this sin?

    srsly though, is there an eslint rule for this?
  • 1
    @erandria None for enforcing non-trivial import conventions. However, ESLint can be extended with plugins: https://eslint.org/docs/...
  • 5
    To me this JS stuff seems all so unnecessarily complex ...
    I creep out when I see so many boilerplate and infrastructure stuff
    Im a java dev btw haha😅
Your Job Suck?
Get a Better Job
Add Comment