[英]Unexpected usage when bundled using esbuild
The following simple setup works when bundled using webpack, but not on esbuild.以下简单设置在使用 webpack 捆绑时有效,但在 esbuild 上无效。 There is no issue in bundling, esbuild spits out all the files correctly, but somehow getting this error on browser.
捆绑没有问题,esbuild 正确吐出所有文件,但不知何故在浏览器上出现此错误。 Any idea about the issue?
对这个问题有任何想法吗?
index.js index.js
import * as monaco from "monaco-editor";
self.MonacoEnvironment = {
getWorkerUrl: function (moduleId, label) {
if (label === "typescript" || label === "javascript") {
return "./ts.worker.bundle.js";
}
return "./editor.worker.bundle.js";
},
};
monaco.editor.create(document.getElementById("container"), {
value: ["function x() {", '\tconsole.log("Hello world!");', "}"].join("\n"),
language: "javascript",
});
Esbuild Config构建配置
const esbuild = require("esbuild");
esbuild.build({
entryPoints: {
app: "./index.js",
"editor.worker": "monaco-editor/esm/vs/editor/editor.worker.js",
"ts.worker": "monaco-editor/esm/vs/language/typescript/ts.worker",
},
globalName: "self",
entryNames: "[name].bundle",
bundle: true,
outdir: "./dist",
loader: {
".ttf": "file",
},
});
Log From Browser Console从浏览器控制台记录
app.bundle.js:2393 Uncaught Error: Unexpected usage
Error: Unexpected usage
at EditorSimpleWorker.loadForeignModule (app.bundle.js:16294)
at app.bundle.js:17060
at app.bundle.js:2393
This line looks problematic:这条线看起来有问题:
globalName: "self",
In the browser, self
is already a built-in variable: https://developer.mozilla.org/en-US/docs/Web/API/Window/self .在浏览器中,
self
已经是一个内置变量: https://developer.mozilla.org/en-US/docs/Web/API/Window/self 。 Shadowing it could potentially be breaking Monaco.跟踪它可能会破坏摩纳哥。 Your code appears to work if you just remove that line.
如果您只是删除该行,您的代码似乎可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.