Skip to content

A Webpack 2 plugin that treats a file with the name of directory as the index file - it's great for importing React components.

License

Notifications You must be signed in to change notification settings

sebastiandeutsch/component-directory-webpack-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What

Normally, Webpack looks for index file when the path passed to require points to a directory; which means there may have a lot of index files. It's also hard for debugging since the DevTools will report an error in index.

This plugin makes it possible to use the name of the directory as the name of the entry file, makes it easier to find.

Usage

Install the plugin:

npm install component-directory-webpack-plugin

Add the following to Webpack's config file:

  var ComponentDirectoryPlugin = require("component-directory-webpack-plugin");

  resolve: {
    plugins: [new ComponentDirectoryPlugin()]
    extensions: ['', '.js', '.jsx'],
  },

Then when import MyComponent from 'components/MyComponent' and the path "component/MyComponent" is resolved to a directory, Webpack will try to look for component/MyComponent/MyComponent.js as the entry.

If there is also an index file, e.g. index.js, and it should be used as entry file instead of the file with the same name of directory, pass true as the first argument when creating new instance:

  var ComponentDirectoryPlugin = require("component-directory-webpack-plugin");

  resolve: {
    plugins: [new ComponentDirectoryPlugin(true)]
    extensions: ['', '.js', '.jsx'],
  },

Tests

npm test

Build Status

Caveats

This plugin has been developed to work with Webpack 2 for similar plugins that work with Webpack 1 use Directory Named Plugin or Directory name as main Plugin.

Copyright

Copyright (c) 2016 Sebastian Deutsch / 9elements

This plugin is heavily inspired by the Directory Named Plugin which is unfortunately broken for Webpack 2.

About

A Webpack 2 plugin that treats a file with the name of directory as the index file - it's great for importing React components.

Resources

License

Stars

Watchers

Forks

Packages

No packages published