'Monaco Editor doesn't load codicons in case of using Webpack
When trying using Monaco Editor in case of using Webpack, codicons are not loaded. I used the monaco-editor-webpack-plugin and followed the instruction, but currently I can't any icons on the Monaco Editor instance in my test page.
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 class="source-editor"></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/**/*'],
},
};
Solution 1:[1]
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']
}
Solution 2:[2]
The css inserted by monaco for elements that have codicon font icons have by default font-family: inherit;
.
.codicon-find-previous-match:before {
content: '\eaa1';
}
Unless you have provided some custom icon font for codicon codes make sure you have a CSS rule that specifies the font-family provided by monaco-editor:
[class^="codicon-"], [class*="codicon-"] {
font-family: "codicon"!important;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yoichiro Tanaka |
Solution 2 | Mihai B. |