67 lines
1.8 KiB
JavaScript
67 lines
1.8 KiB
JavaScript
|
import fs from 'fs'
|
||
|
import path from 'path'
|
||
|
import typescript from '@rollup/plugin-typescript'
|
||
|
import babel from '@rollup/plugin-babel'
|
||
|
import postcss from 'rollup-plugin-postcss'
|
||
|
import tailwind from 'rollup-plugin-tailwindcss'
|
||
|
import autoprefixer from 'autoprefixer'
|
||
|
import resolve from '@rollup/plugin-node-resolve'
|
||
|
import commonjs from '@rollup/plugin-commonjs'
|
||
|
import { terser } from 'rollup-plugin-terser'
|
||
|
|
||
|
// Recursively get .tsx files from a directory
|
||
|
const getComponents = (dir) =>
|
||
|
fs.readdirSync(dir).reduce((files, file) => {
|
||
|
const name = path.join(dir, file)
|
||
|
const isDirectory = fs.statSync(name).isDirectory()
|
||
|
const isIndexFile = path.basename(name).includes('index')
|
||
|
if (isDirectory) {
|
||
|
return files.concat(getComponents(name))
|
||
|
} else if (isIndexFile && name.endsWith('.tsx')) {
|
||
|
return files.concat(name)
|
||
|
} else {
|
||
|
return files
|
||
|
}
|
||
|
}, [])
|
||
|
|
||
|
// Generate Rollup configuration for each component
|
||
|
const componentsDir = 'src/components'
|
||
|
const componentFiles = getComponents(componentsDir)
|
||
|
|
||
|
const rollupConfig = componentFiles.map((inputFile) => ({
|
||
|
input: inputFile,
|
||
|
output: [
|
||
|
{
|
||
|
dir: 'dist',
|
||
|
format: 'es',
|
||
|
sourcemap: true,
|
||
|
preserveModules: true,
|
||
|
preserveModulesRoot: 'src'
|
||
|
}
|
||
|
],
|
||
|
external: [/@babel\/runtime/, 'react', 'prop-types'],
|
||
|
plugins: [
|
||
|
resolve(),
|
||
|
commonjs(),
|
||
|
typescript(),
|
||
|
babel({
|
||
|
extensions: ['.js', '.jsx', '.ts', '.tsx'],
|
||
|
babelHelpers: 'runtime',
|
||
|
plugins: ['@babel/plugin-transform-runtime']
|
||
|
}),
|
||
|
postcss({
|
||
|
plugins: [autoprefixer()],
|
||
|
sourceMap: true,
|
||
|
extract: true,
|
||
|
minimize: true
|
||
|
}),
|
||
|
tailwind({
|
||
|
input: './styles/globals.css',
|
||
|
purge: false
|
||
|
}),
|
||
|
terser()
|
||
|
]
|
||
|
}))
|
||
|
|
||
|
export default rollupConfig
|