Skip to content

Commit 74c2e57

Browse files
committed
compile webpack.configs
1 parent ea1b0e3 commit 74c2e57

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"es6-set": "^0.1.4",
7272
"es6-symbol": "*",
7373
"eslint-import-resolver-node": "^0.2.0",
74+
"interpret": "^1.0.0",
7475
"lodash.cond": "^4.3.0",
7576
"lodash.endswith": "^4.0.1",
7677
"lodash.find": "^4.3.0",

resolvers/webpack/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var findRoot = require('find-root')
33
, resolve = require('resolve')
44
, get = require('lodash.get')
55
, find = require('array-find')
6+
, interpret = require('interpret')
67
// not available on 0.10.x
78
, isAbsolute = path.isAbsolute || require('is-absolute')
89

@@ -32,6 +33,7 @@ exports.resolve = function (source, file, settings) {
3233

3334
var configPath = get(settings, 'config', 'webpack.config.js')
3435
, webpackConfig
36+
3537
try {
3638
// see if we've got an absolute path
3739
if (!isAbsolute(configPath)) {
@@ -42,7 +44,19 @@ exports.resolve = function (source, file, settings) {
4244
configPath = path.join(packageDir, configPath)
4345
}
4446

47+
var ext = Object.keys(interpret.extensions).reduce(function (chosen, extension) {
48+
var extlen = extension.length
49+
return ((configPath.substr(-extlen) === extension) && (extlen > chosen.length))
50+
? extension : chosen
51+
}, '')
52+
53+
registerCompiler(interpret.extensions[ext])
54+
4555
webpackConfig = require(configPath)
56+
57+
if (webpackConfig && webpackConfig.default) {
58+
webpackConfig = webpackConfig.default
59+
}
4660
} catch (err) {
4761
webpackConfig = {}
4862
}
@@ -57,10 +71,12 @@ exports.resolve = function (source, file, settings) {
5771

5872
// root as first alternate path
5973
var rootPath = get(webpackConfig, ['resolve', 'root'])
74+
6075
if (rootPath) {
6176
if (typeof rootPath === 'string') paths.push(rootPath)
6277
else paths.push.apply(paths, rootPath)
6378
}
79+
6480
// set fallback paths
6581
var fallbackPath = get(webpackConfig, ['resolve', 'fallback'])
6682
if (fallbackPath) {
@@ -71,6 +87,7 @@ exports.resolve = function (source, file, settings) {
7187

7288
// otherwise, resolve "normally"
7389
try {
90+
7491
return { found: true, path: resolve.sync(source, {
7592
basedir: path.dirname(file),
7693

@@ -145,3 +162,23 @@ function packageFilter(config, pkg) {
145162

146163
return pkg
147164
}
165+
166+
167+
function registerCompiler(moduleDescriptor) {
168+
if(moduleDescriptor) {
169+
if(typeof moduleDescriptor === 'string') {
170+
require(moduleDescriptor)
171+
} else if(!Array.isArray(moduleDescriptor)) {
172+
moduleDescriptor.register(require(moduleDescriptor.module))
173+
} else {
174+
for(var i = 0; i < moduleDescriptor.length; i++) {
175+
try {
176+
registerCompiler(moduleDescriptor[i])
177+
break
178+
} catch(e) {
179+
// do nothing
180+
}
181+
}
182+
}
183+
}
184+
}

resolvers/webpack/test/config.js

+10
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ describe("config", function () {
1818
expect(resolve('foo', file, absoluteSettings)).to.have.property('path')
1919
.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'))
2020
})
21+
22+
it("finds compile-to-js configs", function () {
23+
var settings = {
24+
config: path.join(__dirname, './files/webpack.config.babel.js'),
25+
}
26+
27+
expect(resolve('main-module', file, settings))
28+
.to.have.property('path')
29+
.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'))
30+
})
2131
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import path from 'path'
2+
3+
export default {
4+
resolve: {
5+
alias: {
6+
'foo': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'),
7+
},
8+
modulesDirectories: ['node_modules', 'bower_components'],
9+
root: path.join(__dirname, 'src'),
10+
fallback: path.join(__dirname, 'fallback'),
11+
},
12+
13+
externals: [
14+
{ 'jquery': 'jQuery' },
15+
'bootstrap',
16+
],
17+
}

0 commit comments

Comments
 (0)