Skip to content

Commit

Permalink
Merge 488bbb1 into 4351987
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Nov 27, 2015
2 parents 4351987 + 488bbb1 commit 281b66f
Show file tree
Hide file tree
Showing 42 changed files with 735 additions and 224 deletions.
4 changes: 1 addition & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ root = true
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.js]
indent_style = space
indent_width = 2
indent_size = 2
end_of_line = lf
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ rules:
curly: [2, "multi-line"]
comma-dangle: [2, always-multiline]
eqeqeq: [2, "allow-null"]
no-shadow: 1
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# files
src/
reports/
resolvers/

# config
.eslintrc
Expand Down
13 changes: 11 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ language: node_js
node_js:
- 0.10
- 0.12
- iojs
- 4
- stable
env:
- NODE_PATH=./lib

script: "npm run-script ci-test"
install:
- npm -g install npm@2
- npm install
# install all resolver deps
- "for resolver in ./resolvers/*; do cd $resolver && npm install && cd ../..; done"

script:
- "npm run-script ci-test"
- "cd resolvers/webpack && npm test"
- "cd ../.." # come back out of resolvers folder

after_success:
- npm run coveralls
5 changes: 5 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ environment:
install:
# Get the latest stable version of Node.js or io.js
- ps: Install-Product node $env:nodejs_version

# update npm
- npm -g install npm@2
- set PATH=%APPDATA%\npm;%PATH%

# install modules
- npm install

Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"ci-test": "npm run-script pretest && mocha --recursive --reporter dot tests/lib/",
"debug": "mocha debug --recursive --reporter dot tests/lib/",
"compile": "rm -rf lib/ && babel -d lib/ src/",
"prepublish": "npm run compile",
"pretest": "eslint ./src && npm run compile && babel -d tests/lib/ tests/src/",
"prepublish": "eslint ./src && npm run compile",
"pretest": "npm run compile && babel -d tests/lib/ tests/src/",
"coveralls": "istanbul cover --report lcovonly --dir reports/coverage _mocha tests/lib/ -- --recursive --reporter dot; remap-istanbul -i reports/coverage/coverage.json -o reports/coverage/lcov.info --type lcovonly && cat ./reports/coverage/lcov.info | coveralls"
},
"repository": {
Expand Down Expand Up @@ -44,15 +44,16 @@
"eslint": ">=1.8.0",
"istanbul": "^0.4.0",
"mocha": "^2.2.1",
"remap-istanbul": "^0.4.0"
"remap-istanbul": "^0.4.0",
"eslint-import-resolver-webpack": "file:./resolvers/webpack",
"eslint-import-resolver-node": "file:./resolvers/node"
},
"peerDependencies": {
"eslint": ">=1.4.0"
},
"dependencies": {
"babel-runtime": "5.x",
"babylon": "^6.1.2",
"resolve": "1.1.6"
"babylon": "^6.1.2"
},
"greenkeeper": {
"ignore": [
Expand Down
3 changes: 3 additions & 0 deletions resolvers/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
env:
es6: false
26 changes: 26 additions & 0 deletions resolvers/node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# eslint-import-resolver-node

[![npm](https://img.shields.io/npm/v/eslint-import-resolver-node.svg)](https://www.npmjs.com/package/eslint-import-resolver-node)

Default Node-style module resolution plugin for [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import).

Published separately to allow pegging to a specific version in case of breaking
changes.

Config is passed directly through to [`resolve`](https://www.npmjs.com/package/resolve) as options:

```yaml
settings:
import/resolver:
node:
moduleDirectory:
- node_modules
- src
```

or to use the default options:

```yaml
settings:
import/resolver: node
```
17 changes: 17 additions & 0 deletions resolvers/node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var resolve = require('resolve')
, path = require('path')
, assign = require('object-assign')

exports.resolveImport = function resolveImport(source, file, config) {
if (resolve.isCore(source)) return null

return resolve.sync(source, opts(path.dirname(file), config))
}

function opts(basedir, config) {
return assign( {}
, config
, { basedir: basedir }
)
}

30 changes: 30 additions & 0 deletions resolvers/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "eslint-import-resolver-node",
"version": "0.0.0",
"description": "Node default behavior import resolution plugin for eslint-plugin-import.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/benmosher/eslint-plugin-import"
},
"keywords": [
"eslint",
"eslintplugin",
"esnext",
"modules",
"eslint-plugin-import"
],
"author": "Ben Mosher (me@benmosher.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/benmosher/eslint-plugin-import/issues"
},
"homepage": "https://github.com/benmosher/eslint-plugin-import",
"dependencies": {
"object-assign": "^4.0.1",
"resolve": "^1.1.6"
}
}
1 change: 1 addition & 0 deletions resolvers/webpack/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "presets": ["es2015"] }
2 changes: 2 additions & 0 deletions resolvers/webpack/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/
.babelrc
27 changes: 27 additions & 0 deletions resolvers/webpack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# eslint-import-resolver-webpack

[![npm](https://img.shields.io/npm/v/eslint-import-resolver-webpack.svg)](https://www.npmjs.com/package/eslint-import-resolver-webpack)

Webpack-literate module resolution plugin for [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import).

Published separately to allow pegging to a specific version in case of breaking
changes.

Will look for `webpack.config.js` as a sibling of the first ancestral `package.json`,
or a `config` parameter may be provided with another filename/path relative to the
`package.json`.

```yaml
---
settings:
import/resolver: webpack # take all defaults
```

or with explicit config file name:

```yaml
---
settings:
import/resolver:
webpack: { config: 'webpack.dev.config.js' }
```
87 changes: 87 additions & 0 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
var findRoot = require('find-root')
, path = require('path')
, resolve = require('resolve')
, get = require('lodash.get')

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

/**
* Find the full path to 'source', given 'file' as a full reference path.
*
* resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js'
* @param {string} source - the module to resolve; i.e './some-module'
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
* TODO: take options as a third param, with webpack config file name
* @return {string?} the resolved path to source, undefined if not resolved, or null
* if resolved to a non-FS resource (i.e. script tag at page load)
*/
exports.resolveImport = function resolveImport(source, file, settings) {

// strip loaders
var finalBang = source.lastIndexOf('!')
if (finalBang >= 0) {
source = source.slice(finalBang + 1)
}

if (resolve.isCore(source)) return null

var webpackConfig
try {
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')))
} catch (err) {
webpackConfig = {}
}

// externals
if (findExternal(source, webpackConfig.externals)) return null

// replace alias if needed
source = resolveAlias(source, get(webpackConfig, ['resolve', 'alias'], {}))

var paths = []

// root as first alternate path
var rootPath = get(webpackConfig, ['resolve', 'root'])
if (rootPath) paths.push(rootPath)

// otherwise, resolve "normally"
return resolve.sync(source, {
basedir: path.dirname(file),

// defined via http://webpack.github.io/docs/configuration.html#resolve-extensions
extensions: get(webpackConfig, ['resolve', 'extensions'])
|| ['', '.webpack.js', '.web.js', '.js'],

// http://webpack.github.io/docs/configuration.html#resolve-modulesdirectories
moduleDirectory: get(webpackConfig, ['resolve', 'modulesDirectories'])
|| ['web_modules', 'node_modules'],

paths: paths,
})
}

function findExternal(source, externals) {
if (!externals) return false

// string match
if (typeof externals === 'string') return (source === externals)

// array: recurse
if (externals instanceof Array) {
return externals.some(function (e) { return findExternal(source, e) })
}

if (externals instanceof RegExp) {
return externals.test(source)
}

if (typeof externals === 'function') {
throw new Error('unable to handle function externals')
}

// else, vanilla object
return Object.keys(externals).some(function (e) { return source === e })
}
37 changes: 37 additions & 0 deletions resolvers/webpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "eslint-import-resolver-webpack",
"version": "0.0.0",
"description": "Resolve paths to dependencies, given a webpack.config.js. Plugin for eslint-plugin-import.",
"main": "index.js",
"scripts": {
"test": "mocha --compilers js:babel-core/register"
},
"repository": {
"type": "git",
"url": "git+https://github.com/benmosher/eslint-plugin-import.git"
},
"keywords": [
"eslint-plugin-import",
"eslint",
"jsnext",
"modules",
"webpack"
],
"author": "Ben Mosher (me@benmosher.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/benmosher/eslint-plugin-import/issues"
},
"homepage": "https://github.com/benmosher/eslint-plugin-import#readme",
"dependencies": {
"find-root": "^0.1.1",
"lodash.get": "^3.7.0",
"resolve": "^1.1.6"
},
"devDependencies": {
"babel-core": "^6.1.21",
"babel-preset-es2015": "^6.1.18",
"chai": "^3.4.1",
"mocha": "^2.3.3"
}
}
36 changes: 36 additions & 0 deletions resolvers/webpack/resolve-alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var path = require('path')

// implements a rough version of
// http://webpack.github.io/docs/configuration.html#resolve-alias
module.exports = function resolveAlias(source, aliases) {

for (var alias in aliases) {
var match = matchAlias(source, alias, aliases[alias])
if (match) return match
}

// fail out
return source
}

function matchAlias(source, alias, value) {
var isExact = (alias[alias.length - 1] === '$')
, isFile = (path.extname(value) !== '')
, segments = source.split(path.sep)

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

if (segments[0] === alias) {
// always return exact match
if (segments.length === 1) return value

// prefix match on exact match for file is an error
if (isFile && (isExact || !/^[./]/.test(value))) {
throw new Error('can\'t match file with exact alias prefix')
}

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

}
5 changes: 5 additions & 0 deletions resolvers/webpack/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
env:
mocha: true
rules:
quotes: 0
Loading

0 comments on commit 281b66f

Please sign in to comment.