Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imports of generated files do not respect outExtension map #74

Open
kubijo opened this issue Feb 27, 2023 · 1 comment
Open

Imports of generated files do not respect outExtension map #74

kubijo opened this issue Feb 27, 2023 · 1 comment

Comments

@kubijo
Copy link

kubijo commented Feb 27, 2023

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")
};
@kubijo
Copy link
Author

kubijo commented Feb 27, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant