Skip to content

Commit 9ec6e63

Browse files
feat: filtering paths is delegated to createFilter from @rollup/pluginutils
BREAKING CHANGE: the plugin itself is not responsible for filtering paths
1 parent fa8a9de commit 9ec6e63

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,49 @@ export default {
4646
## API
4747

4848
```typescript
49-
function externalAssets(pattern: string | RegExp | (string | RegExp)[]);
49+
function externalAssets(
50+
include?: string | RegExp | (string | RegExp)[],
51+
exclude?: string | RegExp | (string | RegExp)[],
52+
options?: { resolve?: string | false | null },
53+
);
5054
```
5155

52-
### pattern
56+
### include / exclude
5357

5458
`string | RegExp | (string | RegExp)[]`
5559

56-
A picomatch pattern, or array of patterns, which correspond to assets the plugin should operate on.
60+
A valid [picomatch][9] pattern, or array of patterns.
61+
If `include` is omitted or has zero length, all imports will be considered as assets.
62+
Otherwise, an import path must match one or more of the `include` patterns,
63+
and must not match any of the `exclude` patterns.
64+
65+
**Note**: patterns that include windows paths are normalized to be valid picomatch patterns.
5766

5867
```javascript
68+
import path from "path";
69+
5970
// Operate on images located in the ./assets directory.
6071
externalAssets("assets/**/*.jpg");
6172

6273
// Operate on images located in the ./assets directory.
6374
// and all stylesheet files.
6475
externalAssets(["assets/**/*.{jpg,png}", /\.(css|scss)$/]);
76+
77+
// Operate on all assets except text files.
78+
externalAssets("assets/**/*", "**/*.txt");
79+
80+
// Operate on all assets except text files.
81+
// `__dirname` is the pattern's base dir instead of `process.cwd()`.
82+
externalAssets(path.resolve(__dirname, "assets/**/*"), "**/*.txt");
6583
```
6684

85+
### options
86+
87+
- `resolve` `{string | false | null}`: Optionally resolves the patterns against a directory other than `process.cwd()`.
88+
If a `string` is specified, then the value will be used as the base directory.
89+
Relative paths will be resolved against `process.cwd()` first.
90+
If `false`, then the patterns will not be resolved against any directory.
91+
6792
## Contributing
6893

6994
### Prerequisites
@@ -127,3 +152,4 @@ Please follow the [conventional commits][5] specification, because [semantic-rel
127152
[6]: https://github.com/semantic-release/semantic-release
128153
[7]: https://github.com/concordancejs/concordance/issues/68
129154
[8]: https://jestjs.io/
155+
[9]: https://github.com/micromatch/picomatch#globbing-features

src/index.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,33 @@ import { getIdDeduplicator, getOutputId, getRelativeImportPath } from "./helpers
88
const PLUGIN_NAME = "external-assets";
99
const PREFIX = `\0${PLUGIN_NAME}:`;
1010

11+
interface Options {
12+
/**
13+
* Optionally resolves the patterns against a directory other than `process.cwd()`.
14+
* If a `string` is specified, then the value will be used as the base directory.
15+
* Relative paths will be resolved against `process.cwd()` first.
16+
* If `false`, then the patterns will not be resolved against any directory.
17+
*/
18+
resolve?: string | false | null;
19+
}
20+
1121
/**
1222
* Make assets external but include them in the output.
13-
* @param pattern - A picomatch pattern, or array of patterns,
14-
* which correspond to assets the plugin should operate on.
23+
* @param include A valid picomatch pattern, or array of patterns.
24+
* If `include` is omitted or has zero length, all imports will be processed.
25+
*
26+
* **Note**: patterns that include windows paths are normalized to be valid picomatch patterns.
27+
* @param exclude If an asset matches one of the `exclude` patterns, its import will not be processed.
28+
*
29+
* **Note**: patterns that include windows paths are normalized to be valid picomatch patterns.
30+
* @param options The options object.
1531
*/
16-
export default function externalAssets(pattern: FilterPattern): Plugin {
17-
if (!pattern) throw new Error("please specify a pattern for targeted assets");
18-
19-
const idFilter = createFilter(pattern);
32+
export default function externalAssets(
33+
include?: FilterPattern,
34+
exclude?: FilterPattern,
35+
options?: Options,
36+
): Plugin {
37+
const idFilter = createFilter(include, exclude, options);
2038
const deduplicateId = getIdDeduplicator();
2139

2240
return {
@@ -35,11 +53,7 @@ export default function externalAssets(pattern: FilterPattern): Plugin {
3553
},
3654

3755
async load(id) {
38-
if (
39-
id.startsWith("\0") // Virtual module.
40-
|| id.includes("?") // Id reserved by some other plugin.
41-
|| !idFilter(id) // Filtered out id.
42-
) return null;
56+
if (!idFilter(id)) return null;
4357

4458
// For two or more assets with the same content, only one asset is going to be emitted.
4559
// `this.emitFile` deduplicates in the same way.

tests/general.test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
import externalAssets from "../src/index";
2-
3-
test("pattern is required", () => {
4-
// @ts-ignore
5-
expect(() => externalAssets()).toThrow(Error);
6-
});
7-
81
test.todo("produces correct sourcemaps");

0 commit comments

Comments
 (0)