pearone / vite-plugin-monaco-editor-nls

vite plugin Monaco editor localize
30 stars 19 forks source link

(void 0) is not a function #9

Open 542154968 opened 1 year ago

542154968 commented 1 year ago

依赖

{
  "name": "computing-platform",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "sea": "vite --mode sea",
    "build": "vue-tsc --noEmit && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@element-plus/icons-vue": "^2.0.9",
    "@types/node": "^18.7.15",
    "axios": "^0.27.2",
    "blockly": "^8.0.5",
    "element-plus": "^2.2.16",
    "mavon-editor": "^3.0.0-beta",
    "monaco-editor": "^0.34.0",
    "vue": "^3.2.37",
    "vue-router": "^4.1.5"
  },
  "devDependencies": {
    "@iconify-json/ep": "^1.1.7",
    "@vitejs/plugin-vue": "^3.0.3",
    "sass": "^1.54.8",
    "sass-loader": "^13.0.2",
    "typescript": "^4.6.4",
    "unplugin-auto-import": "^0.11.2",
    "unplugin-icons": "^0.14.9",
    "unplugin-vue-components": "^0.22.4",
    "vite": "^3.0.7",
    "vite-plugin-html": "^3.2.0",
    "vite-plugin-monaco-editor-nls": "^2.0.2",
    "vue-tsc": "^0.39.5"
  }
}

配置

import { defineConfig } from "vite";
import { loadEnv } from "vite";
import zh_CN from "./src/plugins/monaco-editor/vscode-language-pack-zh-hans/main.i18n.json";

import MonacoEditorNlsPlugin, {
  esbuildPluginMonacoEditorNls,
  Languages,
} from "vite-plugin-monaco-editor-nls";

// https://vitejs.dev/config/
export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return defineConfig({
    base: "./",

    optimizeDeps: {
      /** vite 版本需要大于等于2.3.0 */
      esbuildOptions: {
        plugins: [
          esbuildPluginMonacoEditorNls({
            locale: Languages.zh_hans,
            /**
             * The weight of `localedata` is higher than that of `locale`
             */
            localeData: zh_CN.contents,
          }),
        ],
      },
    },

    plugins: [
      MonacoEditorNlsPlugin.default({
        locale: Languages.zh_hans,
        /**
         * The weight of `localedata` is higher than that of `locale`
         */
        localeData: zh_CN.contents,
      }),
    ],

  });
};

报错

image

vue-router.mjs:3441 TypeError: (void 0) is not a function
    at node_modules/monaco-editor/esm/vs/base/common/platform.js (platform.js:41:34)
    at __init (chunk-MB5FDF5C.js?v=3fbab092:9:56)
    at editorOptions.js:6:1

image

542154968 commented 1 year ago

我找到了另一种汉化的解决方案

<template>
  <div ref="divRef" style="height: 500px; width: 500px"></div>
</template>

<script lang="ts" setup>
import loader from "@monaco-editor/loader";
import * as monaco from "monaco-editor";
const divRef = ref();

loader.config({ monaco });
loader.config({
  "vs/nls": {
    // availableLanguages: { "*": "de" },
    availableLanguages: { "*": "zh-cn" },
  },
});

loader.init().then(monacoInstance => {
  monacoInstance.editor.create(divRef.value, {
    value: "321313123",
  });
});
</script>

<style lang="scss"></style>

image

liuyike98 commented 1 year ago

你这还引入monaco干嘛,走的是cdn,不信你开审查元素看看。。。我已经试过了

JackieCheung commented 1 year ago

同样的问题

dyimo commented 1 month ago

你这还引入monaco干嘛,走的是cdn,不信你开审查元素看看。。。我已经试过了

这种方法没有走cdn吧,走的本地npm