forked from symfony/ux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
115 lines (107 loc) · 4.11 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const resolve = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const typescript = require('@rollup/plugin-typescript');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
/**
* 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
}
});
/**
* Moves the generated TypeScript declaration files to the correct location.
*
* This could probably be configured in the TypeScript plugin.
*/
const moveTypescriptDeclarationsPlugin = (packagePath) => ({
name: 'move-ts-declarations',
writeBundle: async () => {
const isBridge = packagePath.includes('src/Bridge');
const globPattern = isBridge
? path.join(packagePath, 'dist', packagePath.replace(/^src\//, ''), '**/*.d.ts')
: path.join(packagePath, 'dist', '*', 'assets', 'src', '**/*.d.ts')
const files = glob.sync(globPattern);
files.forEach((file) => {
// a bit odd, but remove first 7 or 13 directories, which will leave
// only the relative path to the file
const relativePath = file.split('/').slice(isBridge ? 13 : 7).join('/');
const targetFile = path.join(packagePath, 'dist', relativePath);
if (!fs.existsSync(path.dirname(targetFile))) {
fs.mkdirSync(path.dirname(targetFile), { recursive: true });
}
fs.renameSync(file, targetFile);
});
}
});
const file = process.env.INPUT_FILE;
const packageRoot = path.join(file, '..', '..');
const packagePath = path.join(packageRoot, 'package.json');
const packageData = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
const peerDependencies = [
'@hotwired/stimulus',
...(packageData.peerDependencies ? Object.keys(packageData.peerDependencies) : [])
];
// custom handling for StimulusBundle
if (file.includes('StimulusBundle/assets/src/loader.ts')) {
peerDependencies.push('./controllers.js');
}
// React, Vue
if (file.includes('assets/src/loader.ts')) {
peerDependencies.push('./components.js');
}
module.exports = {
input: file,
output: {
file: path.join(packageRoot, 'dist', path.basename(file, '.ts') + '.js'),
format: 'esm',
},
external: peerDependencies,
plugins: [
resolve(),
typescript({
filterRoot: packageRoot,
include: [
'src/**/*.ts',
// TODO: Remove for the next major release
// "@rollup/plugin-typescript" v11.0.0 fixed an issue (https://github.com/rollup/plugins/pull/1310) that
// cause a breaking change for UX React users, the dist file requires "react-dom/client" instead of "react-dom"
// and it will break for users using the Symfony AssetMapper without Symfony Flex (for automatic "importmap.php" upgrade).
'**/node_modules/react-dom/client.js'
],
compilerOptions: {
outDir: '.',
declaration: true,
emitDeclarationOnly: true,
}
}),
commonjs(),
wildcardExternalsPlugin(peerDependencies),
moveTypescriptDeclarationsPlugin(packageRoot),
],
};