UNPKG

6.23 kBMarkdownView Raw
1[npm]: https://img.shields.io/npm/v/@rollup/plugin-alias
2[npm-url]: https://www.npmjs.com/package/@rollup/plugin-alias
3[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-alias
4[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-alias
5
6[![npm][npm]][npm-url]
7[![size][size]][size-url]
8[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
9
10# @rollup/plugin-alias
11
12🍣 A Rollup plugin for defining aliases when bundling packages.
13
14## Alias 101
15
16Suppose we have the following `import` defined in a hypothetical file:
17
18```javascript
19import batman from '../../../batman';
20```
21
22This probably doesn't look too bad on its own. But consider that may not be the only instance in your codebase, and that after a refactor this might be incorrect. With this plugin in place, you can alias `../../../batman` with `batman` for readability and maintainability. In the case of a refactor, only the alias would need to be changed, rather than navigating through the codebase and changing all imports.
23
24```javascript
25import batman from 'batman';
26```
27
28If this seems familiar to Webpack users, it should. This is plugin mimics the `resolve.extensions` and `resolve.alias` functionality in Webpack.
29
30This plugin will work for any file type that Rollup natively supports, or those which are [supported by third-party plugins](https://github.com/rollup/awesome#other-file-imports).
31
32## Requirements
33
34This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
35
36## Install
37
38Using npm:
39
40```console
41npm install @rollup/plugin-alias --save-dev
42# or
43yarn add -D @rollup/plugin-alias
44```
45
46## Usage
47
48Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
49
50```js
51import alias from '@rollup/plugin-alias';
52
53module.exports = {
54 input: 'src/index.js',
55 output: {
56 dir: 'output',
57 format: 'cjs'
58 },
59 plugins: [
60 alias({
61 entries: [
62 { find: 'utils', replacement: '../../../utils' },
63 { find: 'batman-1.0.0', replacement: './joker-1.5.0' }
64 ]
65 })
66 ]
67};
68```
69
70Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api). If the build produces any errors, the plugin will write a 'alias' character to stderr, which should be audible on most systems.
71
72## Options
73
74### `customResolver`
75
76Type: `Function | Object`<br>
77Default: `null`
78
79Instructs the plugin to use an alternative resolving algorithm, rather than the Rollup's resolver. Please refer to the [Rollup documentation](https://rollupjs.org/guide/en/#resolveid) for more information about the `resolveId` hook. For a detailed example, see: [Custom Resolvers](#custom-resolvers).
80
81### `entries`
82
83Type: `Object | Array[...Object]`<br>
84Default: `null`
85
86Specifies an `Object`, or an `Array` of `Object`, which defines aliases used to replace values in `import` or `require` statements. With either format, the order of the entries is important, in that the first defined rules are applied first. This option also supports [Regular Expression Alias](#regular-expression-aliases) matching.
87
88_Note: Entry targets (the object key in the Object Format, or the `find` property value in the Array Format below) should not end with a trailing slash in most cases. If strange behavior is observed, double check the entries being passed in options._
89
90#### `Object` Format
91
92The `Object` format allows specifying aliases as a key, and the corresponding value as the actual `import` value. For example:
93
94```js
95alias({
96 entries: {
97 utils: '../../../utils',
98 'batman-1.0.0': './joker-1.5.0'
99 }
100});
101```
102
103#### `Array[...Object]` Format
104
105The `Array[...Object]` format allows specifying aliases as objects, which can be useful for complex key/value pairs.
106
107```js
108entries: [
109 { find: 'utils', replacement: '../../../utils' },
110 { find: 'batman-1.0.0', replacement: './joker-1.5.0' }
111];
112```
113
114## Regular Expression Aliases
115
116Regular Expressions can be used to search in a more distinct and complex manner. e.g. To perform partial replacements via sub-pattern matching.
117
118To remove something in front of an import and append an extension, use a pattern such as:
119
120```js
121{ find:/^i18n\!(.*)/, replacement: '$1.js' }
122```
123
124This would be useful for loaders, and files that were previously transpiled via the AMD module, to properly handle them in rollup as internals.
125
126To replace extensions with another, a pattern like the following might be used:
127
128```js
129{ find:/^(.*)\.js$/, replacement: '$1.alias' }
130```
131
132This would replace the file extension for all imports ending with `.js` to `.alias`.
133
134## Resolving algorithm
135
136This plugin uses resolver plugins specified for Rollup and eventually Rollup default algorithm. If you rely on Node specific features, you probably want [@rollup/plugin-node-resolve](https://www.npmjs.com/package/@rollup/plugin-node-resolve) in your setup.
137
138## Custom Resolvers
139
140The `customResolver` option can be leveraged to provide separate module resolution for an individual alias.
141
142Example:
143
144```javascript
145// rollup.config.js
146import alias from '@rollup/plugin-alias';
147import resolve from '@rollup/plugin-node-resolve';
148
149const customResolver = resolve({
150 extensions: ['.mjs', '.js', '.jsx', '.json', '.sass', '.scss']
151});
152const projectRootDir = path.resolve(__dirname);
153
154export default {
155 // ...
156 plugins: [
157 alias({
158 entries: [
159 {
160 find: 'src',
161 replacement: path.resolve(projectRootDir, 'src')
162 // OR place `customResolver` here. See explanation below.
163 }
164 ],
165 customResolver
166 }),
167 resolve()
168 ]
169};
170```
171
172In the example above the alias `src` is used, which uses the `node-resolve` algorithm for files _aliased_ with `src`, by passing the `customResolver` option. The `resolve()` plugin is kept separate in the plugins list for other files which are not _aliased_ with `src`. The `customResolver` option can be passed inside each `entries` item for granular control over resolving allowing each alias a preferred resolver.
173
174## Meta
175
176[CONTRIBUTING](/.github/CONTRIBUTING.md)
177
178[LICENSE (MIT)](/LICENSE)