Skip to content

Commit

Permalink
Merge PR #137 (closes #136).
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Dec 1, 2015
2 parents a44341b + 0af7486 commit ffe8d39
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install:

# install modules
- npm install
- cd .\resolvers\webpack && npm install && cd ..\..

# Post-install test scripts.
test_script:
Expand All @@ -34,6 +35,7 @@ test_script:
- npm --version
# run tests
- npm run-script ci-test
- cd .\resolvers\webpack && npm test

# Don't actually build.
build: off
17 changes: 13 additions & 4 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var findRoot = require('find-root')
, resolve = require('resolve')
, get = require('lodash.get')
, find = require('array-find')
// not available on 0.10.x
, isAbsolute = path.isAbsolute || require('is-absolute')

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

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

if (resolve.isCore(source)) return null

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

webpackConfig = require(path.join(packageDir, get(settings, 'config', 'webpack.config.js')))
configPath = path.join(packageDir, configPath)
}

webpackConfig = require(configPath)
} catch (err) {
webpackConfig = {}
}
Expand Down
1 change: 1 addition & 0 deletions resolvers/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"array-find": "^1.0.0",
"find-root": "^0.1.1",
"is-absolute": "^0.2.3",
"lodash.get": "^3.7.0",
"resolve": "^1.1.6"
},
Expand Down
6 changes: 4 additions & 2 deletions resolvers/webpack/resolve-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ module.exports = function resolveAlias(source, aliases) {
return source
}

// using '/' only for consistency with Webpack docs
var sep = '/'
function matchAlias(source, alias, value) {
var isExact = (alias[alias.length - 1] === '$')
, isFile = (path.extname(value) !== '')
, segments = source.split(path.sep)
, segments = source.split(sep)

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

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

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

}
22 changes: 22 additions & 0 deletions resolvers/webpack/test/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var chai = require('chai')
, expect = chai.expect

import { resolveImport as resolve } from '../index'

import path from 'path'

var file = path.join(__dirname, 'files', 'src', 'jsx', 'dummy.js')
var absoluteSettings = {
config: path.join(__dirname, 'files', 'some', 'absolute.path.webpack.config.js'),
}

describe("config", function () {
it("finds webpack.config.js in parent directories", function () {
expect(resolve('main-module', file)).to.exist
.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'))
})
it("finds absolute webpack.config.js files", function () {
expect(resolve('foo', file, absoluteSettings)).to.exist
.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'))
})
})
16 changes: 16 additions & 0 deletions resolvers/webpack/test/files/some/absolute.path.webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var path = require('path')

module.exports = {
resolve: {
alias: {
'foo': path.join(__dirname, 'absolutely', 'goofy', 'path', 'foo.js'),
},
modulesDirectories: ['node_modules', 'bower_components'],
root: path.join(__dirname, 'src'),
},

externals: [
{ 'jquery': 'jQuery' },
'bootstrap',
],
}
Empty file.

0 comments on commit ffe8d39

Please sign in to comment.