|
| 1 | +const fs = require('node:fs'); |
| 2 | +const path = require('node:path'); |
| 3 | +const resolve = require('@rollup/plugin-node-resolve'); |
| 4 | +const commonjs = require('@rollup/plugin-commonjs'); |
| 5 | +const typescript = require('@rollup/plugin-typescript'); |
| 6 | +const glob = require('glob'); |
| 7 | + |
| 8 | +/** |
| 9 | + * Guarantees that any files imported from a peer dependency are treated as an external. |
| 10 | + * |
| 11 | + * For example, if we import `chart.js/auto`, that would not normally |
| 12 | + * match the "chart.js" we pass to the "externals" config. This plugin |
| 13 | + * catches that case and adds it as an external. |
| 14 | + * |
| 15 | + * Inspired by https://github.com/oat-sa/rollup-plugin-wildcard-external |
| 16 | + */ |
| 17 | +const wildcardExternalsPlugin = (peerDependencies) => ({ |
| 18 | + name: 'wildcard-externals', |
| 19 | + resolveId(source, importer) { |
| 20 | + if (importer) { |
| 21 | + let matchesExternal = false; |
| 22 | + peerDependencies.forEach((peerDependency) => { |
| 23 | + if (source.includes(`/${peerDependency}/`)) { |
| 24 | + matchesExternal = true; |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + if (matchesExternal) { |
| 29 | + return { |
| 30 | + id: source, |
| 31 | + external: true, |
| 32 | + moduleSideEffects: true, |
| 33 | + }; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return null; // other ids should be handled as usually |
| 38 | + }, |
| 39 | +}); |
| 40 | + |
| 41 | +/** |
| 42 | + * Moves the generated TypeScript declaration files to the correct location. |
| 43 | + * |
| 44 | + * This could probably be configured in the TypeScript plugin. |
| 45 | + */ |
| 46 | +const moveTypescriptDeclarationsPlugin = (packageRoot) => ({ |
| 47 | + name: 'move-ts-declarations', |
| 48 | + writeBundle: async () => { |
| 49 | + const isBridge = packageRoot.includes('src/Bridge'); |
| 50 | + const globPattern = path.join('dist', '**', 'assets', 'src', '**/*.d.ts'); |
| 51 | + const files = glob.sync(globPattern); |
| 52 | + |
| 53 | + files.forEach((file) => { |
| 54 | + const relativePath = file; |
| 55 | + // a bit odd, but remove first 7 or 4 directories, which will leave only the relative path to the file |
| 56 | + // ex: dist/Chartjs/assets/src/controller.d.ts' => 'dist/controller.d.ts' |
| 57 | + const targetFile = relativePath.replace( |
| 58 | + `${relativePath |
| 59 | + .split('/') |
| 60 | + .slice(1, isBridge ? 7 : 4) |
| 61 | + .join('/')}/`, |
| 62 | + '' |
| 63 | + ); |
| 64 | + if (!fs.existsSync(path.dirname(targetFile))) { |
| 65 | + fs.mkdirSync(path.dirname(targetFile), { recursive: true }); |
| 66 | + } |
| 67 | + fs.renameSync(file, targetFile); |
| 68 | + }); |
| 69 | + }, |
| 70 | +}); |
| 71 | + |
| 72 | +/** |
| 73 | + * @param {String} packageRoot |
| 74 | + * @param {Array<String>} inputFiles |
| 75 | + */ |
| 76 | +function getRollupConfiguration({ packageRoot, inputFiles }) { |
| 77 | + const packagePath = path.join(packageRoot, 'package.json'); |
| 78 | + const packageData = JSON.parse(fs.readFileSync(packagePath, 'utf8')); |
| 79 | + const peerDependencies = [ |
| 80 | + '@hotwired/stimulus', |
| 81 | + ...(packageData.peerDependencies ? Object.keys(packageData.peerDependencies) : []), |
| 82 | + ]; |
| 83 | + |
| 84 | + inputFiles.forEach((file) => { |
| 85 | + // custom handling for StimulusBundle |
| 86 | + if (file.includes('StimulusBundle/assets/src/loader.ts')) { |
| 87 | + peerDependencies.push('./controllers.js'); |
| 88 | + } |
| 89 | + |
| 90 | + // React, Vue |
| 91 | + if (file.includes('assets/src/loader.ts')) { |
| 92 | + peerDependencies.push('./components.js'); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + const outDir = path.join(packageRoot, 'dist'); |
| 97 | + |
| 98 | + return { |
| 99 | + input: inputFiles, |
| 100 | + output: { |
| 101 | + dir: outDir, |
| 102 | + entryFileNames: '[name].js', |
| 103 | + format: 'esm', |
| 104 | + }, |
| 105 | + external: peerDependencies, |
| 106 | + plugins: [ |
| 107 | + resolve(), |
| 108 | + typescript({ |
| 109 | + filterRoot: '.', |
| 110 | + tsconfig: path.join(__dirname, '..', 'tsconfig.json'), |
| 111 | + include: [ |
| 112 | + 'src/**/*.ts', |
| 113 | + // TODO: Remove for the next major release |
| 114 | + // "@rollup/plugin-typescript" v11.0.0 fixed an issue (https://github.com/rollup/plugins/pull/1310) that |
| 115 | + // cause a breaking change for UX React users, the dist file requires "react-dom/client" instead of "react-dom" |
| 116 | + // and it will break for users using the Symfony AssetMapper without Symfony Flex (for automatic "importmap.php" upgrade). |
| 117 | + '**/node_modules/react-dom/client.js', |
| 118 | + ], |
| 119 | + compilerOptions: { |
| 120 | + outDir: outDir, |
| 121 | + declaration: true, |
| 122 | + emitDeclarationOnly: true, |
| 123 | + }, |
| 124 | + }), |
| 125 | + commonjs(), |
| 126 | + wildcardExternalsPlugin(peerDependencies), |
| 127 | + moveTypescriptDeclarationsPlugin(packageRoot), |
| 128 | + ], |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +module.exports = { |
| 133 | + getRollupConfiguration, |
| 134 | +}; |
0 commit comments