wd-David/esbuild-plugin-pino

Imports of generated files do not respect `outExtension` map

Opened this issue · 1 comments

When I map js files to cjs by using esbuild's outExtension: { '.js': '.cjs' } option, this plugin still generates js require calls

globalThis.__bundlerPathsOverrides = {
    ...globalThis.__bundlerPathsOverrides || {},
    "thread-stream-worker": pinoBundlerAbsolutePath("./thread-stream-worker.js"),
    "pino-worker": pinoBundlerAbsolutePath("./pino-worker.js"),
    "pino-pipeline-worker": pinoBundlerAbsolutePath("./pino-pipeline-worker.js"),
    "pino/file": pinoBundlerAbsolutePath("./pino-file.js"),
    "pino-pretty": pinoBundlerAbsolutePath("./pino-pretty.js")
};

For now, I have resorted to using a rewrite hack…

import { build, type Plugin } from 'esbuild';
import esbuildPluginPino from 'esbuild-plugin-pino';
import { readFileSync, writeFileSync } from 'node:fs';

build({
    entryPoints: ['src/server.ts'],
    outExtension: { '.js': '.cjs' },
    outdir: 'build',
    //
    bundle: true,
    sourcemap: true,
    //
    format: 'cjs',
    platform: 'node',
    target: 'node19.6',
    //
    // minify: true,
    legalComments: 'none',
    define: { 'process.env.NODE_ENV': 'production' },
    plugins: [
        esbuildPluginPino({
            transports: ['pino-pretty'],
        }) as any as Plugin,
    ],
})
    .then(() => {
        // renameSync('build/server.js', 'build/server.cjs');
        let content = readFileSync('build/server.cjs', 'utf8');
        content = content.replace(
            /pinoBundlerAbsolutePath\("(.*?)\.js"\)/g,
            'pinoBundlerAbsolutePath("$1.cjs")'
        );
        writeFileSync('build/server.cjs', content);
    })
    .catch(() => process.exit(1));

…btw, you can also see that I had to type-cast the plugin because otherwise it gave me a type mismatch