复制 esbuild 的插件 "is not a function"

Copy plugin for esbuild "is not a function"

我正在处理 esbuild 配置,并希望在出于测试目的构建项目时将文件 (manifest.json) 复制到特定文件夹。我将 copy plugin 包含在我的 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));
}

我什至可以Ctrl + Click进入VS Code中的复制功能。

然后当我 运行 它告诉我复制不是函数:

$ npm run dev

> obsidian-sample-plugin@1.1.0 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! obsidian-sample-plugin@1.1.0 dev: `node esbuild.config.mjs`
npm ERR! Exit status 1

编辑:进一步研究

我发现这个问题的复制(到剪贴板)功能产生了同样的错误:devtools console: copy is not a function while on youtube

问题是,DOM 包含另一个名为 copy 的元素,它不是函数。即使我不在此处的浏览器中,我也尝试重命名导入。

import copyIsADamnFunction from 'esbuild-plugin-copy';

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

同样的结果:

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

当我删除导入时,我得到

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

所以导入导入了一些东西。 VS Code 告诉我鼠标悬停在那个副本上是一个带有一个可选参数的函数,返回一个 esbuild.Plugin:

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

如果我写 copy. VS Code 甚至会为我提供代码补全,例如 applyargumentsbindcall,它们都属于 Function.prototypecopy怎么可能不是函数呢?

显然这是插件中的错误。应该会在实际版本中修复。

除此之外,this 线程中的某个人提供了一个对我有用的解决方法:

而不是

import copy from 'esbuild-plugin-copy';

使用

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