Monaco Editor 在使用 Webpack 的情况下不会加载代码图标

Yoi*_*aka 3 javascript webpack monaco-editor

在使用 Webpack 的情况下尝试使用Monaco Editor时,不会加载代码图标。我使用了 monaco-editor-webpack-plugin 并按照说明进行操作,但目前我的测试页面中的 Monaco Editor 实例上看不到任何图标。

在此输入图像描述

我是否忘记加载代码了?

您可以通过以下方式重现此问题:https ://github.com/yoichiro/monaco-editor-test/tree/main

索引.html

<!doctype html>
<html lang="en">
<head>
  <style>
    .source-editor {
                width: 640px;
                height: 320px;
        }
  </style>
</head>
<body>
  <div class="source-editor"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

索引.ts

import * as monaco from 'monaco-editor';

window.addEventListener('load', () => {
        monaco.editor.create(document.querySelector('.source-editor'));
});
Run Code Online (Sandbox Code Playgroud)

webpack.config.js

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const MonacoEditorWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: './src/index.ts',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js',
    clean: true,
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: 'ts-loader',
      },
      {
        test: /\.scss$/i,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
          },
          {
            loader: 'css-loader',
          },
          {
            loader: 'sass-loader',
            options: {
              sassOptions: {
                outputStyle: 'expanded',
              },
            },
          },
        ],
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.ttf$/,
        use: ['file-loader'],
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js', 'scss', 'html'],
  },
  plugins: [
    new MonacoEditorWebpackPlugin(),
    new MiniCssExtractPlugin({
      filename: 'style.css',
    }),
    new HtmlWebpackPlugin({ template: './src/index.html' }),
  ],
  devtool: 'source-map',
  watchOptions: {
    ignored: /node_modules/,
  },
  devServer: {
    static: './build',
    // open: true,
    watchFiles: ['src/**/*'],
  },
};
Run Code Online (Sandbox Code Playgroud)

Yoi*_*aka 8

从 Webpack 5 开始,我们需要使用资产模块而不是加载器。

也就是说,以下代码适用于 Webpack 5:

{
  test: /\.ttf$/,
  type: 'asset/resource'
}
Run Code Online (Sandbox Code Playgroud)

如果使用 Webpack 4 及更低版本,以下代码将起作用:

{
  test: /\.ttf$/,
  use: ['file-loader']
}
Run Code Online (Sandbox Code Playgroud)

  • webpack 5 的第一个选项仍然对我不起作用 - 同样的错误。 (3认同)