Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Babel 7; standardjs #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"presets": ["es2015","stage-1"]
"presets": ["@babel/preset-env"],
"plugins": [
// Stage 3
["@babel/plugin-proposal-class-properties", { "loose": false }]
]
}
170 changes: 0 additions & 170 deletions .eslintrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
build
*.swp
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

### v1.2.0

Update to Babel v7. Use standardjs instead of custom eslint. Include 'prepare' script to support direct 'npm install' from git repo.

### v1.1.0

Add support to use inside Meteor. Basically, we turn off plugin when running inside Meteor.
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ Babel plugin to change the behaviour of `import` to root based paths.<br>
## Example
```javascript
// Usually
import SomeExample from '../../../some/example.js';
import SomeExample from '../../../some/example.js'

// With Babel-Root-Importer
import SomeExample from '/some/example.js';
// With babel-root-slash-import
import SomeExample from '/some/example.js'
```

## Install
```
npm install babel-root-slash-import --save-dev
npm install --save-dev babel-root-slash-import
```

```
yarn add --dev babel-root-slash-import
```

## Use
Expand All @@ -26,7 +30,7 @@ Add a `.babelrc` file and write:
```
or pass the plugin with the plugins-flag on CLI
```
babel-node myfile.js --plugins babel-root-slash-import
npx babel-node myfile.js --plugins babel-root-slash-import
```

## Extras
Expand Down
10 changes: 5 additions & 5 deletions config/mocha.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chai from 'chai';
import sinon from 'sinon';
import chai from 'chai'
import sinon from 'sinon'

global.chai = chai;
global.expect = chai.expect;
global.sinon = sinon;
global.chai = chai
global.expect = chai.expect
global.sinon = sinon
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
global.rootPath = process.cwd();
module.exports = require('./build/index.js');
global.rootPath = process.cwd()
module.exports = require('./build/index.js')
44 changes: 27 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
{
"name": "babel-root-slash-import",
"version": "1.1.0",
"version": "1.2.0",
"description": "Babel Plugin to enable relative root-import",
"author": "Arunoda Susiripala <[email protected]>",
"contributors": [
{
"name": "Adrian Lanning",
"email": "[email protected]"
}
],
"license": "MIT",
"main": "index.js",
"files": [
"index.js",
"build"
],
"repository": "mantrajs/babel-root-slash-import",
"repository": "alanning/babel-root-slash-import",
"scripts": {
"test": "mocha test/*.spec.js --require config/mocha.js --compilers js:babel-core/register",
"test-watch": "mocha test/*.spec.js --require config/mocha.js --compilers js:babel-core/register --watch",
"lint-js": "eslint plugin",
"compile": "babel -d build/ plugin/"
},
"dependencies": {
"babel": "^6.1.18"
"test": "npx mocha test/*.spec.js --require @babel/register --require config/mocha.js",
"test-watch": "npx mocha test/*.spec.js --require @babel/register --require config/mocha.js --watch",
"lint-js": "npx standard plugin",
"prepare": "npx babel -d build/ plugin/",
"compile": "npx babel -d build/ plugin/"
},
"dependencies": {},
"devDependencies": {
"babel-core": "^6.2.1",
"babel-eslint": "^4.0.6",
"babel-cli": "6.x.x",
"babel-preset-es2015": "^6.1.18",
"babel-preset-stage-1": "^6.1.18",
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.4",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/preset-env": "^7.4.4",
"@babel/register": "^7.4.4",
"babel-eslint": "^10.0.1",
"chai": "^3.2.0",
"eslint": "^1.1.0",
"mocha": "^2.2.5",
"sinon": "^1.15.4"
"eslint": "^5.16.0",
"mocha": "^3.1.0",
"sinon": "^7.3.2",
"standard": "^12.0.1"
},
"standard": {
"parser": "babel-eslint"
}
}
25 changes: 11 additions & 14 deletions plugin/helper.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
export default function(path) {
export default function (path) {
class BabelRootImportHelper {

root = global.rootPath || process.cwd();

transformRelativeToRootPath(path, rootPathSuffix) {
transformRelativeToRootPath (path, rootPathSuffix) {
if (this.hasRoot(path)) {
const withoutRoot = path.substring(1, path.length);
return `${this.root}${rootPathSuffix ? rootPathSuffix : ''}/${withoutRoot}`;
const withoutRoot = path.substring(1, path.length)
return `${this.root}${rootPathSuffix || ''}/${withoutRoot}`
}
if (typeof path === 'string') {
return path;
return path
}
throw new Error('ERROR: No path passed');
throw new Error('ERROR: No path passed')
}

hasRoot(string) {
let containsTilde = false;

hasRoot (string) {
if (typeof string !== 'string') {
return false;
return false
}

const firstChar = string.substring(0, 1);
return firstChar === '/';
const firstChar = string.substring(0, 1)
return firstChar === '/'
}
}

return new BabelRootImportHelper();
return new BabelRootImportHelper()
}
28 changes: 14 additions & 14 deletions plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import BabelRootImportHelper from './helper';
import BabelRootImportHelper from './helper'

export default function({ types: t }) {
export default function ({ types: t }) {
class BabelRootImport {
constructor() {
const that = this;
constructor () {
const that = this
return {
visitor: {
ImportDeclaration(path, state) {
const givenPath = path.node.source.value;
ImportDeclaration (path, state) {
const givenPath = path.node.source.value

var rootPathSuffix = state && state.opts && typeof state.opts.rootPathSuffix === 'string' ?
'/' + state.opts.rootPathSuffix.replace(/^(\/)|(\/)$/g, '') :
'';
var rootPathSuffix = state && state.opts && typeof state.opts.rootPathSuffix === 'string'
? '/' + state.opts.rootPathSuffix.replace(/^(\/)|(\/)$/g, '')
: ''

if(BabelRootImportHelper().hasRoot(givenPath)) {
path.node.source.value = BabelRootImportHelper().transformRelativeToRootPath(givenPath, rootPathSuffix);
if (BabelRootImportHelper().hasRoot(givenPath)) {
path.node.source.value = BabelRootImportHelper().transformRelativeToRootPath(givenPath, rootPathSuffix)
}
}
}
};
}
}
}

Expand All @@ -29,8 +29,8 @@ export default function({ types: t }) {
if (global.meteorBabelHelpers) {
return {
visitor: {}
};
}
}

return new BabelRootImport();
return new BabelRootImport()
}
Loading