Skip to content

Commit

Permalink
Webpack resolver plugin (#377)
Browse files Browse the repository at this point in the history
* Add webpack.ResolverPlugin support

* Add tests

* Fix tests
  • Loading branch information
Rogeres authored and benmosher committed Jul 14, 2016
1 parent 8adfd2a commit eaf867c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 2 deletions.
16 changes: 14 additions & 2 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,19 @@ exports.resolve = function (source, file, settings) {
// externals
if (findExternal(source, webpackConfig.externals, path.dirname(file))) return { found: true, path: null }

var otherPlugins = []

// support webpack.ResolverPlugin
if (webpackConfig.plugins) {
webpackConfig.plugins.forEach(function (plugin) {
if (plugin.constructor && plugin.constructor.name === 'ResolverPlugin' && Array.isArray(plugin.plugins)) {
otherPlugins.push.apply(otherPlugins, plugin.plugins);
}
});
}

// otherwise, resolve "normally"
var resolver = createResolver(webpackConfig.resolve || {})
var resolver = createResolver(webpackConfig.resolve || {}, otherPlugins)
try {
return { found: true, path: resolver.resolveSync(path.dirname(file), source) }
} catch (err) {
Expand Down Expand Up @@ -116,7 +126,7 @@ var DirectoryDescriptionFileFieldAliasPlugin =

// adapted from tests &
// https://github.com/webpack/webpack/blob/v1.13.0/lib/WebpackOptionsApply.js#L322
function createResolver(resolve) {
function createResolver(resolve, otherPlugins) {
var resolver = new Resolver(syncFS)

resolver.apply(
Expand All @@ -135,6 +145,8 @@ function createResolver(resolve) {
new ResultSymlinkPlugin()
)

resolver.apply.apply(resolver, otherPlugins);

return resolver
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
11 changes: 11 additions & 0 deletions resolvers/webpack/test/files/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
var path = require('path')
var pluginsTest = require('webpack-resolver-plugin-test')

module.exports = {
resolve: {
alias: {
'foo': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'),
'some-alias': path.join(__dirname, 'some')
},
modulesDirectories: ['node_modules', 'bower_components'],
root: path.join(__dirname, 'src'),
Expand All @@ -20,4 +22,13 @@ module.exports = {
callback();
}
],

plugins: [
new pluginsTest.ResolverPlugin([
new pluginsTest.SimpleResolver(
path.join(__dirname, 'some', 'bar', 'bar.js'),
path.join(__dirname, 'some', 'bar')
)
])
]
}
29 changes: 29 additions & 0 deletions resolvers/webpack/test/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var chai = require('chai')
, expect = chai.expect
, path = require('path')

var webpack = require('../index')

var file = path.join(__dirname, 'files', 'dummy.js')

describe("plugins", function () {
var resolved, aliasResolved

before(function () {
resolved = webpack.resolve('./some/bar', file)
aliasResolved = webpack.resolve('some-alias/bar', file)
})

it("work", function () {
expect(resolved).to.have.property('found', true)
})

it("is correct", function () {
expect(resolved).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'some', 'bar', 'bar.js'))
})

it("work with alias", function () {
expect(aliasResolved).to.have.property('found', true)
})
})

0 comments on commit eaf867c

Please sign in to comment.