-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathrollup.js
135 lines (124 loc) · 4.66 KB
/
rollup.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const fs = require('node:fs');
const path = require('node:path');
const resolve = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const typescript = require('@rollup/plugin-typescript');
const glob = require('glob');
/**
* 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 = (packageRoot) => ({
name: 'move-ts-declarations',
writeBundle: async () => {
const isBridge = packageRoot.includes('src/Bridge');
const globPattern = path.join('dist', '**', 'assets', 'src', '**/*.d.ts');
const files = glob.sync(globPattern);
files.forEach((file) => {
const relativePath = file;
// a bit odd, but remove first 7 or 4 directories, which will leave only the relative path to the file
// ex: dist/Chartjs/assets/src/controller.d.ts' => 'dist/controller.d.ts'
const targetFile = relativePath.replace(
`${relativePath
.split('/')
.slice(1, isBridge ? 7 : 4)
.join('/')}/`,
''
);
if (!fs.existsSync(path.dirname(targetFile))) {
fs.mkdirSync(path.dirname(targetFile), { recursive: true });
}
fs.renameSync(file, targetFile);
});
},
});
/**
* @param {String} packageRoot
* @param {Array<String>} inputFiles
*/
function getRollupConfiguration({ packageRoot, inputFiles }) {
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) : []),
];
inputFiles.forEach((file) => {
// 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');
}
});
const outDir = path.join(packageRoot, 'dist');
return {
input: inputFiles,
output: {
dir: outDir,
entryFileNames: '[name].js',
format: 'esm',
},
external: peerDependencies,
plugins: [
resolve(),
typescript({
filterRoot: '.',
tsconfig: path.join(__dirname, '..', 'tsconfig.json'),
noEmitOnError: true,
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: outDir,
declaration: true,
emitDeclarationOnly: true,
},
}),
commonjs(),
wildcardExternalsPlugin(peerDependencies),
moveTypescriptDeclarationsPlugin(packageRoot),
],
};
}
module.exports = {
getRollupConfiguration,
};