让摩纳哥与Vuejs和电子合作

Jee*_*eef 3 javascript amd vue.js electron monaco-editor

我有兴趣在Vue.js支持的Electron项目中使用Monaco编辑器.

迄今:

微软提供了一个电子样本(我已经运行并正常工作)

摩纳哥有各种各样的vue.js npm回购 - 但它们似乎都没有完全支持Electron开箱即用.

看起来最有希望的是vue-monaco,但我遇到了正确整合它的问题.

AMD要求?

这是Microsoft示例中与Electron一起使用的代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Monaco Editor!</title>
    </head>
    <body>
        <h1>Monaco Editor in Electron!</h1>
        <div id="container" style="width:500px;height:300px;border:1px solid #ccc"></div>
    </body>

    <script>
        // Monaco uses a custom amd loader that overrides node's require.
        // Keep a reference to node's require so we can restore it after executing the amd loader file.
        var nodeRequire = global.require;
    </script>
    <script src="../node_modules/monaco-editor/min/vs/loader.js"></script>
    <script>
        // Save Monaco's amd require and restore Node's require
        var amdRequire = global.require;
        global.require = nodeRequire;
    </script>

    <script>
        // require node modules before loader.js comes in
        var path = require('path');

        function uriFromPath(_path) {
            var pathName = path.resolve(_path).replace(/\\/g, '/');
            if (pathName.length > 0 && pathName.charAt(0) !== '/') {
                pathName = '/' + pathName;
            }
            return encodeURI('file://' + pathName);
        }

        amdRequire.config({
            baseUrl: uriFromPath(path.join(__dirname, '../node_modules/monaco-editor/min'))
        });

        // workaround monaco-css not understanding the environment
        self.module = undefined;

        // workaround monaco-typescript not understanding the environment
        self.process.browser = true;

        amdRequire(['vs/editor/editor.main'], function() {
            var editor = monaco.editor.create(document.getElementById('container'), {
                value: [
                    'function x() {',
                    '\tconsole.log("Hello world!");',
                    '}'
                ].join('\n'),
                language: 'javascript'
            });
        });
    </script>
</html>
Run Code Online (Sandbox Code Playgroud)

我正在使用的模块允许这样的事情:

<template>
    <monaco-editor :require="amdRequire" />
</template>

<script>
export default {
    methods: {
        amdRequire: window.amdRequire
        // Or put this in `data`, doesn't really matter I guess
    }
}
</script>
Run Code Online (Sandbox Code Playgroud)

我似乎无法弄清楚如何获得amdRequireElecton + vue中定义的正确变量.我相信如果我能够征服这一切,其他一切都变得简单.

电子常见问题提到了这个问题(我认为):我不能在电子中起诉jQuery/RequireJS/Meteor/AngularJS

示例代码

我在GitHub上放了一个示例项目https://github.com/jeeftor/Vue-Monaco-Electron,其中"违规"组件位于./src/renderer/components/Monaco.vue

摘要

如何让这个Monaco编辑器正确加载到将在电子内部运行的Vue.js组件?

谢谢你尽你所能的帮助.

Pap*_*lon 5

我做的差不多,只是没有额外的vue-monaco组件.在经历了相当多的努力之后,我可以解决这个问题:

function loadMonacoEditor () {
  const nodeRequire = global.require

  const loaderScript = document.createElement('script')

  loaderScript.onload = () => {
    const amdRequire = global.require
    global.require = nodeRequire

    var path = require('path')

    function uriFromPath (_path) {
      var pathName = path.resolve(_path).replace(/\\/g, '/')

      if (pathName.length > 0 && pathName.charAt(0) !== '/') {
        pathName = '/' + pathName
      }

      return encodeURI('file://' + pathName)
    }

    amdRequire.config({
      baseUrl: uriFromPath(path.join(__dirname, '../../../node_modules/monaco-editor/min'))
    })

    // workaround monaco-css not understanding the environment
    self.module = undefined

    // workaround monaco-typescript not understanding the environment
    self.process.browser = true

    amdRequire(['vs/editor/editor.main'], function () {
      this.monaco.editor.create(document.getElementById('container'), {
        value: [
          'function x() {',
          '\tconsole.log("Hello world!");',
          '}'
        ].join('\n'),
        language: 'javascript'
      })
    })
  }

  loaderScript.setAttribute('src', '../node_modules/monaco-editor/min/vs/loader.js')
  document.body.appendChild(loaderScript)
}
Run Code Online (Sandbox Code Playgroud)

我刚刚拿了电子amd样品并调整了一下.我loadMonacoEditor在组件的创建函数中调用该函数.

为了不解决Not allowed to load local resource: file:///C:/.../node_modules/monaco-editor/min/vs/editor/editor.main.css问题,您还必须设置

webPreferences: {
  webSecurity: false
}
Run Code Online (Sandbox Code Playgroud)

在你的实例中BrowserWindow.