Skip to content

Commit a7f1a89

Browse files
feat: plugin can be positioned anywhere in the plugins list
1 parent 89a8020 commit a7f1a89

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,32 @@ export default function externalAssets(pattern: FilterPattern, options?: PluginO
6060

6161
return {
6262
name: PLUGIN_NAME,
63+
async options(inputOptions) {
64+
const plugins = inputOptions.plugins;
65+
66+
// No transformations.
67+
if (!plugins) return null;
68+
69+
// Separate our plugin from other plugins.
70+
const externalAssetsPlugins: Plugin[] = [];
71+
const otherPlugins = plugins.filter(plugin => {
72+
if (plugin.name !== PLUGIN_NAME) return true;
73+
74+
externalAssetsPlugins.push(plugin);
75+
return false;
76+
});
77+
78+
// Re-position our plugin to be the first in the list.
79+
// Otherwise, if there's a plugin that resolves paths before ours,
80+
// non-external imports can trigger the load hook for assets that can't be parsed by other plugins.
81+
return {
82+
...inputOptions,
83+
plugins: [
84+
...externalAssetsPlugins,
85+
...otherPlugins,
86+
],
87+
}
88+
},
6389
async resolveId(source, importer) {
6490
// Skip resolving entrypoints,
6591
// and don't resolve imports from filtered out modules.

tests/general.test.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const test = require("ava");
22
const { nodeResolve } = require("@rollup/plugin-node-resolve");
3-
const { bundleThrowsMacro } = require("./macros");
3+
const { rollup } = require("rollup");
44
const externalAssets = require("..");
55

66
const falsy = [undefined, null, false, "", NaN, 0];
@@ -15,18 +15,17 @@ for (const value of falsy) {
1515
});
1616
}
1717

18-
// If there's a plugin that resolves paths before ours, non-external imports trigger the load hook.
19-
test("Plugin doesn't work if it's not the first in the list", bundleThrowsMacro,
20-
{
21-
input: "tests/fixtures/src/index2.js",
22-
plugins: [
23-
nodeResolve({
24-
moduleDirectories: ["tests/fixtures/node_modules"],
25-
}),
26-
externalAssets(["tests/fixtures/assets/*", /@fontsource\/open-sans/]),
27-
],
28-
},
29-
{
30-
code: "PARSE_ERROR",
31-
},
32-
);
18+
// Solved by re-positioning the plugin to be the first on the list.
19+
test("Plugin works even if it's not the first in the list", async t => {
20+
await t.notThrowsAsync(
21+
rollup({
22+
input: "tests/fixtures/src/index2.js",
23+
plugins: [
24+
nodeResolve({
25+
moduleDirectories: ["tests/fixtures/node_modules"],
26+
}),
27+
externalAssets(["tests/fixtures/assets/*", /@fontsource\/open-sans/]),
28+
],
29+
})
30+
);
31+
});

0 commit comments

Comments
 (0)