You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -17,6 +17,7 @@ A Rollup plugin which makes it possible to manipulate import statements. Feature
17
17
-[`warnings`](#warnings)
18
18
-[`units`](#units)
19
19
-[`module`](#module-option-for-units)
20
+
-[`rawModule`](#rawmodule-option-for-units)
20
21
-[`hash`](#hash-option-for-units)
21
22
-[`id`](#id-option-for-units)
22
23
-[`file`](#file-option-for-units)
@@ -54,14 +55,18 @@ A Rollup plugin which makes it possible to manipulate import statements. Feature
54
55
-[Removing an Import Statement](#removing-an-import-statement)
55
56
-[Shorthand Method](#shorthand-method)
56
57
-[Moving an Import Statement (cut and paste)](#moving-an-import-statement-cut-and-paste)
57
-
-[Changing the module](#changing-the-module)
58
+
-[Modifying the module](#changing-the-module)
59
+
-[Changing a relative path to an absolute path (passing a String to rename)](#changing-a-relative-path-to-an-absolute-path-passing-a-string-to-rename)
60
+
-[Changing a relative path to different directory (making use of a rename Function)](#changing-a-relative-path-to-different-directory-making-use-of-a-rename-function)
58
61
-[Addressing the (default) members](#addressing-the-default-members)
59
62
-[Adding a defaultMember](#adding-a-defaultmember)
60
63
-[Removing a member](#removing-a-member)
61
64
-[Removing a group of members](#removing-a-group-of-members)
62
65
-[Changing a defaultMember name](#changing-a-defaultmember-name)
63
66
-[Renaming but keeping the alias](#renaming-but-keeping-the-alias)
64
67
-[Addressing an alias](#addressing-an-alias)
68
+
-[Applying RegExp for module matching](#applying-regexp-for-module-matching)
69
+
-[Using rawModule for module matching](#using-rawmodule-for-module-matching)
65
70
-[General Hints](#general-hints)
66
71
-[Chaining](#chaining)
67
72
-[Array and Object shortening](#array-and-object-shortening)
@@ -162,7 +167,7 @@ This is where the plugin comes to life. Here is the place where units are gettin
162
167
---
163
168
164
169
#### `module` <samp>[option for units]</samp>
165
-
Type: `String`
170
+
Type: `String`| `Object`
166
171
Default: `null`
167
172
168
173
Selects a unit by its module name. Each import has a name object. This is constructed from the module.
@@ -171,16 +176,34 @@ Path information are getting removed. Consider this basic es6 import statement:
171
176
importfoofrom"./path/bar.js";
172
177
```
173
178
The corresponding unit assigns the module name `bar.js` which can be matched with: `module: "bar.js"`
174
-
(The matching method is actually a little more generous. You can skip the extension or even bigger parts if you like and if this doesn't lead to multiple matches).
179
+
180
+
The matching method is actually a little more generous. If the value is a `String` the provided value will get searched anywhere in the module name. You can skip the extension or even bigger parts if you like and if this doesn't lead to multiple matches. However, if you need more control, you can always use a [Regular Expression Object](#applying-regexp-for-module-matching). (If you need access to the full path you should use the [`rawModule`](#rawmodule-option-for-units) matching method.)
175
181
176
182
Absolute imports are directly assigned as the name attribute. So, the following example can be matched with `module: "bar"`
177
183
```js
178
184
importfoofrom"bar";
179
185
```
180
186
187
+
To match to an exact module name like `react` but exclude `react-table` for example, you can provide a `RegExp`:
188
+
`module: /^react$/`.
189
+
181
190
Also see this [example](#changing-the-module) of matching a module and changing it.
182
191
183
192
193
+
194
+
#### `rawModule` <samp>[option for units]</samp>
195
+
Type: `String` | `Object`
196
+
Default: `null`
197
+
198
+
Selects a unit by its raw module name. `rawModule` works exactly the same as [`module`](#module-option-for-units). The only difference is, that is using the raw full module path. Consider the example from above:
199
+
```js
200
+
importfoofrom"./path/bar.js";
201
+
```
202
+
The raw module stores the value `"./path/bar.js"` including the quotation marks, which can be matched as shown before via `String` or `RegExp`. See this [example](#using-rawmodule-for-module-matching).
203
+
204
+
_If any other matching option is set, `rawModule` gets ignored._
205
+
206
+
184
207
#### `hash` <samp>[option for units]</samp>
185
208
Type: `String`
186
209
Default: `null`
@@ -189,7 +212,7 @@ Selects a unit by its hash. If - for any reason - it is not possible to match vi
189
212
190
213
The hash is generated by the module name, its members and also the filename. If the filename or any of the other properties are changing so is the hash. So, if a module is selected via hash and any of the properties are changed, the build will fail afterwards as the hash is no longer existent. This is why the matching via module name should be preferred.
191
214
192
-
If the hash option is set, the [module](#module-option-for-units) option will get ignored.
215
+
_If the hash option is set, the [module](#module-option-for-units) option will get ignored._
193
216
194
217
195
218
#### `id` <samp>[option for units]</samp>
@@ -206,7 +229,7 @@ Internally every unit gets an Id. There are different scopes for the generation:
206
229
207
230
The first ES6 Import statement of a file will have the Id `1000`, the second `1001` and so forth. For a quick test, you can select via Id (if the [filename](#file) is specified). But actually this is only an internal method to locate the statements. Testing is the only other reason to use it. If the order or number of import statements changes, this will directly affect the Ids. This selection method should therefore never been used in production.
208
231
209
-
If the Id option is set, [hash](#hash-option-for-units) and [module](#module-option-for-units) will get ignored.
232
+
_If the Id option is set, [hash](#hash-option-for-units) and [module](#module-option-for-units) will get ignored._
210
233
211
234
212
235
#### `file` <samp>[option for units]</samp>
@@ -353,10 +376,21 @@ An option to target an alias of a [selected](#select-option-for-actions) `defaul
353
376
354
377
355
378
##### `rename` <samp>[option for actions]</samp>
356
-
Type: `String`
379
+
Type: `String`| `Function`_(when targeting the module)_
357
380
Default: `null`
358
381
359
-
This option is used to rename a [selected](#select-option-for-actions) specific part (`defaultMember`, `member`, `module`). The value is the new name of the selected part. See this [example](#changing-the-module).
382
+
This option is used to rename a [selected](#select-option-for-actions) specific part (`defaultMember`, `member`, `module`). The value is a string of the new name of the selected part.
383
+
384
+
Examples:
385
+
*[Changing a relative path to an absolute path](#changing-a-relative-path-to-an-absolute-path-passing-a-string-to-rename)
386
+
*[Changing a defaultMember name](#changing-a-defaultmember-name)
387
+
388
+
<br>
389
+
If the selected part is `module`, the value could alternatively be a function. The function must return a raw full module name (eg. `() => '"./new-module-name"'`) as a `String`. The original raw name is getting passed to the function and can be accessed by passing a variable to the function: `oldRawName => oldRawName.replace("foo", "bar")`.
390
+
391
+
When passing a function the [`modType`](#modtype-option-for-actions) is getting ignored. Always make sure the return value includes quotation marks if the import statement requires it.
392
+
393
+
See this [example](#changing-a-relative-path-to-different-directory-making-use-of-a-rename-function).
#### Basic CJS Statement via [`createModule`](#createmodule-option-for-units)
446
480
CJS Imports are also supported. But this time the [`type`](#type-option-for-units) needs to be specified. Also a variable name has to be set. In this example the [`const`](#const-option-for-units)_foo_. (Other declaration types are: [`let`](#let-option-for-units), [`var`](#var-option-for-units) and [`global`](#global-option-for-units)).
In this example there is a relative path that should be changed to a non relative module. This can be achieved like this:
764
+
### Modifying the module
765
+
To change a module in any way, first it must be [`select`](#select-option-for-actions)ed and then [`rename`](#rename-option-for-actions)ed, which can be fed with a `String` or a `Function` for module manipulation.
766
+
767
+
#### Changing a relative path to an absolute path (passing a `String` to [`rename`](#rename-option-for-actions))
768
+
In this example there is a relative path, that should be changed to a non relative module. This can be achieved like this:
732
769
733
770
###### Source Code
734
771
```js
@@ -757,6 +794,53 @@ import foo from "bar";
757
794
```
758
795
___
759
796
797
+
798
+
#### Changing a relative path to different directory (making use of a [`rename`](#rename-option-for-actions) function)
799
+
In this example there is a relative path, that should be changed to a sub-directory. This time a function is used for the goal, also a little help of an external function from [path](https://nodejs.org/api/path.html), which must be available (imported) in the rollup config file.
800
+
801
+
_(keep in mind, that a function in `rename` is only valid for modules)_
802
+
803
+
###### Source Code
804
+
```js
805
+
importfoofrom"./path/to/bar.js";
806
+
```
807
+
808
+
###### Rollup Config
809
+
```js
810
+
811
+
plugins: [
812
+
importManager({
813
+
units: {
814
+
file:"**/my-file.js",
815
+
module:"bar.js",
816
+
actions: {
817
+
select:"module",
818
+
rename:moduleSourceRaw=> {
819
+
820
+
// Get rid of the quotes
821
+
constimportPath=moduleSourceRaw.slice(1, -1);
822
+
823
+
// Parse the path into its parts (path must be imported for this example)
824
+
constimportInfo=path.parse(importPath);
825
+
826
+
// Build the new import path with the sub-directory
`defaultMembers` and `members` are using the exact same methods. It is only important to keep in mind to address default members with `select: "defaultMembers"` or for a specific one `select: "defaultMember"`; for members `select: "members"` and `select: "member"`.
762
846
@@ -991,6 +1075,69 @@ import { foo, baz as corge, quux as grault } from "quuz";
991
1075
```
992
1076
___
993
1077
1078
+
### Applying RegExp for [module](#module-option-for-units) matching
1079
+
This example demonstrates a case, where matching the module via a regular expression is necessary. Exemplary the first import statement of the following source code should be matched and removed. Searching for 'bar' or 'bar.js' is not an option, as this matches both statements. RegExp to the rescue:
1080
+
1081
+
###### Source Code
1082
+
```js
1083
+
importfoofrom"./path/to/bar.js";
1084
+
importbazfrom"./path/to/foobar.js";
1085
+
```
1086
+
1087
+
###### Rollup Config
1088
+
```js
1089
+
1090
+
plugins: [
1091
+
importManager({
1092
+
units: {
1093
+
file:"**/my-file.js",
1094
+
module:/[^foo]bar\.js/,
1095
+
actions:"remove"
1096
+
}
1097
+
})
1098
+
]
1099
+
```
1100
+
1101
+
###### Bundle Code
1102
+
```js
1103
+
importbazfrom"./path/to/foobar.js";
1104
+
```
1105
+
___
1106
+
1107
+
1108
+
### Using [`rawModule`](#rawmodule-option-for-units) for module matching
1109
+
This example demonstrates a case, where it is desired to match a module by its raw module-name part.
1110
+
1111
+
_(Be aware of the quotation marks, which are also part of the raw string in this case and might introduce problems when applying RegExp without taken them into account.)_
1112
+
1113
+
###### Source Code
1114
+
```js
1115
+
importfoofrom"./path/to/bar.js";
1116
+
importbazfrom"./path/to/foobar.js";
1117
+
```
1118
+
1119
+
###### Rollup Config
1120
+
```js
1121
+
1122
+
plugins: [
1123
+
importManager({
1124
+
units: {
1125
+
file:"**/my-file.js",
1126
+
rawModule:"./path/to/bar.js", // or eg. /\/bar.js/
0 commit comments