https://github.com/bluedaniel/js-codemod-import-absolute
:art: Codemod to replace relative imports with absolute or custom paths
https://github.com/bluedaniel/js-codemod-import-absolute
codemod jscodeshift
Last synced: 5 months ago
JSON representation
:art: Codemod to replace relative imports with absolute or custom paths
- Host: GitHub
- URL: https://github.com/bluedaniel/js-codemod-import-absolute
- Owner: bluedaniel
- Created: 2017-07-14T08:18:26.000Z (almost 9 years ago)
- Default Branch: develop
- Last Pushed: 2022-11-18T21:17:51.000Z (over 3 years ago)
- Last Synced: 2024-11-02T19:34:11.399Z (over 1 year ago)
- Topics: codemod, jscodeshift
- Language: JavaScript
- Homepage:
- Size: 55.7 KB
- Stars: 8
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-codemods - js-codemod-import-absolute - Codemod to replace relative imports with absolute or custom paths. (JavaScript)
README
# JS Codemod Import Absolute
Codemod to replace `["parent", "sibling", "index"]` imports with absolute or custom paths.
It can also sort according to the `eslint-plugin-import` [order rule](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md).
*Note*: Use the -d option for a dry-run and use -p to print the output for comparison.
```javascript
// /Users/bluedaniel/Sites/test/index.js
import bar from './bar';
import _ from 'lodash';
import baz from './bar/baz';
import chalk from 'chalk';
import foo from '../foo';
import foo from 'src/foo';
import fs from 'fs';
import main from './';
import path from 'path';
import qux from '../../foo/qux';
// jscodeshift -t import-absolute.js
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import _ from 'lodash';
import foo from 'src/foo';
import qux from "/Users/bluedaniel/foo/qux";
import foo from "/Users/bluedaniel/Sites/foo";
import bar from "/Users/bluedaniel/Sites/test/bar";
import baz from "/Users/bluedaniel/Sites/test/bar/baz";
import main from "/Users/bluedaniel/Sites/test";
```
## Installing
```shell
$ git clone https://github.com/bluedaniel/js-codemod-import-absolute.git
$ cd js-codemod-import-absolute
$ npm i
```
## Options (All are optional)
```
--absolutePath
Replaces your current directory to a string of your choosing
--replace
Replace any string in the new path
--replaceWith [default '']
Replaces any occurrences of the string specified in `--replace`. Default is ''
--sort [default true]
Sorts imports according to `eslint-plugin-import` order rule
https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md
```