Home > Enterprise >  Monaco Editor doesn't load codicons in case of using Webpack
Monaco Editor doesn't load codicons in case of using Webpack

Time:04-02

When trying using enter image description here

Did I forget something to load the codicons?

You can reproduce this issue with: https://github.com/yoichiro/monaco-editor-test/tree/main

index.html

<!doctype html>
<html lang="en">
<head>
  <style>
    .source-editor {
                width: 640px;
                height: 320px;
        }
  </style>
</head>
<body>
  <div ></div>
</body>
</html>

index.ts

import * as monaco from 'monaco-editor';

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

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/**/*'],
  },
};

CodePudding user response:

Since Webpack 5, we need to use Asset Modules instead of loaders.

That is, the following code works on Webpack 5:

{
  test: /\.ttf$/,
  type: 'asset/resource'
}

If using Webpack 4 and lower, the following code will work:

{
  test: /\.ttf$/,
  use: ['file-loader']
}
  • Related