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

compile webpack.configs #278

Merged
merged 1 commit into from
Apr 27, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 37 additions & 0 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)) {
Expand All @@ -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 = {}
}
Expand All @@ -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) {
Expand All @@ -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),

Expand Down Expand Up @@ -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
}
}
}
}
}
10 changes: 10 additions & 0 deletions resolvers/webpack/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
})
17 changes: 17 additions & 0 deletions resolvers/webpack/test/files/webpack.config.babel.js
Original file line number Diff line number Diff line change
@@ -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',
],
}