From e447b9e7d7887fe57f3161e2fb7794f8b143a210 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 17 Apr 2024 18:51:15 +0200 Subject: [PATCH] Simplify vite config --- packages/light-client/vite.config.ts | 77 +++++---------------------- vite.base.config.ts | 78 ++++++++++++++++++++++++++++ vitest.base.browser.config.ts | 2 + 3 files changed, 92 insertions(+), 65 deletions(-) create mode 100644 vite.base.config.ts diff --git a/packages/light-client/vite.config.ts b/packages/light-client/vite.config.ts index 8fe172efe8b5..86ba95634cef 100644 --- a/packages/light-client/vite.config.ts +++ b/packages/light-client/vite.config.ts @@ -1,66 +1,15 @@ -import {defineConfig} from "vite"; -import {nodePolyfills} from "vite-plugin-node-polyfills"; -import topLevelAwait from "vite-plugin-top-level-await"; -import {visualizer} from "rollup-plugin-visualizer"; -import {blsBrowserPlugin} from "../../scripts/vite/plugins/blsBrowserPlugin.js"; +import {defineConfig, mergeConfig} from "vite"; +import {getBaseViteConfig} from "../../vite.base.config.js"; import pkgJSON from "./package.json"; -// TODO: Investigate why this banner is not appended to the build header. -const banner = - `/* ${pkgJSON.description}\n` + - " * \n" + - ` * Version: ${pkgJSON.version}\n` + - ` * Author: ${pkgJSON.author}\n` + - ` * License: ${pkgJSON.license}\n` + - ` * Web: ${pkgJSON.homepage}\n` + - "*/"; - -export default defineConfig({ - plugins: [ - topLevelAwait(), - blsBrowserPlugin(), - nodePolyfills({ - include: ["http", "https", "stream"], - globals: {Buffer: true, process: true}, - protocolImports: true, - }), - ...(process.env.CI ? [] : [visualizer()]), - ], - mode: "production", - appType: "custom", - esbuild: { - banner, - legalComments: "none", - }, - build: { - // "modules" refer to ['es2020', 'edge88', 'firefox78', 'chrome87', 'safari14'] - target: "modules", - outDir: "dist", - sourcemap: true, - minify: false, - manifest: "manifest.json", - ssr: false, - ssrManifest: false, - emptyOutDir: true, - lib: { - entry: "src/index.browser.ts", - formats: ["es"], - name: "LightClient", - fileName: (format) => { - if (format === "esm" || format === "es") { - return "lightclient.min.mjs"; - } else if (format === "cjs") { - return "lightclient.min.cjs"; - } else { - return `lightclient.min.${format}.js`; - } - }, - }, - rollupOptions: { - output: { - inlineDynamicImports: true, - footer: ` +export default mergeConfig( + getBaseViteConfig(pkgJSON, {libName: "LightClient", entry: "src/index.browser.ts"}), + defineConfig({ + build: { + rollupOptions: { + output: { + footer: ` globalThis.lodestar = globalThis.lodestar === undefined ? {} : globalThis.lodestar; globalThis.lodestar.lightclient = { Lightclient, @@ -73,10 +22,8 @@ export default defineConfig({ validation }; `, - }, - treeshake: { - preset: "recommended", + }, }, }, - }, -}); + }) +); diff --git a/vite.base.config.ts b/vite.base.config.ts new file mode 100644 index 000000000000..5f249ee80556 --- /dev/null +++ b/vite.base.config.ts @@ -0,0 +1,78 @@ +import {UserConfig, defineConfig} from "vite"; +import {nodePolyfills} from "vite-plugin-node-polyfills"; +import {visualizer} from "rollup-plugin-visualizer"; +import topLevelAwait from "vite-plugin-top-level-await"; +import {blsBrowserPlugin} from "./scripts/vite/plugins/blsBrowserPlugin.js"; + +export function getBaseViteConfig( + pkgInfo: { + description: string; + version: string; + author: string; + license: string; + homepage: string; + }, + {entry, libName}: {entry: string; libName: string} +): UserConfig { + // TODO: Investigate why this banner is not appended to the build header. + const banner = + `/* ${pkgInfo.description}\n` + + " * \n" + + ` * Version: ${pkgInfo.version}\n` + + ` * Author: ${pkgInfo.author}\n` + + ` * License: ${pkgInfo.license}\n` + + ` * Web: ${pkgInfo.homepage}\n` + + "*/"; + + return defineConfig({ + plugins: [ + topLevelAwait(), + blsBrowserPlugin(), + nodePolyfills({ + include: ["http", "https", "stream"], + globals: {Buffer: true, process: true}, + protocolImports: true, + }), + ...(process.env.CI ? [] : [visualizer()]), + ], + mode: "production", + appType: "custom", + esbuild: { + banner, + legalComments: "none", + }, + build: { + // "modules" refer to ['es2020', 'edge88', 'firefox78', 'chrome87', 'safari14'] + target: "modules", + outDir: "dist", + sourcemap: true, + minify: false, + manifest: "manifest.json", + ssr: false, + ssrManifest: false, + emptyOutDir: true, + lib: { + entry, + formats: ["es"], + name: libName, + fileName: (format) => { + if (format === "esm" || format === "es") { + return `${libName.toLowerCase()}.min.mjs`; + } else if (format === "cjs") { + return `${libName.toLowerCase()}.min.cjs`; + } else { + return `${libName.toLowerCase()}.min.${format}.js`; + } + }, + }, + rollupOptions: { + output: { + inlineDynamicImports: true, + }, + treeshake: { + preset: "recommended", + }, + }, + }, + }); +} diff --git a/vitest.base.browser.config.ts b/vitest.base.browser.config.ts index a07f4d842b06..edd53c406ae1 100644 --- a/vitest.base.browser.config.ts +++ b/vitest.base.browser.config.ts @@ -3,10 +3,12 @@ import {defineConfig} from "vitest/config"; const __dirname = new URL(".", import.meta.url).pathname; import {nodePolyfills} from "vite-plugin-node-polyfills"; import topLevelAwait from "vite-plugin-top-level-await"; +import { blsBrowserPlugin } from "./scripts/vite/plugins/blsBrowserPlugin"; export default defineConfig({ plugins: [ topLevelAwait(), + blsBrowserPlugin(), nodePolyfills({ include: ["buffer", "process", "util", "string_decoder", "url", "querystring", "events"], globals: {Buffer: true, process: true},