/component-directory-webpack-plugin

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

Primary LanguageJavaScriptMIT LicenseMIT

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.