Skip to content

Instantly share code, notes, and snippets.

@CodyJasonBennett
Last active October 16, 2021 19:17
Show Gist options
  • Save CodyJasonBennett/326ed82a2970b992c1045bcf4ed72c98 to your computer and use it in GitHub Desktop.
Save CodyJasonBennett/326ed82a2970b992c1045bcf4ed72c98 to your computer and use it in GitHub Desktop.
Rollup - Advanced Vercel API
// rollup.config.js (builds src/api => api -- use `rollup -c` to build)
import { join } from 'path';
import { mkdirSync, copyFileSync, statSync, readdirSync } from 'fs';
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
const API_DIR = join(process.cwd(), 'src/api');
const BUILD_DIR = join(process.cwd(), 'api');
// Copy package.json to build dir
mkdirSync(BUILD_DIR);
copyFileSync(join(process.cwd(), 'package.json'), join(BUILD_DIR, 'package.json'));
const crawlRoutes = (path, routes = []) => {
if (statSync(path).isDirectory()) {
readdirSync(path).map(file => crawlRoutes(join(path, file), routes));
} else {
routes.push(path);
}
return routes;
};
const config = crawlRoutes(API_DIR).map(route => ({
input: route,
output: {
file: route.replace(API_DIR, BUILD_DIR),
format: 'cjs',
plugins: [resolve(), babel()],
exports: 'auto',
},
}));
export default config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment