React-Monaco-Editor:找不到由monaco-editor-webpack-plugin生产的工作者

时间:2019-08-06 13:44:22

标签: reactjs webpack monaco-editor

我正在尝试将monaco编辑器集成到现有的react应用程序中。我按照(react-monaco-editor)的安装步骤进行操作,但是没有得到突出显示或自动完成打字稿的语法,这与FAQ一致,就好像我没有设置monaco-editor-webpack-plugin一样。

我已经设置了插件。我可以看到monaco-editor-webpack-plugin沿着我的app.js拖放文件,所以我认为它可以正常工作: enter image description here

似乎我的问题是react-monaco-editor试图从错误的位置提取这些文件。如果我加载页面并打开“网络”标签,则可以看到以下内容:

enter image description here

有两个问题:

1-路径是我系统上的完整路径,但应该是相对路径-例如

http://127.0.0.1/dist/app/secured/49.js

2-“安全”和“ 49.js”(secured49.js)之间以及其他数字文件之间都没有斜杠,但是editor.worker.js带有斜杠(怪异)。

所以我希望回答的问题是-react-monaco-editor如何确定它将从中提取这些文件的路径,并且有什么方法可以影响该路径?

任何帮助或建议将不胜感激!下面的一些代码片段对您有所帮助。


package.json:

"monaco-editor-webpack-plugin": "^1.7.0",
"react-monaco-editor": "^0.26.2",

weback.config.js

'use strict';

const path = require('path');
const webpack = require('webpack');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

process.noDeprecation = true;

const babel = {
  loader: 'babel-loader',
  options: {
    presets: ['@babel/preset-env', '@babel/preset-react'],
    plugins: [
      ['@babel/plugin-proposal-decorators', { 'legacy': true }],
      ['jsx-control-statements'],
      '@babel/plugin-proposal-function-bind',
      ['@babel/plugin-proposal-decorators', { 'legacy': true }],
      ['@babel/plugin-proposal-class-properties', { 'loose': true }]
    ],
    compact: true
  }
};

const config = {
  entry: './index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.js'
  },
  mode: 'development', // Change to production on checkin
  context: settings.app(),
  resolve: {
    modules: [
      settings.app(),
      path.join(settings.project(), 'node_modules'),
      path.join(__dirname, 'node_modules')
    ],
    symlinks: true,
    extensions: ['.js', '.jsx', '.json', '.css', 'scss', 'less']
  },
  resolveLoader: {
    modules: [
      path.join(settings.project(), 'node_modules'),
      path.join(__dirname, 'node_modules')
    ],
    symlinks: true
  },
  module: {
    rules: [
      {
        test: /.(js|jsx)?$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                '@babel/preset-env',
                '@babel/preset-react'
              ],
              plugins: [
                '@babel/plugin-proposal-function-bind',
                ['jsx-control-statements'],
                ['@babel/plugin-proposal-decorators', {'legacy': true}],
                ['@babel/plugin-proposal-class-properties', { 'loose': true }]
              ],
              compact: true
            }
          }
        ],
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192
            }
          }
        ]
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.scss$/,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'sass-loader'
          }
        ]
      },
      {
        test: /\.less$/,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'less-loader'
          }
        ]
      },
      {
        test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 100000,
            mimetype: 'application/font-woff'
          }
        }]
      },
      {
        test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 100000,
            mimetype: 'application/octet-stream'
          }
        }]
      },
      {
        test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 100000,
            mimetype: 'application/image/svg+xml'
          }
        }]
      },
      {
        test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
        use: 'file-loader'
      },
      {
        test: /-font\.otf(\?.+)?$/,
        use: [
          'file-loader?name=fonts/[name].[ext]'
        ]
      }
    ]
  },
  devtool: 'source-map',
  node: {
    console: true,
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  },
  plugins: [
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
    new CaseSensitivePathsPlugin(),
    new MonacoWebpackPlugin()
  ]
};

module.exports = config;

index.js

import { observer } from 'mobx-react';
import React, { Component } from 'react';
import MonacoEditor from 'react-monaco-editor';

@observer
export default class TSEditor extends Component {
  constructor(props) {
    super(props);
    this.state = {
      code: 'var name : string = "kyle";'
    };

    this.editorDidMount = this.editorDidMount.bind(this);
    this.onChange = this.onChange.bind(this);
  }

  onChange(newValue, e) {
    console.log('onChange', newValue, e);
  }

  editorDidMount(editor, monaco) {
    console.log('editorDidMount', editor);
    editor.focus();
  }

  render() {
    const code = this.state.code;
    const options = {
      selectOnLineNumbers: true
    };

    return (
      <MonacoEditor
        language="typescript"
        theme="vs-dark"
        value={code}
        options={options}
        onChange={this.onChange}
        editorDidMount={this.editorDidMount}
      />
    );
  }
}

2 个答案:

答案 0 :(得分:0)

事实证明这是由于我的webpack配置的输出部分中的publicPath错误导致的问题。

对于其他遇到类似问题的人,请在webpack配置中添加publicPath,以提供Web Worker JS文件的正确位置。

答案 1 :(得分:0)

我的情况……这在 webpack.config.js 中起到了作用

  devServer: {
    https: true,
    port: 3000,
    publicPath: "/dist/"
  },
  ...
  ..
  .
  plugins: [
      ...
      ..
      .
      new MonacoWebpackPlugin({
        publicPath: "/dist/"
      })
  ]

可用选项记录在 https://github.com/Microsoft/monaco-editor-webpack-plugin#options

相关问题