egoist / vue-monaco

MonacoEditor component for Vue.js
MIT License
690 stars 127 forks source link

How exactly do you pass in a require property for electron #8

Closed jeeftor closed 5 years ago

jeeftor commented 6 years ago

I'm trying to use this in electron. I've downloaded the Monaco sample project (https://github.com/Microsoft/monaco-editor-samples/tree/master/electron-amd) and that runs fine in electron - but I'm at a loss how to make your component work in electron.

I understand I can pass in a require property - but I'm not exactly clear how to do that.

I assume I use it like this:

<template>
  <monaco-editor
    class="editor"
    v-model="code"
    require="???????"
    language="javascript">
  </monaco-editor>
</template>

Do I construct my own amdRequire and pass it in?

Do i do something like :require=COMPUTED_PROPERTY

Any chance you know how to make this work in electron? I've tried a few routes and got confused.

Sample Code from Microsoft

<!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>
egoist commented 6 years ago

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>