pinojs/pino

Bundling to `ESM` using `vite`

Closed this issue · 1 comments

I wanted to share my working config bundling a CLI tool to ESM using the following vite.config.ts

import { type Plugin, defineConfig } from "vite";

import packageJson from "./package.json";

const noExternal = Object.keys({
  ...packageJson.dependencies,
  ...packageJson.devDependencies
});

const pino = (): Plugin => ({
  name: "pino",
  apply: "build",
  config() {
    return {
      define: {
        globalThis: {
          __bundlerPathsOverrides: {
            "thread-stream-worker": "",
            "pino-worker": "",
            "pino-pipeline-worker": "",
            "pino-pretty": ""
          }
        }
      }
    };
  },
  renderChunk(code) {
    return code.replace("commonjsGlobal.process", "process");
  }
});

export default defineConfig({
  plugins: [pino()],
  build: {
    target: "esnext"
  },
  ssr: {
    noExternal
  }
});

package.json

{
  "type": "module",
  "scripts": {
    "dev": "vite-node --watch src/index.ts",
    "build": "vite build --ssr src/index.ts",
    "start": "node dist/index.js"
  }
  ...
}

Creating the logger

import pino from "pino";
import pinoPretty from "pino-pretty";

const logger = pino({ level, base: null }, pinoPretty({ sync: true }));

Notes:

  • doesn't work when configuring pino-pretty as transport (needs to be imported)
  • doesn't require shipping any additional files
  • might only work for simple stdout logging (didn't test logging to file)

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.