Skip to content

Commit

Permalink
Merge branch 'update_core'
Browse files Browse the repository at this point in the history
  • Loading branch information
johvin committed Apr 25, 2018
2 parents 86360b9 + 1d66d6d commit fc1a38c
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ sudo: false
language: node_js
node_js:
- "4"
- "5"
- "6"
- "8"
- "node"
script:
- "npm run cov"
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,38 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- add builtin modules resolve test case and relevant dependencies

### Fixed
- fix the bug that inner function `resolveLookupPaths` resolve `/node_modules` as `//node_modules`
- update core modules for Node.js 8/9/...

### Changed
- add peerDependencies, repository, bugs, homepage config items to package.json
- update readme
- update changelog
- update travis config
- add release script to complete preparation before publish


## [1.0.3] - 2017-01-12
### Fixed
- fix the bug that the path like /path/to/node_modules/node_modules is excluded from the lookup paths


## [1.0.2] - 2017-01-05
### Changed
- fix the bug that it's unable to resolve non-relative path modules when current working directory is subdirectory of the project root directory.


## [1.0.1] - 2016-12-29
### Changed
- remove package `resolve` to fix the bug caused by format changing of the internal file core.json of resolve 1.1.7
- add bug description and solution to README.md


## [1.0.0] - 2016-12-23
### Added
- support resolve node modules with alias config
Expand Down
48 changes: 34 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,69 @@
# eslint-import-resolver-alias

