Skip to content

Commit a8def10

Browse files
Merge pull request wso2#9889 from JayaShakthi97/patch-rotation
Bundle Monaco Editor as an npm dependency
2 parents e65fb34 + 169d8fb commit a8def10

6 files changed

Lines changed: 113 additions & 88 deletions

File tree

.changeset/cyan-roses-rest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@wso2is/console": minor
3+
---
4+
5+
Bundle monaco editor as an npm dependency

apps/console/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@
132132
"i18next-xhr-backend": "^3.2.2",
133133
"js-beautify": "^1.13.0",
134134
"lodash-es": "^4.17.21",
135+
"monaco-editor": "0.36.1",
136+
"monaco-editor-webpack-plugin": "7.1.1",
135137
"mustache": "^4.2.0",
136138
"node-forge": "^0.10.0",
137139
"rc-tree": "^4.0.0-beta.2",

apps/console/src/index.tsx

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2020-2024, WSO2 LLC. (https://www.wso2.com).
2+
* Copyright (c) 2020-2026, WSO2 LLC. (https://www.wso2.com).
33
*
44
* WSO2 LLC. licenses this file to you under the Apache License,
55
* Version 2.0 (the "License"); you may not use this file except
@@ -29,6 +29,7 @@ import GlobalVariablesProvider from "@wso2is/admin.core.v1/providers/global-vari
2929
import { store } from "@wso2is/admin.core.v1/store";
3030
import OrganizationsProvider from "@wso2is/admin.organizations.v1/providers/organizations-provider";
3131
import { ContextUtils } from "@wso2is/core/utils";
32+
import * as monaco from "monaco-editor";
3233
import React, { ReactElement, useEffect, useState } from "react";
3334
import * as ReactDOM from "react-dom";
3435
import { Provider } from "react-redux";
@@ -39,36 +40,8 @@ import Theme from "./theme";
3940
// Set the runtime config in the context.
4041
ContextUtils.setRuntimeConfig(Config.getDeploymentConfig());
4142

42-
/**
43-
* TODO: Use Monaco with the webpack plugin.
44-
* {@link https://github.com/wso2-enterprise/asgardeo-product/issues/23937}
45-
*
46-
* Function to check the status of the Monaco CDN.
47-
* If the CDN is not available, the default CDN will be used.
48-
*/
49-
const checkCDNStatus = async () => {
50-
try {
51-
const response: Response = await fetch("https://cdn.jsdelivr.net/npm/monaco-editor@0.36.1/min/vs/loader.js");
52-
53-
if (response.ok) {
54-
loader.config({
55-
paths: {
56-
vs: "https://cdn.jsdelivr.net/npm/monaco-editor@0.36.1/min/vs"
57-
}
58-
});
59-
} else {
60-
loader.config({
61-
paths: {
62-
vs: "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.36.1/min/vs"
63-
}
64-
});
65-
}
66-
} catch (error) {
67-
// Use default CDN.
68-
}
69-
};
70-
71-
checkCDNStatus();
43+
// Configure monaco editor.
44+
loader.config({ monaco: monaco as any });
7245

7346
/**
7447
* Render root component with configs.

apps/console/src/public/WEB-INF/web.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@
9494
<url-pattern>/auth-spa-3.1.2.min.js</url-pattern>
9595
</servlet-mapping>
9696

97+
<servlet-mapping>
98+
<servlet-name>default</servlet-name>
99+
<url-pattern>/monaco-workers/*</url-pattern>
100+
</servlet-mapping>
101+
97102
<servlet-mapping>
98103
<servlet-name>default</servlet-name>
99104
<url-pattern>/startup-config.js</url-pattern>

apps/console/webpack.config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import ESLintPlugin from "eslint-webpack-plugin";
2828
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
2929
import HtmlWebpackPlugin from "html-webpack-plugin";
3030
import JsonMinimizerPlugin from "json-minimizer-webpack-plugin";
31+
import MonacoWebpackPlugin from "monaco-editor-webpack-plugin";
3132
import webpack, {
3233
Configuration,
3334
RuleSetRule,
@@ -465,6 +466,14 @@ module.exports = (config: WebpackOptionsNormalized, context: NxWebpackContextInt
465466
})
466467
);
467468

469+
// Bundle only the required monaco editor languages.
470+
config.plugins.push(
471+
new MonacoWebpackPlugin({
472+
filename: "monaco-workers/[name].worker.js",
473+
languages: [ "javascript", "json", "typescript", "html", "xml", "shell" ]
474+
})
475+
);
476+
468477
// Update the existing `DefinePlugin` plugin added by NX.
469478
const existingDefinePlugin: WebpackPluginInstance =
470479
config.plugins.find((plugin: WebpackPluginInstance) => {
@@ -600,6 +609,8 @@ module.exports = (config: WebpackOptionsNormalized, context: NxWebpackContextInt
600609
];
601610

602611
config.module.rules.unshift({
612+
// Exclude monaco editor related files to avoid duplicate packing of worker.js files.
613+
exclude: /node_modules[\\/]monaco-editor[\\/]/,
603614
test: /\.worker\.(ts|js)$/,
604615
use: {
605616
loader: "worker-loader",
@@ -678,6 +689,13 @@ module.exports = (config: WebpackOptionsNormalized, context: NxWebpackContextInt
678689
priority: 20,
679690
test: /[\\/]node_modules[\\/](codemirror|js-beautify)[\\/]/
680691
},
692+
monaco: {
693+
chunks: "all",
694+
enforce: true,
695+
name: "monaco",
696+
priority: 30,
697+
test: /[\\/]node_modules[\\/](monaco-editor|@monaco-editor)[\\/]/
698+
},
681699
vendor: {
682700
chunks: "initial",
683701
enforce: true,

0 commit comments

Comments
 (0)