1

I like to keep my files in organized folders in my project. For example, having an Auth folder for auth-related components, a Components folder for reusable components, a Profile folder for user profile-related components, etc.

Rather than having to either alias all of my folders whenever I create a new one, or have to reference the complete file path for all of my imports, I'd like to be able to declare something as simply as:

import MyComponent from 'MyComponent';

I imagine this can be done with some sort of pre-compile command to flatten the src directory structure into a new folder that just contains all of my files, but I don't know how to do that (or if there's a better method for doing so).

I'm also aware that this will likely mean my filenames need to be unique, which is not a problem for me.

Mike K
  • 11
  • 1

1 Answers1

2

You can use Webpack's resolve.alias. https://webpack.js.org/configuration/resolve/

Create aliases to import or require certain modules more easily.

Example:

// webpack.config.js (or whatever)
module.exports = {
  //...
  resolve: {
    alias: {
      Utilities: path.resolve(__dirname, 'src/utilities/'),
      Templates: path.resolve(__dirname, 'src/templates/')
    }
  }
};

Now you could type e.g. import Foo from 'Utilities/Foo' and it would be as if you had typed the full path name of the module (src/utilities/Foo)

jered
  • 11,220
  • 2
  • 23
  • 34