Skip to content

Commit

Permalink
feat: plugin factory function can be used to produce multiple instances
Browse files Browse the repository at this point in the history
  • Loading branch information
recursive-beast committed Feb 27, 2021
1 parent 0099412 commit 95089b5
Show file tree
Hide file tree
Showing 5 changed files with 7,140 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export default function externalAssets(pattern: FilterPattern, options?: PluginO
],
}
},
async resolveId(source, importer) {
async resolveId(source, importer, options) {
// `this.resolve` was called from another instance of this plugin.
// skip to avoid infinite loop.
if (options.custom?.[PLUGIN_NAME]?.skip) return null;

// Skip resolving entrypoints,
// and don't resolve imports from filtered out modules.
if (!importer || !importerFilter(importer)) return null;
Expand All @@ -96,6 +100,11 @@ export default function externalAssets(pattern: FilterPattern, options?: PluginO
// We need to skip this plugin to avoid an infinite loop.
const resolution = await this.resolve(source, importer, {
skipSelf: true,
custom: {
[PLUGIN_NAME]: {
skip: true,
}
}
});

// If it cannot be resolved, or if the id is filtered out,
Expand Down
14 changes: 14 additions & 0 deletions tests/general.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const test = require("ava");
const { nodeResolve } = require("@rollup/plugin-node-resolve");
const { rollup } = require("rollup");
const { outputSnapshotMacro } = require("./macros");
const externalAssets = require("..");

const falsy = [undefined, null, false, "", NaN, 0];
Expand Down Expand Up @@ -29,3 +30,16 @@ test("Plugin works even if it's not the first in the list", async t => {
})
);
});

test("Multiple instances of the plugin can be used at the same time", outputSnapshotMacro,
{
input: "tests/fixtures/src/index2.js",
plugins: [
externalAssets("tests/fixtures/assets/*"),
nodeResolve({
moduleDirectories: ["tests/fixtures/node_modules"],
}),
externalAssets(/@fontsource\/open-sans/),
],
}
);
2 changes: 1 addition & 1 deletion tests/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const externalAssets = require("..");

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

t.is(resolution, null);
});
Expand Down
Loading

0 comments on commit 95089b5

Please sign in to comment.