Skip to content

Commit 95089b5

Browse files
feat: plugin factory function can be used to produce multiple instances
1 parent 0099412 commit 95089b5

File tree

5 files changed

+7140
-2
lines changed

5 files changed

+7140
-2
lines changed

src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ export default function externalAssets(pattern: FilterPattern, options?: PluginO
8686
],
8787
}
8888
},
89-
async resolveId(source, importer) {
89+
async resolveId(source, importer, options) {
90+
// `this.resolve` was called from another instance of this plugin.
91+
// skip to avoid infinite loop.
92+
if (options.custom?.[PLUGIN_NAME]?.skip) return null;
93+
9094
// Skip resolving entrypoints,
9195
// and don't resolve imports from filtered out modules.
9296
if (!importer || !importerFilter(importer)) return null;
@@ -96,6 +100,11 @@ export default function externalAssets(pattern: FilterPattern, options?: PluginO
96100
// We need to skip this plugin to avoid an infinite loop.
97101
const resolution = await this.resolve(source, importer, {
98102
skipSelf: true,
103+
custom: {
104+
[PLUGIN_NAME]: {
105+
skip: true,
106+
}
107+
}
99108
});
100109

101110
// If it cannot be resolved, or if the id is filtered out,

tests/general.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const test = require("ava");
22
const { nodeResolve } = require("@rollup/plugin-node-resolve");
33
const { rollup } = require("rollup");
4+
const { outputSnapshotMacro } = require("./macros");
45
const externalAssets = require("..");
56

67
const falsy = [undefined, null, false, "", NaN, 0];
@@ -29,3 +30,16 @@ test("Plugin works even if it's not the first in the list", async t => {
2930
})
3031
);
3132
});
33+
34+
test("Multiple instances of the plugin can be used at the same time", outputSnapshotMacro,
35+
{
36+
input: "tests/fixtures/src/index2.js",
37+
plugins: [
38+
externalAssets("tests/fixtures/assets/*"),
39+
nodeResolve({
40+
moduleDirectories: ["tests/fixtures/node_modules"],
41+
}),
42+
externalAssets(/@fontsource\/open-sans/),
43+
],
44+
}
45+
);

tests/resolve.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const externalAssets = require("..");
66

77
test("Skips resolving entrypoints", async t => {
88
const plugin = externalAssets("dummy_patern");
9-
const resolution = await plugin.resolveId("dummy_source", undefined);
9+
const resolution = await plugin.resolveId("dummy_source", undefined, {});
1010

1111
t.is(resolution, null);
1212
});

0 commit comments

Comments
 (0)