UNPKG

2.38 kBMarkdownView Raw
1# rollup-plugin-alias
2Define aliases when bundling packages with Rollup.
3
4[![Build Status](https://travis-ci.org/frostney/rollup-plugin-alias.svg?branch=master)](https://travis-ci.org/frostney/rollup-plugin-alias) [![Dependency Status](https://david-dm.org/frostney/rollup-plugin-alias.svg)](https://david-dm.org/frostney/rollup-plugin-alias) [![devDependency Status](https://david-dm.org/frostney/rollup-plugin-alias/dev-status.svg)](https://david-dm.org/frostney/rollup-plugin-alias#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/github/frostney/rollup-plugin-alias/badge.svg?branch=master)](https://coveralls.io/github/frostney/rollup-plugin-alias?branch=master)
5
6Let's take a simple import as an example:
7
8```javascript
9import something from '../../../something';
10
11something();
12```
13
14This probably doesn't look too bad on its own. But imagine this is not the only instance in your code base and after a refactor/restructuring this might fall over. With this plugin in place, you can alias `../../../something` with `something` for readability. In case of a refactor only the alias would need to be changed instead of navigating through the code base and changing all imports.
15
16```javascript
17import something from 'something';
18
19something();
20```
21
22When we write tests, we may want an easier way to access the local library we are testing or mocking libraries. We may also define aliases to counteract "require hell" and get rid of all those `../../../` imports we may have in the process.
23
24For Webpack users: This is a plugin to mimic the `resolve.alias` functionality in Rollup.
25
26## Installation
27```
28$ npm install rollup-plugin-alias
29```
30
31## Usage
32```javascript
33import { rollup } from 'rollup';
34import alias from 'rollup-plugin-alias';
35
36rollup({
37 entry: './src/index.js',
38 plugins: [alias({
39 somelibrary: './mylocallibrary'
40 })],
41});
42```
43
44An optional `resolve` array with file extensions can be provided.
45If present local aliases beginning with `./` will be resolved to existing files:
46
47```javascript
48import { rollup } from 'rollup';
49import alias from 'rollup-plugin-alias';
50
51rollup({
52 entry: './src/index.js',
53 plugins: [alias({
54 resolve: ['.jsx', '.js'],
55 foo: './bar' // Will check for ./bar.jsx and ./bar.js
56 })],
57});
58```
59If not given local aliases will be resolved with a `.js` extension.
60
61## License
62MIT, see `LICENSE` for more information