Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/native. An initiative which aims to make it easy to create complex, light-weight, and perfor okikio.github.io/native
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141import kleur from "kleur";import { rollup } from "rollup";import { terser } from "rollup-plugin-terser";
import { nodeResolve } from '@rollup/plugin-node-resolve';import commonjs from "@rollup/plugin-commonjs";
import bundleSize from "rollup-plugin-bundle-size";import esbuild from "rollup-plugin-esbuild";
import { readFileSync } from "fs";import glob from 'fast-glob';
import path from "path";import { fileURLToPath } from "url";
import minimist from "minimist";
const __filename = fileURLToPath(import.meta.url);const __dirname = path.dirname(__filename);
/** * @param {string}[scope] - packages to only build (if you don't * want to build everything) * @param {string}[ignore] - packages to not build * * @returns string[] - sorted list of Package objects that * represent packages to be built. */export const getSortedPackages = async (scope = [], ignore = []) => { scope = [].concat(scope); ignore = [].concat(ignore); return (await glob(["packages"], { expandDirectories: scope.length ? scope.map(v => `${v.replace("@okikio/", "")}/package.json`) : ["package.json"], ignore: ignore.length ? ignore.map(v => `packages/${v.replace("@okikio/", "")}/package.json`) : [], deep: 2 })).map(file => ({ file: file, location: file.replace("package.json", ""), json: JSON.parse(readFileSync(file, "utf-8")) }))};
export const rollupConfig = async ({ scope, ignore } = {}) => { /** * @type {import('rollup').RollupOptions[]} */ const config = []; const packages = await getSortedPackages(scope, ignore);
packages.forEach((pkg) => { /* Absolute path to package directory */ const basePath = path.relative(__dirname, pkg.location);
let pkgObj = pkg.json;
/* Absolute path to input file */ const input = path.join(basePath, pkgObj.main);
/* "main" & "module" field from package.json file. */ const { main, module, umd: name, legacy } = pkgObj.publishConfig; // const external = Object.keys(pkgObj?.peerDependencies ?? {});
/* Push build config for this package. */ config.push({ input, // external, output: [ { file: path.join(basePath, module), exports: "named", format: "es" }, { file: path.join(basePath, main), exports: "named", format: "cjs" }, { file: path.join(basePath, legacy), exports: "named", name, format: "umd", }, ], }); });
return config;};
// Support --scope and --ignore globs if passed in via commandlineconst args = minimist(process.argv.slice(2));const configs = await rollupConfig(args);
export async function build() { for (let { input, output, external } of configs) { let title = kleur.gray(`Bundled in`); console.time(title); console.log(`\n` + `Building ${kleur.red(input)}`);
// Create a bundle const bundle = await rollup({ input, plugins: [ esbuild({ target: "es2021", // default, or 'es20XX', 'esnext' logLevel: "info",
color: true, bundle: true, minify: true, treeShaking: true,
sourcemap: true, platform: "browser", tsconfig: "./tsconfig.json", }), nodeResolve(), commonjs(), bundleSize(), terser({ ecma: 2021, compress: true, mangle: true }) ] });
// or write the bundle to disk for (let outputOption of output) { await bundle.write(outputOption); }
// closes the bundle await bundle.close(); console.timeEnd(title); }}
build();