[![Version npm][version]](http://browsenpm.org/package/eslint-import-resolver-alias)[![Build Status][build]](https://travis-ci.org/johvin/eslint-import-resolver-alias)[![Download][download]](https://www.npmjs.com/package/eslint-import-resolver-alias)[![Dependencies][david]](https://david-dm.org/johvin/eslint-import-resolver-alias)[![Coverage Status][cover]](https://coveralls.io/github/johvin/eslint-import-resolver-alias?branch=master)
[![Version npm][version]](http://browsenpm.org/package/eslint-import-resolver-alias)
[![Build Status][build]](https://travis-ci.org/johvin/eslint-import-resolver-alias)
[![Download][download]](https://www.npmjs.com/package/eslint-import-resolver-alias)
[![Dependencies][david]](https://david-dm.org/johvin/eslint-import-resolver-alias)
[![Coverage Status][cover]](https://coveralls.io/github/johvin/eslint-import-resolver-alias?branch=master)
[![Known Vulnerabilities][vulnerabilities]](https://snyk.io/test/npm/eslint-import-resolver-alias)
[![License][license]](https://opensource.org/licenses/MIT)

[version]: http://img.shields.io/npm/v/eslint-import-resolver-alias.svg?style=flat-square
[build]: http://img.shields.io/travis/johvin/eslint-import-resolver-alias/master.svg?style=flat-square
[download]: https://img.shields.io/npm/dt/eslint-import-resolver-alias.svg?style=flat-square
[download]: https://img.shields.io/npm/dm/eslint-import-resolver-alias.svg?style=flat-square
[david]: https://img.shields.io/david/johvin/eslint-import-resolver-alias.svg?style=flat-square
[cover]: http://img.shields.io/coveralls/johvin/eslint-import-resolver-alias/master.svg?style=flat-square
[vulnerabilities]: https://snyk.io/test/npm/eslint-import-resolver-alias/badge.svg?style=flat-square
[license]: https://img.shields.io/badge/License-MIT-brightgreen.svg?style=flat-square


This is a simple Node module import resolution plugin for [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import), which supports module alias.
This is a simple Node.js module import resolution plugin for [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import), which supports module alias and native Node.js module import.

## Example

## Installation

Prerequisites: Node.js >=4.x and corresponding version of npm.

```shell
npm install eslint-plugin-import eslint-import-resolver-alias --save-dev
```


## Usage

Pass this resolver and its parameters to `eslint-plugin-import` using your `eslint` config file, `.eslintrc` or `.eslintrc.js`.

```js
// .eslintrc.js
module.exports = {
settings: {
'import/resolver': {
'alias': [
['babel-polyfill', 'babel-polyfill/dist/polyfill.min.js'],
['helper', './utils/helper'],
['material-ui/DatePicker', '../custom/DatePicker'],
['material-ui', 'material-ui-ie10']
]
],
// node: true
}
}
};
```


Note:

- The items of alias config array is also array which contains 2 string
+ The first string represents the mapped module
+ The second string represents the module alias
- The order of 'material-ui/DatePicker' and 'material-ui' cannot be reversed, otherwise the alias rule 'material-ui/DatePicker' does not work.

## Note

`eslint-import-resolver-alias` 1.0.0 depends on part of `resolve` 1.1.7 which changes its format from 1.2.0 and the resolve dependency is resolved as ^1.1.7 in package.json dependencies settings. So update `eslint-import-resolver-alias` to 1.0.1 is the correct way.
+ The first string represents the mapped module name or path
+ The second string represents the module alias, the actual module path os module name
- The config item `['helper', './utils/helper']` means that the module `helper/*` will be resolved to `./utils/helper/*`. See [#3](https://github.com/johvin/eslint-import-resolver-alias/issues/3)
- The order of 'material-ui/DatePicker' and 'material-ui' cannot be reversed, otherwise the alias rule 'material-ui/DatePicker' does not work
- when the config is an empty array or not an array, the resolver falls back to native Node.js module import

## CHANGELOG

[`CHANGELOG`](./CHANGELOG.md)

## References:
## References

- eslint-plugin-import/no-extraneous-dependencies
- eslint-plugin-import/no-unresolved
Expand Down
23 changes: 23 additions & 0 deletions build/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs');
const adjustMD = require('adjust-md-for-publish');

function copyFile(filename) {
fs.writeFileSync(`dist/${filename}`, fs.readFileSync(filename));
}

adjustMD({
filename: 'README.md',
destname: 'dist/README.md',
filterSection: [
'CHANGELOG',
'References'
]
});

[
'core.js',
'index.js',
'package.json'
].forEach(copyFile);

console.log('\n release is ready !!!');
1 change: 1 addition & 0 deletions core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 0 additions & 40 deletions core.json

This file was deleted.

1 change: 1 addition & 0 deletions dist/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NOTE.md
3 changes: 3 additions & 0 deletions dist/NOTE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# NOTE

The `dist` folder contains the standalone build for guideline, however files here are not checked-in whenever there are changes in the source code. If you are on the `dev` branch, files here are **NOT** up to date. Only the `master` branch contains the built files for the latest stable version.
60 changes: 60 additions & 0 deletions dist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# eslint-import-resolver-alias

[![Version npm][version]](http://browsenpm.org/package/eslint-import-resolver-alias)
[![Build Status][build]](https://travis-ci.org/johvin/eslint-import-resolver-alias)
[![Download][download]](https://www.npmjs.com/package/eslint-import-resolver-alias)
[![Dependencies][david]](https://david-dm.org/johvin/eslint-import-resolver-alias)
[![Coverage Status][cover]](https://coveralls.io/github/johvin/eslint-import-resolver-alias?branch=master)
[![Known Vulnerabilities][vulnerabilities]](https://snyk.io/test/npm/eslint-import-resolver-alias)
[![License][license]](https://opensource.org/licenses/MIT)

[version]: http://img.shields.io/npm/v/eslint-import-resolver-alias.svg?style=flat-square
[build]: http://img.shields.io/travis/johvin/eslint-import-resolver-alias/master.svg?style=flat-square
[download]: https://img.shields.io/npm/dm/eslint-import-resolver-alias.svg?style=flat-square
[david]: https://img.shields.io/david/johvin/eslint-import-resolver-alias.svg?style=flat-square
[cover]: http://img.shields.io/coveralls/johvin/eslint-import-resolver-alias/master.svg?style=flat-square
[vulnerabilities]: https://snyk.io/test/npm/eslint-import-resolver-alias/badge.svg?style=flat-square
[license]: https://img.shields.io/badge/License-MIT-brightgreen.svg?style=flat-square


This is a simple Node.js module import resolution plugin for [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import), which supports module alias and native Node.js module import.


## Installation

Prerequisites: Node.js >=4.x and corresponding version of npm.

```shell
npm install eslint-plugin-import eslint-import-resolver-alias --save-dev
```


## Usage

Pass this resolver and its parameters to `eslint-plugin-import` using your `eslint` config file, `.eslintrc` or `.eslintrc.js`.

```js
// .eslintrc.js
module.exports = {
settings: {
'import/resolver': {
'alias': [
['babel-polyfill', 'babel-polyfill/dist/polyfill.min.js'],
['helper', './utils/helper'],
['material-ui/DatePicker', '../custom/DatePicker'],
['material-ui', 'material-ui-ie10']
],
// node: true
}
}
};
```

Note:

- The items of alias config array is also array which contains 2 string
+ The first string represents the mapped module name or path
+ The second string represents the module alias, the actual module path os module name
- The config item `['helper', './utils/helper']` means that the module `helper/*` will be resolved to `./utils/helper/*`. See [#3](https://github.com/johvin/eslint-import-resolver-alias/issues/3)
- The order of 'material-ui/DatePicker' and 'material-ui' cannot be reversed, otherwise the alias rule 'material-ui/DatePicker' does not work
- when the config is an empty array or not an array, the resolver falls back to native Node.js module import
1 change: 1 addition & 0 deletions dist/core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';
/**
* this resolver is used as a plugin of eslint-plugin-import
* to solve npm package mapping problem
*
*/
const path = require('path');
const coreModules = {};

require('./core').forEach(function (m) {
this[m] = true
}, coreModules);

exports.interfaceVersion = 2;

const Module = module.constructor;
const toString = Object.prototype.toString;

exports.resolve = (modulePath, sourceFile, config) => {
const isRelativePath = modulePath[0] === '.';
if (!isRelativePath && coreModules[modulePath]) {
return { found: true, path: null };
}

const sourceDir = path.dirname(sourceFile);
let findPath;

if (isRelativePath) {
findPath = path.resolve(sourceDir, modulePath);
return findModulePath(findPath, []);
}

/* istanbul ignore else */
if (toString.call(config) === '[object Array]') {
for (let i = 0, len = config.length; i < len; i++) {
const re = new RegExp(`(^|/)${config[i][0]}($|/)`);
const match = modulePath.match(re);
if (match) {
findPath = modulePath.replace(match[0], `${match[1]}${config[i][1]}${match[2]}`);
break;
}
}
}

if (!findPath) {
findPath = modulePath;
}

const paths = resolveLookupPaths(sourceDir);
return findModulePath(findPath, paths);
};

function findModulePath(request, paths) {
const filename = Module._findPath(request, paths);
return {
found: !!filename,
path: filename || null
};
}

function resolveLookupPaths(absoluteSourceDir) {
const paths = [];
let curDir;
let nextDir = absoluteSourceDir;
do {
// not append node_modules to a path already ending with node_modules
while(nextDir.indexOf('node_modules', nextDir.length - 12) !== -1) {
nextDir = path.resolve(nextDir, '..');
}
curDir = nextDir;
paths.push(path.resolve(curDir, 'node_modules'));
nextDir = path.resolve(curDir, '..');
} while(nextDir !== curDir);

paths.push.apply(paths, Module.globalPaths);
return paths;
}
44 changes: 44 additions & 0 deletions dist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "eslint-import-resolver-alias",
"version": "1.0.3",
"description": "a simple Node behavior import resolution plugin for eslint-plugin-import, supporting module alias.",
"main": "index.js",
"scripts": {
"release": "node build/release.js",
"test": "mocha test/test.js --require test/setup.js",
"cov": "istanbul cover node_modules/mocha/bin/_mocha -i index.js -- test/test.js --require test/setup.js"
},
"keywords": [
"eslint",
"eslintplugin",
"modules",
"alias",
"eslint-plugin-import",
"import-resolver"
],
"engines": {
"node": ">= 4"
},
"author": "johvin",
"license": "MIT",
"devDependencies": {
"adjust-md-for-publish": "^1.0.0",
"builtin-modules": "^2.0.0",
"istanbul": "^1.0.0-alpha.2",
"mocha": "^3.2.0"
},
"peerDependencies": {
"eslint-plugin-import": ">=1.4.0"
},
"directories": {
"test": "test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/johvin/eslint-import-resolver-alias.git"
},
"bugs": {
"url": "https://github.com/johvin/eslint-import-resolver-alias/issues"
},
"homepage": "https://github.com/johvin/eslint-import-resolver-alias#readme"
}
Loading

0 comments on commit fc1a38c

Please sign in to comment.