From 68ce4ff6eb6f1bd9e456d2828d01c4afc0eb50da Mon Sep 17 00:00:00 2001 From: MopTym Date: Thu, 8 Sep 2016 12:10:00 +0800 Subject: [PATCH] support webpack2 --- README.md | 21 ++++++++++++++ lib/index-file-webpack-plugin.js | 28 +++++++++++++------ package.json | 3 +- test/fixtures/same-as-dirname/a/a.js | 0 .../same-as-dirname/same-as-dirname.js | 1 + 5 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/same-as-dirname/a/a.js diff --git a/README.md b/README.md index 7ba5cba..87ce8ac 100644 --- a/README.md +++ b/README.md @@ -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' + ]) + ] + } +} +``` diff --git a/lib/index-file-webpack-plugin.js b/lib/index-file-webpack-plugin.js index b46b351..45cc4ba 100644 --- a/lib/index-file-webpack-plugin.js +++ b/lib/index-file-webpack-plugin.js @@ -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) : [] @@ -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() @@ -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 () { @@ -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) + } }) } diff --git a/package.json b/package.json index 3ff7a8e..6e8c4c6 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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": { diff --git a/test/fixtures/same-as-dirname/a/a.js b/test/fixtures/same-as-dirname/a/a.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/same-as-dirname/same-as-dirname.js b/test/fixtures/same-as-dirname/same-as-dirname.js index e69de29..625b552 100644 --- a/test/fixtures/same-as-dirname/same-as-dirname.js +++ b/test/fixtures/same-as-dirname/same-as-dirname.js @@ -0,0 +1 @@ +require('./a')