Jeef
Jeef

Reputation: 27275

Getting Monaco to work with Vuejs and electron

I'm interested in using the Monaco editor in a Vue.js backed Electron project.

Thus far:

Microsoft provides an Electron Sample (which I've run and works correctly)

There are a variety of vue.js npm repos for monaco - yet none of them seem to fully support Electron right out of the box.

The one that looks most promising is vue-monaco but I've run into issues correctly integrating it.

AMD Require?

This is the code from the Microsoft sample for using with 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>

The module I'm using allows for something like this:

<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>

I can't seem to figure out how to get the correct amdRequire variable defined in Electon + vue. I believe if i can conquer this everything else becomes simple.

The Electron FAQ mentions something about this (i think): I can not sue jQuery/RequireJS/Meteor/AngularJS in Electron

Sample Code

I put a sample project up on GitHub https://github.com/jeeftor/Vue-Monaco-Electron with the "offending" component being in ./src/renderer/components/Monaco.vue

Summary

How can I get this Monaco Editor to load correctly inside of a Vue.js component that will be run inside electron?

Thanks for any help you can offer.

Upvotes: 3

Views: 2632

Answers (1)

Papa Mufflon
Papa Mufflon

Reputation: 20010

I'm doing nearly the same, just without the extra vue-monaco component. After struggling quite a bit, I could solve the problem:

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)
}

I've just taken the electron-amd sample and adjusted it a bit. I call the loadMonacoEditor function in the components' created function.

In order to not get the Not allowed to load local resource: file:///C:/.../node_modules/monaco-editor/min/vs/editor/editor.main.css problem, you also have to set

webPreferences: {
  webSecurity: false
}

in your instance of the BrowserWindow.

Upvotes: 5

Related Questions