Skip to content

Commit

Permalink
Simplify vite config
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Apr 17, 2024
1 parent 6048592 commit e447b9e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 65 deletions.
77 changes: 12 additions & 65 deletions packages/light-client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -73,10 +22,8 @@ export default defineConfig({
validation
};
`,
},
treeshake: {
preset: "recommended",
},
},
},
},
});
})
);
78 changes: 78 additions & 0 deletions vite.base.config.ts
Original file line number Diff line number Diff line change
@@ -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",
},
},
},
});
}
2 changes: 2 additions & 0 deletions vitest.base.browser.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit e447b9e

Please sign in to comment.