Skip to content

Commit 07846b5

Browse files
Merge pull request #56 from UmamiAppearance/regex-module-selection_plugin
Regex module selection
2 parents 98bc2ad + 67a5df8 commit 07846b5

File tree

7 files changed

+391
-88
lines changed

7 files changed

+391
-88
lines changed

README.md

Lines changed: 158 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ A Rollup plugin which makes it possible to manipulate import statements. Feature
1717
- [`warnings`](#warnings)
1818
- [`units`](#units)
1919
- [`module`](#module-option-for-units)
20+
- [`rawModule`](#rawmodule-option-for-units)
2021
- [`hash`](#hash-option-for-units)
2122
- [`id`](#id-option-for-units)
2223
- [`file`](#file-option-for-units)
@@ -54,14 +55,18 @@ A Rollup plugin which makes it possible to manipulate import statements. Feature
5455
- [Removing an Import Statement](#removing-an-import-statement)
5556
- [Shorthand Method](#shorthand-method)
5657
- [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)
5861
- [Addressing the (default) members](#addressing-the-default-members)
5962
- [Adding a defaultMember](#adding-a-defaultmember)
6063
- [Removing a member](#removing-a-member)
6164
- [Removing a group of members](#removing-a-group-of-members)
6265
- [Changing a defaultMember name](#changing-a-defaultmember-name)
6366
- [Renaming but keeping the alias](#renaming-but-keeping-the-alias)
6467
- [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)
6570
- [General Hints](#general-hints)
6671
- [Chaining](#chaining)
6772
- [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
162167
---
163168

164169
#### `module` <samp>[option for units]</samp>
165-
Type: `String`
170+
Type: `String` | `Object`
166171
Default: `null`
167172

168173
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:
171176
import foo from "./path/bar.js";
172177
```
173178
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.)
175181

176182
Absolute imports are directly assigned as the name attribute. So, the following example can be matched with `module: "bar"`
177183
```js
178184
import foo from "bar";
179185
```
180186

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+
181190
Also see this [example](#changing-the-module) of matching a module and changing it.
182191

183192

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+
import foo from "./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+
184207
#### `hash` <samp>[option for units]</samp>
185208
Type: `String`
186209
Default: `null`
@@ -189,7 +212,7 @@ Selects a unit by its hash. If - for any reason - it is not possible to match vi
189212

190213
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.
191214

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._
193216

194217

195218
#### `id` <samp>[option for units]</samp>
@@ -206,7 +229,7 @@ Internally every unit gets an Id. There are different scopes for the generation:
206229

207230
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.
208231

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._
210233

211234

212235
#### `file` <samp>[option for units]</samp>
@@ -353,10 +376,21 @@ An option to target an alias of a [selected](#select-option-for-actions) `defaul
353376

354377

355378
##### `rename` <samp>[option for actions]</samp>
356-
Type: `String`
379+
Type: `String` | `Function` _(when targeting the module)_
357380
Default: `null`
358381

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).
360394

361395

362396
##### `modType` <samp>[option for actions]</samp>
@@ -439,9 +473,9 @@ import "foobar";
439473
import bar as pub from "baz";
440474
import bar, { baz as qux } from "./path/to/foo.js"; // <--
441475
```
442-
443476
___
444477

478+
445479
#### Basic CJS Statement via [`createModule`](#createmodule-option-for-units)
446480
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)).
447481

@@ -695,7 +729,7 @@ plugins: [
695729
```
696730
___
697731

698-
#### Moving an Import Statement (cut and paste):
732+
### Moving an Import Statement (cut and paste):
699733

700734
###### Source Code
701735
```js
@@ -727,8 +761,11 @@ import { foo } from "bar"; // |
727761
```
728762
___
729763

730-
### Changing the module
731-
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:
732769

733770
###### Source Code
734771
```js
@@ -757,6 +794,53 @@ import foo from "bar";
757794
```
758795
___
759796

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+
import foo from "./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+
const importPath = moduleSourceRaw.slice(1, -1);
822+
823+
// Parse the path into its parts (path must be imported for this example)
824+
const importInfo = path.parse(importPath);
825+
826+
// Build the new import path with the sub-directory
827+
const newPath = [importInfo.dir, "build-temp", importInfo.base].join("/");
828+
829+
// Remember to add quotes again
830+
return `"${newPath}"`;
831+
}
832+
}
833+
}
834+
})
835+
]
836+
```
837+
838+
###### Bundle Code
839+
```js
840+
import foo from "./path/to/build-temp/bar.js";
841+
```
842+
___
843+
760844
### Addressing the (default) members
761845
`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"`.
762846

@@ -991,6 +1075,69 @@ import { foo, baz as corge, quux as grault } from "quuz";
9911075
```
9921076
___
9931077

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+
import foo from "./path/to/bar.js";
1084+
import baz from "./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+
import baz from "./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+
import foo from "./path/to/bar.js";
1116+
import baz from "./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/
1127+
actions: "remove"
1128+
}
1129+
})
1130+
]
1131+
```
1132+
1133+
###### Bundle Code
1134+
```js
1135+
import baz from "./path/to/foobar.js";
1136+
```
1137+
___
1138+
1139+
1140+
9941141
## General Hints
9951142

9961143
### Chaining

0 commit comments

Comments
 (0)