Skip to content

Add plugins support for webpack resolver #320

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

Closed
wants to merge 5 commits into from
Closed
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
28 changes: 27 additions & 1 deletion resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ exports.interfaceVersion = 2
* if resolved to a non-FS resource (i.e. script tag at page load)
*/
exports.resolve = function (source, file, settings) {
var originalSource = source
var rawSource = source

// strip loaders
var finalBang = source.lastIndexOf('!')
if (finalBang >= 0) {
source = source.slice(finalBang + 1)
source = rawSource = source.slice(finalBang + 1)
}

if (resolve.isCore(source)) return { found: true, path: null }
Expand Down Expand Up @@ -116,6 +118,30 @@ exports.resolve = function (source, file, settings) {
else paths.push.apply(paths, fallbackPath)
}

const sourceInfo = {
originalSource: originalSource, // original source
rawSource: rawSource, // source with stripped loader(same as source, if there wasn't one)
}

if (Array.isArray(get(settings, 'plugins'))) {
source = settings.plugins.reduce(function pluginsReducer(currentSource, plugin, index) {
if (typeof plugin !== 'function') {
throw new TypeError(
'Expected webpack resolver plugin to be a function. Got' + plugin + '. Plugin index:' + index + '.'
)
}

currentSource = plugin(currentSource, sourceInfo)

if (typeof currentSource !== 'string') {
throw new TypeError(
'Expected webpack resolver plugin to return a string. Got ' + currentSource + '. Plugin index: ', index + '.'
)
}

return currentSource;
}, source)
}

// otherwise, resolve "normally"
try {
Expand Down