npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-auto-export

v0.1.0

Published

A webpack plugin to automatically export all modules from within a given path.

Downloads

41

Readme

webpack-auto-export

A webpack plugin to automatically export all modules from within a given path.

Understanding the problem

It's very common, in a react environment, to, for instance, have a /components folder to hold all our shared components. We then go on to import all the necessary components in our /pages or /containers definitions.

import Button from '../../components/Button'
import Form from '../../components/Form'
import Icon from '../../components/Icon'

If, however, we create an index file inside /components folder, we can export all of the definitions from the same path.

// components/index.js
export { default as Button } from './Button'
export { default as Form } from './Form'
export { default as Icon } from './Icon'

This allows us to makes our imports a lot cleaner, as shown next:

// page / container definition:
import { Button, Form, Icon } from '../../components'

The downside

As you can probably already imagine, despite the simplified imports, this comes with a necessity for the developer to maintain all of the exports manually.

Say you create a new component. If you don't export it from components/index file, then, your pages definition can look like the following:

import { Button, Form, Icon } from '../../components'
import Table from '../../components/Table'

So, if any of your components are not exported from the index file, it defeats the purpose of exporting any component from there.

And, as developers, we might not be really interested in maintaining bureocracy in our code, and we force ourselvs to do so simply to keep the consistency.

The solution

webpack-auto-export is a webpack plugin that allows you to automatically export your modules from any specified folder.

With an extremelly simple configuration, you can be up and running within seconds.

// webpack.config.js
const AutoExport = require('webpack-auto-export')

module.exports = {
  ...
  plugins: [
    ...
    new AutoExport({
      extension: '.js', // define extension of generated index file
      exportType: 'named', // the default way to export. values can be: 'named' | 'default' | 'detect'
      baseDir: './src', // base directory to observe the changes
      paths: [ // the folders to be automatically exported
        'hooks', // hooks folder will use default exportType
        { path: 'components', exportType: 'detect' }, // we can also specify the export type for any given path.
      ],
    })
  ]
}

One great feature of the plugin is that it allows for use to choose the exportType. The values can be:

named - export as named export (export * from './PATH') • default - export as default export (export { default as Module } from './PATH') • detect - Detect should be avoided, for performance issues. It will read the files and detect whether or not they contain an export default. If so, it uses default export, otherwise, it'll use named export.