├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── fixtures ├── MyAwesomeComponent │ └── MyAwesomeComponent.js ├── RandomDirectory │ └── index.js └── UglyEdgeCase │ ├── UglyEdgeCase.js │ └── index.js └── plugin.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /coverage 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "5.0" 5 | - node 6 | script: npm run travis 7 | 8 | after_success: 9 | - cat ./coverage/lcov.info | node_modules/.bin/coveralls --verbose 10 | - cat ./coverage/coverage.json | node_modules/codecov.io/bin/codecov.io.js 11 | - rm -rf ./coverage 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Sebastian Deutsch 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What 2 | 3 | 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**. 4 | 5 | This plugin makes it possible to use the name of the directory as the name of the entry file, makes it easier to find. 6 | 7 | ## Usage 8 | 9 | Install the plugin: 10 | 11 | ``` 12 | npm install component-directory-webpack-plugin 13 | ``` 14 | 15 | Add the following to Webpack's config file: 16 | 17 | ```javascript 18 | var ComponentDirectoryPlugin = require("component-directory-webpack-plugin"); 19 | 20 | resolve: { 21 | plugins: [new ComponentDirectoryPlugin()] 22 | extensions: ['', '.js', '.jsx'], 23 | }, 24 | 25 | ``` 26 | 27 | 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. 28 | 29 | 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: 30 | 31 | ```javascript 32 | var ComponentDirectoryPlugin = require("component-directory-webpack-plugin"); 33 | 34 | resolve: { 35 | plugins: [new ComponentDirectoryPlugin(true)] 36 | extensions: ['', '.js', '.jsx'], 37 | }, 38 | 39 | ``` 40 | ## Tests 41 | 42 | ``` javascript 43 | npm test 44 | ``` 45 | 46 | [![Build Status](https://secure.travis-ci.org/sebastiandeutsch/component-directory-webpack-plugin.png?branch=master)](http://travis-ci.org/sebastiandeutsch/component-directory-webpack-plugin) 47 | 48 | ## Caveats 49 | 50 | This plugin has been developed to work with Webpack 2 for similar plugins that work with Webpack 1 use [Directory Named Plugin](https://github.com/shaketbaby/directory-named-webpack-plugin) or [Directory name as main Plugin](https://github.com/spalger/webpack-directory-name-as-main). 51 | 52 | ## Copyright 53 | 54 | Copyright (c) 2016 [Sebastian Deutsch](https://twitter.com/sippndipp) / [9elements](http://9elements.com/) 55 | 56 | This plugin is heavily inspired by the [Directory Named Plugin](https://github.com/shaketbaby/directory-named-webpack-plugin) which is unfortunately broken for Webpack 2. 57 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require("path"); 2 | var forEachBail = require("enhanced-resolve/lib/forEachBail"); 3 | var createInnerCallback = require("enhanced-resolve/lib/createInnerCallback"); 4 | var basename = require("enhanced-resolve/lib/getPaths").basename; 5 | 6 | var assign = require("object-assign"); 7 | 8 | module.exports = ComponentDirectoryPlugin; 9 | 10 | function ComponentDirectoryPlugin(honorIndexFile) { 11 | if(honorIndexFile === undefined) { 12 | this.honorIndexFile = false; 13 | } else { 14 | this.honorIndexFile = honorIndexFile; 15 | } 16 | } 17 | 18 | ComponentDirectoryPlugin.prototype.apply = function (resolver) { 19 | var honorIndexFile = this.honorIndexFile; 20 | resolver.plugin("directory", function(request, callback) { 21 | var fs = resolver.fileSystem; 22 | var topLevelCallback = callback; 23 | var filename = basename(request.path); 24 | 25 | var filePath = resolver.join(request.path, filename); 26 | 27 | forEachBail( 28 | honorIndexFile ? [resolver.join(request.path, "index"), filePath] : [filePath], 29 | function(file) { 30 | var obj = assign({}, request, { 31 | path: file, 32 | relativePath: request.relativePath && resolver.join(request.relativePath, filename) 33 | }); 34 | resolver.doResolve("raw-file", obj, "using path: " + filePath, callback); 35 | }, 36 | function(result) { 37 | return result ? callback(null, result) : callback(); 38 | } 39 | ); 40 | }); 41 | }; 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "component-directory-webpack-plugin", 3 | "version": "1.0.5", 4 | "description": "A Webpack plugin that treats a file with the name of directory as the index file - it's great for importing React components.", 5 | "main": "index.js", 6 | "scripts": { 7 | "push": "git push --tag origin master:master", 8 | "bump": "bump --prompt --commit --tag --push --all", 9 | "lint": "eslint lib test", 10 | "test": "mocha --full-trace --check-leaks", 11 | "cover": "istanbul cover node_modules/mocha/bin/_mocha", 12 | "travis": "npm run cover -- --report lcovonly" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/sebastiandeutsch/component-directory-webpack-plugin.git" 17 | }, 18 | "keywords": [ 19 | "webpack", 20 | "react", 21 | "plugin", 22 | "directory", 23 | "named", 24 | "component", 25 | "import" 26 | ], 27 | "author": "Sebastian Deutsch", 28 | "license": "MIT", 29 | "bugs": { 30 | "url": "https://github.com/sebastiandeutsch/component-directory-webpack-plugin/issues" 31 | }, 32 | "homepage": "https://github.com/sebastiandeutsch/component-directory-webpack-plugin#readme", 33 | "dependencies": { 34 | "enhanced-resolve": "^2.2.2", 35 | "object-assign": "^4.1.0" 36 | }, 37 | "devDependencies": { 38 | "eslint": "^1.1.0", 39 | "istanbul": "^0.4.4", 40 | "mocha": "^2.3.4", 41 | "should": "^8.0.2", 42 | "version-bump-prompt": "^1.5.2" 43 | }, 44 | "peerDependencies": { 45 | "webpack": "^2.1.0-beta.x" 46 | } 47 | } -------------------------------------------------------------------------------- /test/fixtures/MyAwesomeComponent/MyAwesomeComponent.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | console.log("MyAwesomeComponent"); 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/RandomDirectory/index.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | console.log("RandomDirectory"); 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/UglyEdgeCase/UglyEdgeCase.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | console.log("UglyEdgeCase.js"); 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/UglyEdgeCase/index.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | console.log("index.js"); 3 | } 4 | -------------------------------------------------------------------------------- /test/plugin.js: -------------------------------------------------------------------------------- 1 | var ResolverFactory = require("enhanced-resolve/lib/ResolverFactory"); 2 | var ComponentDirectoryPlugin = require("../index"); 3 | var should = require("should"); 4 | var path = require("path"); 5 | 6 | describe("plugins", function() { 7 | it("should resolve with the ComponentDirectoryPlugin", function(done) { 8 | var resolver = ResolverFactory.createResolver({ 9 | fileSystem: require("fs"), 10 | plugins: [ 11 | new ComponentDirectoryPlugin() 12 | ] 13 | }); 14 | 15 | resolver.resolve({}, __dirname, "./fixtures/MyAwesomeComponent", function(err, result) { 16 | if(err) return done(err); 17 | result.should.be.eql(path.resolve(__dirname, "fixtures/MyAwesomeComponent/MyAwesomeComponent.js")); 18 | done(); 19 | }); 20 | }); 21 | 22 | it("should resolve with the ComponentDirectoryPlugin but use index as default case", function(done) { 23 | var resolver = ResolverFactory.createResolver({ 24 | fileSystem: require("fs"), 25 | plugins: [ 26 | new ComponentDirectoryPlugin() 27 | ] 28 | }); 29 | 30 | resolver.resolve({}, __dirname, "./fixtures/RandomDirectory", function(err, result) { 31 | if(err) return done(err); 32 | result.should.be.eql(path.resolve(__dirname, "fixtures/RandomDirectory/index.js")); 33 | done(); 34 | }); 35 | }); 36 | 37 | it("should resolve with the ComponentDirectoryPlugin but don't honor the index", function(done) { 38 | var resolver = ResolverFactory.createResolver({ 39 | fileSystem: require("fs"), 40 | plugins: [ 41 | new ComponentDirectoryPlugin(false) 42 | ] 43 | }); 44 | 45 | resolver.resolve({}, __dirname, "./fixtures/UglyEdgeCase", function(err, result) { 46 | if(err) return done(err); 47 | result.should.be.eql(path.resolve(__dirname, "fixtures/UglyEdgeCase/UglyEdgeCase.js")); 48 | done(); 49 | }); 50 | }); 51 | 52 | it("should resolve with the ComponentDirectoryPlugin but honor the index", function(done) { 53 | var resolver = ResolverFactory.createResolver({ 54 | fileSystem: require("fs"), 55 | plugins: [ 56 | new ComponentDirectoryPlugin(true) 57 | ] 58 | }); 59 | 60 | resolver.resolve({}, __dirname, "./fixtures/UglyEdgeCase", function(err, result) { 61 | if(err) return done(err); 62 | result.should.be.eql(path.resolve(__dirname, "fixtures/UglyEdgeCase/index.js")); 63 | done(); 64 | }); 65 | }); 66 | }); 67 | --------------------------------------------------------------------------------