'Copy plugin for esbuild "is not a function"

I'm working on an esbuild configuration and want to copy a file (manifest.json) to a specific folder when I build my project for testing purposes. I included the copy plugin into my esbuild-config.mjs

import esbuild from "esbuild";
import process from "process";
import builtins from 'builtin-modules'
import copy from 'esbuild-plugin-copy';

const prod = (process.argv[2] === 'production');
    
const baseConfig = {
    // ...
};

const testVaultPluginFolder = 'test-vault/.obsidian/plugins/obsidian-sample-plugin/';
const devConfig = {
    ...baseConfig,
    outfile: testVaultPluginFolder + 'main.js',
    plugins: [
        copy({  // <- This line causes the problem
            assets: [
                { from: ['manifest.json'], to: [testVaultPluginFolder] }
            ]
        })
    ]
};

const prodConfig = {
    ...baseConfig,
    outfile: 'main.js',
};

if (prod){
    esbuild.build(prodConfig).catch(() => process.exit(1));
} else {
    esbuild.build(devConfig).catch(() => process.exit(1));
}

I can even Ctrl + Click into the copy function in VS Code.

Then when I run it it tells me that copy is not a function:

$ npm run dev

> [email protected] dev C:\Workspaces\RNSS-Sample\obsidian-sample-plugin
> node esbuild.config.mjs

file:///C:/Workspaces/.../obsidian-sample-plugin/esbuild.config.mjs:59
                copy({
                ^

TypeError: copy is not a function
    at file:///C:/Workspaces/.../obsidian-sample-plugin/esbuild.config.mjs:59:3
    at ModuleJob.run (internal/modules/esm/module_job.js:170:25)
    at async Loader.import (internal/modules/esm/loader.js:178:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `node esbuild.config.mjs`
npm ERR! Exit status 1

Edit: Further research

I found this question where the copy (to clipboard) function produced the same error: devtools console: copy is not a function while on youtube

The problem was, that the DOM contained another element named copy that was not a function. Even though I'm not in a Browser here, I tried renaming the import.

import copyIsADamnFunction from 'esbuild-plugin-copy';

plugins: [
    copyIsADamnFunction({
        assets: [
            { from: ['manifest.json'], to: [testVaultPluginFolder] }
        ]
    })
]

Same result:

TypeError: copyIsADamnFunction is not a function
    at file:///C:/Workspaces/.../obsidian-sample-plugin/esbuild.config.mjs:59:3

When I remove the import I get

ReferenceError: copy is not defined
    at file:///C:/Workspaces/.../obsidian-sample-plugin/esbuild.config.mjs:58:11

So the import imports something. And VS Code tells me on mouse over that copy is a function with one optional parameter returning a esbuild.Plugin:

(alias) copy(options?: Partial<Options>): esbuild.Plugin
import copy

If I write copy. VS Code even offers me code completion like apply, arguments, bind and call which all belong to Function.prototype. How can copy not be a function?



Solution 1:[1]

Apparently this was a bug in the plugin. It should be fixed in an actual version.

Beside this someone in this Thread provided a workaroud which worked for me:

Instead of

import copy from 'esbuild-plugin-copy';

Use

import { copy } from "esbuild-plugin-copy";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ruik