|
| 1 | +import typescript from '@rollup/plugin-typescript' |
| 2 | +import resolve from '@rollup/plugin-node-resolve' |
| 3 | +import commonjs from '@rollup/plugin-commonjs' |
| 4 | +import styles from 'rollup-plugin-styles' |
| 5 | +import terser from '@rollup/plugin-terser' |
| 6 | +import { exec } from 'child_process' |
| 7 | + |
| 8 | +const isWatchMode = process.env.ROLLUP_WATCH === 'true' |
| 9 | +const onSuccessCallback = process.env.ON_SUCCESS |
| 10 | + |
| 11 | +const external = [ |
| 12 | + 'react', |
| 13 | + 'react-dom', |
| 14 | + 'classnames', |
| 15 | + 'prop-types', |
| 16 | + '@ariakit/react', |
| 17 | + 'aria-hidden', |
| 18 | + 'dayjs', |
| 19 | + 'dayjs/plugin/localizedFormat', |
| 20 | + 'react-focus-lock', |
| 21 | + 'react-keyed-flatten-children', |
| 22 | + 'use-callback-ref', |
| 23 | + 'tslib', |
| 24 | +] |
| 25 | + |
| 26 | +const basePlugins = [resolve(), commonjs()] |
| 27 | + |
| 28 | +const baseStylesConfig = { |
| 29 | + autoModules: /\.module\.css$/, |
| 30 | + modules: { |
| 31 | + mode: 'local', |
| 32 | + generateScopedName: '[hash:8]', |
| 33 | + }, |
| 34 | + mode: 'extract', |
| 35 | + url: { inline: true }, |
| 36 | +} |
| 37 | + |
| 38 | +const baseTypescriptConfig = { |
| 39 | + tsconfig: './tsconfig.dist.json', |
| 40 | +} |
| 41 | + |
| 42 | +// Plugin to run a command after successful build |
| 43 | +function onSuccess(command) { |
| 44 | + return { |
| 45 | + name: 'on-success', |
| 46 | + closeBundle() { |
| 47 | + if (command) { |
| 48 | + exec(command, (error, stdout, stderr) => { |
| 49 | + if (error) { |
| 50 | + console.error(`Error executing command: ${error}`) |
| 51 | + return |
| 52 | + } |
| 53 | + if (stdout) console.log(stdout) |
| 54 | + if (stderr) console.error(stderr) |
| 55 | + }) |
| 56 | + } |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Build configurations for es/, lib/, and dist/ folders |
| 62 | +function createConfig({ |
| 63 | + format, |
| 64 | + outputDir, |
| 65 | + outputFile, |
| 66 | + preserveModules, |
| 67 | + withDeclarations, |
| 68 | + withMinification, |
| 69 | + onSuccessCommand, |
| 70 | +}) { |
| 71 | + return { |
| 72 | + input: 'src/index.ts', |
| 73 | + output: { |
| 74 | + ...(outputFile ? { file: outputFile } : { dir: outputDir }), |
| 75 | + format, |
| 76 | + sourcemap: true, |
| 77 | + ...(preserveModules && { |
| 78 | + preserveModules: true, |
| 79 | + preserveModulesRoot: 'src', |
| 80 | + assetFileNames: '[name][extname]', |
| 81 | + }), |
| 82 | + ...(format === 'cjs' && { exports: 'named' }), |
| 83 | + }, |
| 84 | + external, |
| 85 | + plugins: [ |
| 86 | + ...basePlugins, |
| 87 | + styles({ |
| 88 | + ...baseStylesConfig, |
| 89 | + ...(withMinification && { minimize: true }), |
| 90 | + }), |
| 91 | + typescript({ |
| 92 | + ...baseTypescriptConfig, |
| 93 | + compilerOptions: { |
| 94 | + outDir: outputDir || 'dist', |
| 95 | + ...(withDeclarations |
| 96 | + ? { declaration: true, declarationMap: false } |
| 97 | + : { declaration: false, declarationMap: false, declarationDir: undefined }), |
| 98 | + }, |
| 99 | + }), |
| 100 | + ...(withMinification ? [terser()] : []), |
| 101 | + ...(onSuccessCommand ? [onSuccess(onSuccessCommand)] : []), |
| 102 | + ], |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// ESM unbundled build (es/ folder) |
| 107 | +const es = createConfig({ |
| 108 | + format: 'esm', |
| 109 | + outputDir: 'es', |
| 110 | + preserveModules: true, |
| 111 | +}) |
| 112 | + |
| 113 | +// CJS unbundled build with TypeScript declarations (lib/ folder) |
| 114 | +const lib = createConfig({ |
| 115 | + format: 'cjs', |
| 116 | + outputDir: 'lib', |
| 117 | + preserveModules: true, |
| 118 | + withDeclarations: true, |
| 119 | + ...(isWatchMode && onSuccessCallback && { onSuccessCommand: onSuccessCallback }), |
| 120 | +}) |
| 121 | + |
| 122 | +// Generate dist/index.js proxy file |
| 123 | +const distIndex = { |
| 124 | + input: 'src/index.ts', |
| 125 | + external: () => true, |
| 126 | + output: { |
| 127 | + file: 'dist/index.js', |
| 128 | + format: 'cjs', |
| 129 | + }, |
| 130 | + plugins: [ |
| 131 | + { |
| 132 | + name: 'write-dist-index', |
| 133 | + generateBundle(_, bundle) { |
| 134 | + for (const fileName in bundle) { |
| 135 | + delete bundle[fileName] |
| 136 | + } |
| 137 | + |
| 138 | + this.emitFile({ |
| 139 | + type: 'asset', |
| 140 | + fileName: 'index.js', |
| 141 | + source: `'use strict' |
| 142 | +
|
| 143 | +if (process.env.NODE_ENV === 'production') { |
| 144 | + module.exports = require('./reactist.cjs.production.min.js') |
| 145 | +} else { |
| 146 | + module.exports = require('./reactist.cjs.development.js') |
| 147 | +} |
| 148 | +`, |
| 149 | + }) |
| 150 | + }, |
| 151 | + }, |
| 152 | + ], |
| 153 | +} |
| 154 | + |
| 155 | +// Bundled CJS development build (dist/ folder) |
| 156 | +const distDev = createConfig({ |
| 157 | + format: 'cjs', |
| 158 | + outputFile: 'dist/reactist.cjs.development.js', |
| 159 | +}) |
| 160 | + |
| 161 | +// Bundled CJS production build - minified (dist/ folder) |
| 162 | +const distProd = createConfig({ |
| 163 | + format: 'cjs', |
| 164 | + outputFile: 'dist/reactist.cjs.production.min.js', |
| 165 | + withMinification: true, |
| 166 | +}) |
| 167 | + |
| 168 | +export default isWatchMode ? [es, lib, distIndex] : [es, lib, distDev, distProd] |
0 commit comments