UNPKG

3.11 kBMarkdownView Raw
1# rollup-plugin-alias
2Define aliases when bundling packages with Rollup.
3
4[![Build Status](https://travis-ci.org/rollup/rollup-plugin-alias.svg?branch=master)](https://travis-ci.org/rollup/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
33// rollup.config.js
34import alias from 'rollup-plugin-alias';
35
36export default {
37 input: './src/index.js',
38 plugins: [
39 alias({
40 resolve: ['.jsx', '.js'], //optional, by default this will just look for .js files or folders
41 entries:[
42 {find:'something', replacement: '../../../something'}, //the initial example
43 {find:'somelibrary-1.0.0', replacement: './mylocallibrary-1.5.0'}, //remap a library with a specific version
44 {find:/^i18n\!(.*)/, replacement: '$1.js'}, //remove something in front of the import and append an extension (e.g. loaders, for files that were previously transpiled via the AMD module, to properly handle them in rollup as internals now)
45 //for whatever reason, replace all .js extensions with .wasm
46 {find:/^(.*)\.js$/, replacement: '$1.wasm'}
47 ]
48 })
49 ],
50};
51
52// or with object syntax
53export default {
54 input: './src/index.js',
55 plugins: [
56 alias({
57 resolve: ['.jsx', '.js'],
58 entries: {
59 something: '../../../something',
60 'somelibrary-1.0.0': './mylocallibrary-1.5.0',
61 }
62 })
63 ],
64};
65```
66The order of the entries is important, in that the first rules are applied first.
67
68You can use either simple Strings or Regular Expressions to search in a more distinct and complex manner (e.g. to do partial replacements via subpattern-matching, see aboves example).
69
70## License
71MIT, see `LICENSE` for more information