Skip to content

Commit ffe8d39

Browse files
committed
Merge PR #137 (closes #136).
2 parents a44341b + 0af7486 commit ffe8d39

File tree

7 files changed

+58
-6
lines changed

7 files changed

+58
-6
lines changed

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ install:
2626

2727
# install modules
2828
- npm install
29+
- cd .\resolvers\webpack && npm install && cd ..\..
2930

3031
# Post-install test scripts.
3132
test_script:
@@ -34,6 +35,7 @@ test_script:
3435
- npm --version
3536
# run tests
3637
- npm run-script ci-test
38+
- cd .\resolvers\webpack && npm test
3739

3840
# Don't actually build.
3941
build: off

resolvers/webpack/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var findRoot = require('find-root')
33
, resolve = require('resolve')
44
, get = require('lodash.get')
55
, find = require('array-find')
6+
// not available on 0.10.x
7+
, isAbsolute = path.isAbsolute || require('is-absolute')
68

79
var resolveAlias = require('./resolve-alias')
810

@@ -26,12 +28,19 @@ exports.resolveImport = function resolveImport(source, file, settings) {
2628

2729
if (resolve.isCore(source)) return null
2830

29-
var webpackConfig
31+
var configPath = get(settings, 'config', 'webpack.config.js')
32+
, webpackConfig
3033
try {
31-
var packageDir = findRoot(file)
32-
if (!packageDir) throw new Error('package not found above ' + file)
34+
// see if we've got an absolute path
35+
if (!isAbsolute(configPath)) {
36+
// if not, find ancestral package.json and use its directory as base for the path
37+
var packageDir = findRoot(file)
38+
if (!packageDir) throw new Error('package not found above ' + file)
3339

34-
webpackConfig = require(path.join(packageDir, get(settings, 'config', 'webpack.config.js')))
40+
configPath = path.join(packageDir, configPath)
41+
}
42+
43+
webpackConfig = require(configPath)
3544
} catch (err) {
3645
webpackConfig = {}
3746
}

resolvers/webpack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"array-find": "^1.0.0",
2828
"find-root": "^0.1.1",
29+
"is-absolute": "^0.2.3",
2930
"lodash.get": "^3.7.0",
3031
"resolve": "^1.1.6"
3132
},

resolvers/webpack/resolve-alias.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ module.exports = function resolveAlias(source, aliases) {
1313
return source
1414
}
1515

16+
// using '/' only for consistency with Webpack docs
17+
var sep = '/'
1618
function matchAlias(source, alias, value) {
1719
var isExact = (alias[alias.length - 1] === '$')
1820
, isFile = (path.extname(value) !== '')
19-
, segments = source.split(path.sep)
21+
, segments = source.split(sep)
2022

2123
if (isExact) alias = alias.slice(0, -1)
2224

@@ -30,7 +32,7 @@ function matchAlias(source, alias, value) {
3032
}
3133

3234
// otherwise, prefix match is fine for non-file paths
33-
if (!isExact && !isFile) return [value].concat(segments.slice(1)).join(path.sep)
35+
if (!isExact && !isFile) return [value].concat(segments.slice(1)).join(sep)
3436
}
3537

3638
}

resolvers/webpack/test/config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var chai = require('chai')
2+
, expect = chai.expect
3+
4+
import { resolveImport as resolve } from '../index'
5+
6+
import path from 'path'
7+
8+
var file = path.join(__dirname, 'files', 'src', 'jsx', 'dummy.js')
9+
var absoluteSettings = {
10+
config: path.join(__dirname, 'files', 'some', 'absolute.path.webpack.config.js'),
11+
}
12+
13+
describe("config", function () {
14+
it("finds webpack.config.js in parent directories", function () {
15+
expect(resolve('main-module', file)).to.exist
16+
.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'))
17+
})
18+
it("finds absolute webpack.config.js files", function () {
19+
expect(resolve('foo', file, absoluteSettings)).to.exist
20+
.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'))
21+
})
22+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var path = require('path')
2+
3+
module.exports = {
4+
resolve: {
5+
alias: {
6+
'foo': path.join(__dirname, 'absolutely', 'goofy', 'path', 'foo.js'),
7+
},
8+
modulesDirectories: ['node_modules', 'bower_components'],
9+
root: path.join(__dirname, 'src'),
10+
},
11+
12+
externals: [
13+
{ 'jquery': 'jQuery' },
14+
'bootstrap',
15+
],
16+
}

resolvers/webpack/test/files/some/absolutely/goofy/path/foo.js

Whitespace-only changes.

0 commit comments

Comments
 (0)