forked from symfony/ux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
64 lines (58 loc) · 1.96 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import glob from 'glob';
import path from 'path';
import pkgUp from 'pkg-up';
/**
* Guarantees that any files imported from a peer dependency are treated as an external.
*
* For example, if we import `chart.js/auto`, that would not normally
* match the "chart.js" we pass to the "externals" config. This plugin
* catches that case and adds it as an external.
*
* Inspired by https://github.com/oat-sa/rollup-plugin-wildcard-external
*/
const wildcardExternalsPlugin = (peerDependencies) => ({
name: 'wildcard-externals',
resolveId(source, importer) {
if (importer) {
let matchesExternal = false;
peerDependencies.forEach((peerDependency) => {
if (source.includes(`/${peerDependency}/`)) {
matchesExternal = true;
}
});
if (matchesExternal) {
return {
id: source,
external: true,
moduleSideEffects: true
};
}
}
return null; // other ids should be handled as usually
}
});
const files = glob.sync("src/**/assets/src/*controller.ts");
const packages = files.map((file) => {
const absolutePath = path.join(__dirname, file);
const packageData = require(pkgUp.sync({ cwd: absolutePath }));
const peerDependencies = [
'@hotwired/stimulus',
...(packageData.peerDependencies ? Object.keys(packageData.peerDependencies) : [])
];
return {
input: file,
output: {
file: path.join(absolutePath, '..', '..', 'dist', path.basename(file, '.ts') + '.js'),
format: 'esm',
},
external: peerDependencies,
plugins: [
resolve(),
typescript(),
wildcardExternalsPlugin(peerDependencies)
],
};
});
export default packages;