Skip to content

Commit

Permalink
support webpack2
Browse files Browse the repository at this point in the history
  • Loading branch information
MopTym committed Sep 8, 2016
1 parent 91d28d3 commit 68ce4ff
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@ var webpack = require('webpack')
]
}
```

## webpack2 config

```js
var IndexFilePlugin = require('index-file-webpack-plugin')
var webpack = require('webpack')
```

```js
{
resolve: {
plugins: [
new IndexFilePlugin([
'[name].js', // '[name]' is directory name.
'main.js',
'index.js'
])
]
}
}
```
28 changes: 19 additions & 9 deletions lib/index-file-webpack-plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var path = require('path')
var Promise = require('promise')
var assign = require("object-assign")

function IndexFilePlugin (fileNameList) {
this.fileNameList = fileNameList ? getArray(fileNameList) : []
Expand All @@ -9,7 +10,10 @@ IndexFilePlugin.prototype.apply = function (resolver) {
var fileNameList = this.fileNameList

resolver.plugin('directory', function (req, done) {
var directory = resolver.join(req.path, req.request)
var isWebpack2 = !!req.descriptionFilePath
var directory = isWebpack2
? req.path
: resolver.join(req.path, req.request)

resolver.fileSystem.stat(directory, function (err, stat) {
if (err || !stat) return done()
Expand All @@ -20,7 +24,7 @@ IndexFilePlugin.prototype.apply = function (resolver) {

requestList.forEach(function (request) {
chain = chain.then(function () {
return doResolve(request, resolver, req)
return doResolve(request, resolver, req, isWebpack2)
})
})
chain.then(function () {
Expand All @@ -39,19 +43,25 @@ function getRequestList (directory, fileNameList) {
})
}

function doResolve (request, resolver, req) {
function doResolve (request, resolver, req, isWebpack2) {
return new Promise(function (resolve, reject) {
resolver.doResolve('file', {
path: req.path,
query: req.query,
request: request
}, function (err, result) {
var cb = function (err, result) {
if (result) {
reject(result)
} else {
resolve()
}
})
}
if (isWebpack2) {
resolver.doResolve('raw-file', assign({}, req, {
path: request,
relativePath: req.relativePath && resolver.join(req.relativePath, path.basename(request))
}), null, cb)
} else {
resolver.doResolve('file', assign({}, req, {
request: request
}), cb)
}
})
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "index-file-webpack-plugin",
"version": "0.1.1",
"version": "0.2.2",
"description": "A webpack resolver plugin to specify directory index file.",
"main": "index.js",
"scripts": {
Expand All @@ -22,6 +22,7 @@
},
"homepage": "https://github.com/WEBuster/index-file-webpack-plugin#readme",
"dependencies": {
"object-assign": "^4.1.0",
"promise": "^7.1.1"
},
"devDependencies": {
Expand Down
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/same-as-dirname/same-as-dirname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./a')

0 comments on commit 68ce4ff

Please sign in to comment.