简体   繁体   English

使用 esbuild 捆绑时出现意外使用

[英]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.

相关问题 Monaco-editor - 'json' 语言抛出意外的使用错误 - Monaco-editor - 'json' language throws unexpected usage error 如何修复错误:在 vue-cli-3 中为 monaco 编辑器加载外部模块的意外用法 - How to fix error: Unexpected usage loading foreign module for monaco editor in vue-cli-3 如何在 esbuild 中成功构建 monaco-editor? - How do I successfully build monaco-editor in esbuild? 使用 react-monaco-editor 生成构建时出现意外令牌错误 - Getting unexpected token error when generating build with react-monaco-editor 无法使用摩纳哥编辑器测试玩笑 - 意外令牌 - Cannot test jest with monaco editor - unexpected token 在摩纳哥编辑器中使用匹配括号时获取空值 - Getting null when using match brackets in monaco editor 一起使用诊断和悬停时将信息存储在模型中 - Store information in model when using diagnostics and hover together ngx-monaco-editor - 容器更改时无法设置布局大小(使用选项卡面板) - ngx-monaco-editor - unable to set layout size when container changes (using tab panel) 当模型更改时,摩纳哥 deltaDecorations 以 7 角消失 - Monaco deltaDecorations disappear in angular 7 when model changes 仅在所选文本稳定时运行该操作 - Run the action only when the selected text is stable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM