From 74c2e57537b5801ba17be407db9f4e6c5e03602d Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Tue, 26 Apr 2016 17:16:15 -0400 Subject: [PATCH] compile webpack.configs --- package.json | 1 + resolvers/webpack/index.js | 37 +++++++++++++++++++ resolvers/webpack/test/config.js | 10 +++++ .../test/files/webpack.config.babel.js | 17 +++++++++ 4 files changed, 65 insertions(+) create mode 100644 resolvers/webpack/test/files/webpack.config.babel.js diff --git a/package.json b/package.json index 9132f7583..32316e611 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "es6-set": "^0.1.4", "es6-symbol": "*", "eslint-import-resolver-node": "^0.2.0", + "interpret": "^1.0.0", "lodash.cond": "^4.3.0", "lodash.endswith": "^4.0.1", "lodash.find": "^4.3.0", diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index a6af00baa..a8533d5cf 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -3,6 +3,7 @@ var findRoot = require('find-root') , resolve = require('resolve') , get = require('lodash.get') , find = require('array-find') + , interpret = require('interpret') // not available on 0.10.x , isAbsolute = path.isAbsolute || require('is-absolute') @@ -32,6 +33,7 @@ exports.resolve = function (source, file, settings) { var configPath = get(settings, 'config', 'webpack.config.js') , webpackConfig + try { // see if we've got an absolute path if (!isAbsolute(configPath)) { @@ -42,7 +44,19 @@ exports.resolve = function (source, file, settings) { configPath = path.join(packageDir, configPath) } + var ext = Object.keys(interpret.extensions).reduce(function (chosen, extension) { + var extlen = extension.length + return ((configPath.substr(-extlen) === extension) && (extlen > chosen.length)) + ? extension : chosen + }, '') + + registerCompiler(interpret.extensions[ext]) + webpackConfig = require(configPath) + + if (webpackConfig && webpackConfig.default) { + webpackConfig = webpackConfig.default + } } catch (err) { webpackConfig = {} } @@ -57,10 +71,12 @@ exports.resolve = function (source, file, settings) { // root as first alternate path var rootPath = get(webpackConfig, ['resolve', 'root']) + if (rootPath) { if (typeof rootPath === 'string') paths.push(rootPath) else paths.push.apply(paths, rootPath) } + // set fallback paths var fallbackPath = get(webpackConfig, ['resolve', 'fallback']) if (fallbackPath) { @@ -71,6 +87,7 @@ exports.resolve = function (source, file, settings) { // otherwise, resolve "normally" try { + return { found: true, path: resolve.sync(source, { basedir: path.dirname(file), @@ -145,3 +162,23 @@ function packageFilter(config, pkg) { return pkg } + + +function registerCompiler(moduleDescriptor) { + if(moduleDescriptor) { + if(typeof moduleDescriptor === 'string') { + require(moduleDescriptor) + } else if(!Array.isArray(moduleDescriptor)) { + moduleDescriptor.register(require(moduleDescriptor.module)) + } else { + for(var i = 0; i < moduleDescriptor.length; i++) { + try { + registerCompiler(moduleDescriptor[i]) + break + } catch(e) { + // do nothing + } + } + } + } +} diff --git a/resolvers/webpack/test/config.js b/resolvers/webpack/test/config.js index 5c36071ad..5f9dcf848 100644 --- a/resolvers/webpack/test/config.js +++ b/resolvers/webpack/test/config.js @@ -18,4 +18,14 @@ describe("config", function () { expect(resolve('foo', file, absoluteSettings)).to.have.property('path') .and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js')) }) + + it("finds compile-to-js configs", function () { + var settings = { + config: path.join(__dirname, './files/webpack.config.babel.js'), + } + + expect(resolve('main-module', file, settings)) + .to.have.property('path') + .and.equal(path.join(__dirname, 'files', 'src', 'main-module.js')) + }) }) diff --git a/resolvers/webpack/test/files/webpack.config.babel.js b/resolvers/webpack/test/files/webpack.config.babel.js new file mode 100644 index 000000000..dfe585a67 --- /dev/null +++ b/resolvers/webpack/test/files/webpack.config.babel.js @@ -0,0 +1,17 @@ +import path from 'path' + +export default { + resolve: { + alias: { + 'foo': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'), + }, + modulesDirectories: ['node_modules', 'bower_components'], + root: path.join(__dirname, 'src'), + fallback: path.join(__dirname, 'fallback'), + }, + + externals: [ + { 'jquery': 'jQuery' }, + 'bootstrap', + ], +}