From 64a74bcde481f4071a1ad52d349488bc5c65430a Mon Sep 17 00:00:00 2001 From: Leonid Nikiforenko Date: Sun, 8 May 2016 15:04:11 +0300 Subject: [PATCH 1/5] Add plugins support for webpack resolver --- resolvers/webpack/index.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index df6f15f030..8090b5788e 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -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 } @@ -116,11 +118,14 @@ exports.resolve = function (source, file, settings) { else paths.push.apply(paths, fallbackPath) } - - // otherwise, resolve "normally" - try { - - return { found: true, path: resolve.sync(source, { + let resolveOptions = { + source, // source, after resolving alias + info: { + originalSource, // original source + rawSource, // source with stripped loader(same as source, if there wasn't one) + webpackConfig, + }, + path: { basedir: path.dirname(file), // defined via http://webpack.github.io/docs/configuration.html#resolve-extensions @@ -133,7 +138,19 @@ exports.resolve = function (source, file, settings) { paths: paths, packageFilter: packageFilter.bind(null, webpackConfig), - }) } + }, + } + + if (Array.isArray(settings.plugins)) { + resolveOptions = settings.plugins.reduce((currentResolveOptions, plugin) => ( + plugin(currentResolveOptions) + ), resolveOptions) + } + + // otherwise, resolve "normally" + try { + + return { found: true, path: resolve.sync(resolveOptions.source, resolveOptions.path) } } catch (err) { return { found: false } } From b64a43d4ee6299e63459edabf8b9768e8f68315a Mon Sep 17 00:00:00 2001 From: Leonid Nikiforenko Date: Sun, 8 May 2016 15:14:22 +0300 Subject: [PATCH 2/5] Allow mutating resolveOptions without returning them --- resolvers/webpack/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index 8090b5788e..e25cca8b95 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -143,7 +143,7 @@ exports.resolve = function (source, file, settings) { if (Array.isArray(settings.plugins)) { resolveOptions = settings.plugins.reduce((currentResolveOptions, plugin) => ( - plugin(currentResolveOptions) + plugin(currentResolveOptions) || currentResolveOptions ), resolveOptions) } From f264ec0382432fc86359c7815afff559f5255af2 Mon Sep 17 00:00:00 2001 From: Leonid Nikiforenko Date: Sun, 8 May 2016 15:36:06 +0300 Subject: [PATCH 3/5] Rework the arguments, passed to plugins --- resolvers/webpack/index.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index e25cca8b95..3ac346c2f7 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -123,21 +123,6 @@ exports.resolve = function (source, file, settings) { info: { originalSource, // original source rawSource, // source with stripped loader(same as source, if there wasn't one) - webpackConfig, - }, - path: { - basedir: path.dirname(file), - - // defined via http://webpack.github.io/docs/configuration.html#resolve-extensions - extensions: get(webpackConfig, ['resolve', 'extensions']) - || ['', '.webpack.js', '.web.js', '.js'], - - // http://webpack.github.io/docs/configuration.html#resolve-modulesdirectories - moduleDirectory: get(webpackConfig, ['resolve', 'modulesDirectories']) - || ['web_modules', 'node_modules'], - - paths: paths, - packageFilter: packageFilter.bind(null, webpackConfig), }, } @@ -150,7 +135,20 @@ exports.resolve = function (source, file, settings) { // otherwise, resolve "normally" try { - return { found: true, path: resolve.sync(resolveOptions.source, resolveOptions.path) } + return { found: true, path: resolve.sync(resolveOptions.source, { + basedir: path.dirname(file), + + // defined via http://webpack.github.io/docs/configuration.html#resolve-extensions + extensions: get(webpackConfig, ['resolve', 'extensions']) + || ['', '.webpack.js', '.web.js', '.js'], + + // http://webpack.github.io/docs/configuration.html#resolve-modulesdirectories + moduleDirectory: get(webpackConfig, ['resolve', 'modulesDirectories']) + || ['web_modules', 'node_modules'], + + paths: paths, + packageFilter: packageFilter.bind(null, webpackConfig), + }) } } catch (err) { return { found: false } } From 3a4f0b23920f62548b38820f0ffcc90b91fd6d4d Mon Sep 17 00:00:00 2001 From: Leonid Nikiforenko Date: Sun, 8 May 2016 16:01:29 +0300 Subject: [PATCH 4/5] Fix failing tests --- resolvers/webpack/index.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index 3ac346c2f7..4f6356a53d 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -118,18 +118,23 @@ exports.resolve = function (source, file, settings) { else paths.push.apply(paths, fallbackPath) } - let resolveOptions = { - source, // source, after resolving alias + var resolveOptions = { + source: source, // source, after resolving alias info: { - originalSource, // original source - rawSource, // source with stripped loader(same as source, if there wasn't one) + originalSource: originalSource, // original source + rawSource: rawSource, // source with stripped loader(same as source, if there wasn't one) }, } - if (Array.isArray(settings.plugins)) { - resolveOptions = settings.plugins.reduce((currentResolveOptions, plugin) => ( - plugin(currentResolveOptions) || currentResolveOptions - ), resolveOptions) + if (Array.isArray(get(settings, 'plugins'))) { + resolveOptions = settings.plugins.reduce(function pluginsReducer(currentResolveOptions, plugin) { + if (typeof plugin !== 'function') { + return currentResolveOptions + } + + return plugin(currentResolveOptions) + || currentResolveOptions // allow user to mutate resolveOptions without returning it + }, resolveOptions) } // otherwise, resolve "normally" @@ -140,11 +145,11 @@ exports.resolve = function (source, file, settings) { // defined via http://webpack.github.io/docs/configuration.html#resolve-extensions extensions: get(webpackConfig, ['resolve', 'extensions']) - || ['', '.webpack.js', '.web.js', '.js'], + || ['', '.webpack.js', '.web.js', '.js'], // http://webpack.github.io/docs/configuration.html#resolve-modulesdirectories moduleDirectory: get(webpackConfig, ['resolve', 'modulesDirectories']) - || ['web_modules', 'node_modules'], + || ['web_modules', 'node_modules'], paths: paths, packageFilter: packageFilter.bind(null, webpackConfig), From 9008f3fc43a6b18fe389b0e3225ea454900023bc Mon Sep 17 00:00:00 2001 From: Leonid Nikiforenko Date: Sun, 8 May 2016 21:39:44 +0300 Subject: [PATCH 5/5] Simplify plugin API --- resolvers/webpack/index.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index 4f6356a53d..41fb809760 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -118,29 +118,35 @@ exports.resolve = function (source, file, settings) { else paths.push.apply(paths, fallbackPath) } - var resolveOptions = { - source: source, // source, after resolving alias - info: { - originalSource: originalSource, // original source - rawSource: rawSource, // source with stripped loader(same as source, if there wasn't one) - }, + 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'))) { - resolveOptions = settings.plugins.reduce(function pluginsReducer(currentResolveOptions, plugin) { + source = settings.plugins.reduce(function pluginsReducer(currentSource, plugin, index) { if (typeof plugin !== 'function') { - return currentResolveOptions + 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 plugin(currentResolveOptions) - || currentResolveOptions // allow user to mutate resolveOptions without returning it - }, resolveOptions) + return currentSource; + }, source) } // otherwise, resolve "normally" try { - return { found: true, path: resolve.sync(resolveOptions.source, { + return { found: true, path: resolve.sync(source, { basedir: path.dirname(file), // defined via http://webpack.github.io/docs/configuration.html#resolve-extensions