webpack 将Monaco-Editor添加到Angular 15

cnh2zyt3  于 2023-03-23  发布在  Webpack
关注(0)|答案(1)|浏览(228)

我不能让monaco编辑器与angular 15一起工作,对于sql和autocomplete worker,
我试着按照这个线程中的步骤:https://github.com/microsoft/monaco-editor/issues/3553
并添加了所有的Webpack的东西,但我无法使它为SQL工作,这是我到目前为止实现的:
我的Webpack配置是:

const path = require('path');
    const MiniCssExtractPlugin = require('mini-css-extract-plugin');
    module.exports = {
    entry: {
        app: './index.js',
        'editor.worker': 'monaco-editor/esm/vs/editor/editor.worker.js',
        'json.worker': 'monaco-editor/esm/vs/language/json/json.worker',
        'css.worker': 'monaco-editor/esm/vs/language/css/css.worker',
        'html.worker': 'monaco-editor/esm/vs/language/html/html.worker',
        'ts.worker': 'monaco-editor/esm/vs/language/typescript/ts.worker'
    },
    output: {
        globalObject: 'self',
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader"
                ]
            },
            {
                test: /\.ttf$/,
                use: ['file-loader']
            }
        ]
    }
};

我的index.js如下:

self.MonacoEnvironment = {
    getWorkerUrl: function (moduleId, label) {
        if (label === 'json') {
            return './json.worker.bundle.js';
        }
        if (label === 'css' || label === 'scss' || label === 'less') {
            return './css.worker.bundle.js';
        }
        if (label === 'html' || label === 'handlebars' || label === 'razor') {
            return './html.worker.bundle.js';
        }
        if (label === 'typescript' || label === 'javascript') {
            return './ts.worker.bundle.js';
        }
        return './editor.worker.bundle.js';
    }
};

在我的组件中,我想使用monaco编辑器im添加以下内容:

import { AfterViewInit, Component } from '@angular/core';
    import * as monaco from 'monaco-editor';
    import { SqlAutocompleteService } from './services/autocomplete/autocomplete.service';
    ...

    editor: monaco.editor.IStandaloneCodeEditor;

    constructor(
    private autocompleteService: SqlAutocompleteService
    ) {}

    ngAfterViewInit() {
    const element = document.getElementById('editor');

    this.editor = monaco.editor.create(element, {
      value: '',
      // value: testQuery,
      language: 'sql',
      theme: 'vs'
    });

    monaco.languages.registerCompletionItemProvider('sql', {
      provideCompletionItems: (model, position) => {
        return this.autocompleteService.provideCompletionItems(model, position);
         }
        });
      }
     }

我的自动完成服务有自动完成的逻辑
我的主要问题是如何添加sql工人,因为现在我无法使它工作

t40tm48m

t40tm48m1#

您可以将这些添加到配置文件中:

const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const path = require('path');

module.exports = {
  entry: {
    'editor.worker': 'monaco-editor/esm/vs/editor/editor.worker.js',
  },
  output: {
    globalObject: 'self',
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
    ],
  },
  plugins: [
    new MonacoWebpackPlugin({
      languages: ['sql'],
    }),
  ],
};

因为这些不是SQL的默认worker,但这应该使它工作。
剩下的你可以像现在一样使用。

相关问题