Skip to content

Commit f133f24

Browse files
committed
Extending rollup config to treat any file from a peer dep as an external
1 parent 44586b5 commit f133f24

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

rollup.config.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@ import glob from 'glob';
44
import path from 'path';
55
import pkgUp from 'pkg-up';
66

7+
/**
8+
* Guarantees that any files imported from a peer dependency are treated as an external.
9+
*
10+
* For example, if we import `chart.js/auto`, that would not normally
11+
* match the "chart.js" we pass to the "externals" config. This plugin
12+
* catches that case and adds it as an external.
13+
*
14+
* Inspired by https://github.com/oat-sa/rollup-plugin-wildcard-external
15+
*/
16+
const wildcardExternalsPlugin = (peerDependencies) => ({
17+
name: 'wildcard-externals',
18+
resolveId(source, importer) {
19+
if (importer) {
20+
let matchesExternal = false;
21+
peerDependencies.forEach((peerDependency) => {
22+
if (source.includes(`/${peerDependency}/`)) {
23+
matchesExternal = true;
24+
}
25+
});
26+
27+
if (matchesExternal) {
28+
return {
29+
id: source,
30+
external: true,
31+
moduleSideEffects: true
32+
};
33+
}
34+
}
35+
36+
return null; // other ids should be handled as usually
37+
}
38+
});
39+
740
const files = glob.sync("src/**/assets/src/*controller.ts");
841
const packages = files.map((file) => {
942
const absolutePath = path.join(__dirname, file);
@@ -23,6 +56,7 @@ const packages = files.map((file) => {
2356
plugins: [
2457
resolve(),
2558
typescript(),
59+
wildcardExternalsPlugin(peerDependencies)
2660
],
2761
};
2862
});

0 commit comments

Comments
 (0)