Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack resolver plugin #377

Merged
merged 3 commits into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) 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)
})
})