-
-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
load monaco from node_modules #12
Comments
Hello, thank you for your feedback. Unfortunately, in the current version, it's not possible. But you can notice that there is a config file and theoretically you can load it from where you want (wherever you have access to). I just need to make it customizable. It should be something like this: import { monaco } from '@monaco-editor/react';
monaco
.config({...})
.init()
... So, you can load it for example from your public folder. I will make the |
Hi again |
I absolutely agree, so I will do it first. |
Hi @SurenAt93 |
Hi @zzeebbii, I hope it will be ready tomorrow. |
Hi guys. The new version is here. (v1.2.0). You can modify config as shown here. For downloading monaco from your filesSo, if you are using CRA, for example, you can install yarn add monaco-editor # (npm install monaco-editor) after that move/copy the minified (it's for an example) version of editor to your (CRA) public folder: mv node_modules/monaco-editor/min/vs/ ./public and now, by changing configs with the new api, you can load monaco files from your public folder: import { monaco } from '@monaco-editor/react';
monaco
.config({
urls: {
monacoLoader: '/vs/loader.js',
monacoBase: '/vs'
},
}); Please check it and let me know if it covers your cases. Thank you! |
Hi @SurenAt93, I know loading mechanism of |
Yes, of course, there is a way, to make a single bundle. But during making that very bundle we will need some extra webpack plugins, which is not "possible" in case of CRA; that's why this package/library exists. |
Actually, I am not using CRA. I am building my bundles with manual webpack configuration that's why I was asking. I can include the |
If you are not using CRA, and you can customize your webpack configs, and also you are okay with a single bundle of monaco, so, in that case, there are other solutions/packages/libraries to do that: Please find those libraries here |
@khoshrou could you please check the new version. And if it covers your case we can close the issue. |
Hi Suren |
Of course no. It will work as expected. By avoiding bundling we avoid wepack configuration. Just it. Originally monaco worked without bundling; on the official home page of monaco it works without bundles. (check the "network" section in the devtools to see more) |
I checked the network section and library is loaded from local folder |
Hi, where is the I can't seem to have this in my project. My package.json has @monaco-editor/react = "^1.2.1" |
Hello @lucid3v. Sorry, that was a typo, it should be |
Thanks! That works. I had some issues getting the import working and here is my setup for anyone else... my code(on my offline machine) is as this:
|
Your case will look like something like this. Please check it out. |
If you want a fresh copy of the latest installed
Inside const CopyWebpackPlugin = require("copy-webpack-plugin");
// in the plugin section
new CopyWebpackPlugin([
{ from: "node_modules/monaco-editor/min/vs/", to: "vs" },
//{ from: "node_modules/monaco-editor/min-maps/vs/", to: "min-maps/vs" } // source-maps
]), |
Do you say in this way we don't need to copy vs to public folder? |
I guess yes, it's about to avoid copying |
What is the difference between monaco.config({
urls: {
monacoLoader: "/vs/loader.js",
monacoBase: "/vs"
}
}); and monaco.config({
paths: {
vs: '...',
},
'vs/nls' : {
availableLanguages: {
'*': 'de',
},
},
}); |
You can use monaco.config and load it from another CDN or from you local files. |
@suren-atoyan I have tried this of course. I'm using the same cdn link that your demo, But still the same issue so I don't understand. I downgraded to use the same version as you on the demo, the 3.1.0. Same issue. The only thing I didn't tried is using local files and it's because I would like to use CDN. I did nothing special, so I'm really lost I'm just following a basic example. I have the same problem with any CDN. There is something wrong on my side. Well I will keep investigate. |
you can compare this one monaco.config({
urls: {
monacoLoader: '/vs/loader.js',
monacoBase: '/vs'
}
}); and monaco.config({
paths: {
vs: '/vs',
}
}); this one ('vs/nls' is about localization). And they are identical from a technical point of view; just the first one is deprecated in the last version (the second one is the new way to configure it). |
Is it possible to provide a reproducible snippet? If no, so, please:
|
I'm not in a CRA environment. https://create-react-app.dev/docs/making-a-progressive-web-app/ . It should be an issue about workers configuration. I have a big hole in my knowledge about workers, time to fill it. Thanks anyway and sorry for bothering you. :) |
@elisherer I see you tried to include the source maps as well. Did that work for you? I'm still getting warning saying Only could make them disappear by disabling Javascript Source Maps in chrome settings. Wonder if there is a better way. |
@martonlanga , I see it does work as I written it, new CopyWebpackPlugin({
patterns: [
{ from: "public", to: "." },
{ from: "node_modules/monaco-editor/min/vs/", to: "vs" },
!PRODUCTION && { from: "node_modules/monaco-editor/min-maps/vs/", to: "min-maps/vs" }
].filter(Boolean)
}) Maybe you are missing the initialization? monaco.config({
paths: {
vs: "/vs"
}
}); |
@elisherer Alright, thanks for your answer! Everything including init is working fine, except source-maps. I'm using Snowpack, maybe it has something to do with that. |
still can not be taken from the modules ? |
If using webpack, use |
not working |
For people looking for an electron solution.
After days searching, this worked for me.
import * as monaco from 'monaco-editor'
import Editor, { loader } from "@monaco-editor/react"
const path = window.require("path") // remember to set contextIsolation to false
function ensureFirstBackSlash(str) {
return str.length > 0 && str.charAt(0) !== "/"
? "/" + str
: str;
}
function uriFromPath(_path) {
const pathName = path.resolve(_path).replace(/\\/g, "/");
return encodeURI("file://" + ensureFirstBackSlash(pathName));
}
loader.config({
paths: {
vs: uriFromPath(
path.join(__dirname, "../node_modules/monaco-editor/min/vs")
)
},
monaco // the real solution is here, injecting monaco into loader
});
export default function App() {
return (
<Editor
height="90vh"
defaultLanguage="javascript"
defaultValue="// some comment"
/>
);
} More Info
My Electron window settings const mainWindow = new BrowserWindow({
height: 600,
width: 800,
webPreferences: {
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
nodeIntegration: true,
contextIsolation: false,
webSecurity: false
},
}); |
@NxRoot could you please instead of this: import * as monaco from 'monaco-editor'
import Editor, { loader } from "@monaco-editor/react"
const path = window.require("path") // remember to set contextIsolation to false
function ensureFirstBackSlash(str) {
return str.length > 0 && str.charAt(0) !== "/"
? "/" + str
: str;
}
function uriFromPath(_path) {
const pathName = path.resolve(_path).replace(/\\/g, "/");
return encodeURI("file://" + ensureFirstBackSlash(pathName));
}
loader.config({
paths: {
vs: uriFromPath(
path.join(__dirname, "../node_modules/monaco-editor/min/vs")
)
},
monaco // the real solution is here, injecting monaco into loader
});
export default function App() {
return (
<Editor
height="90vh"
defaultLanguage="javascript"
defaultValue="// some comment"
/>
);
} try just this: import * as monaco from 'monaco-editor'
import Editor, { loader } from "@monaco-editor/react"
loader.config({ monaco });
export default function App() {
return (
<Editor
height="90vh"
defaultLanguage="javascript"
defaultValue="// some comment"
/>
);
} Once you set |
@suren-atoyan Thank you very much, its working!
|
import * as monaco from 'monaco-editor'
import Editor, { loader } from "@monaco-editor/react"
loader.config({ monaco }); This should be part of the documentation for electron users. |
This looked like such a sweet solution, but unfortunately it did not work for me. I'm getting this error:
after trying:
As others have suggested. Any thoughts? Next step is to eject CRA and use the MonacoWebpackPlugin |
I suggest using Example const { override, addWebpackPlugin } = require("customize-cra");
const MonacoEditorWebpackPlugin = require("monaco-editor-webpack-plugin");
module.exports = {
webpack: override(
addWebpackPlugin(
new MonacoEditorWebpackPlugin({
languages: ["javascript", "json", "xml"],
})
)
)
}; |
hi @suren-atoyan After setting e.g:
|
Hi suren, thanks for making this React version of Monaco editor. it simplifies my work a lot. I can't load monaco editor using cdn due to usage scenario. So I need to load it locally. I used CRA. and processed it as you described above. But I found that import { monaco } from '@monaco-editor/react'. There seems to be an error, I sent this to import the monaco instance directly for configuration. The version I'm using is "@monaco-editor/react": "4.2.1", which is a fairly late version, and I'm not sure if there are any major differences in usage between that and the v1.2.0 version provided above. Also my project is based on TypeScript. this may be one of the influencing factors. Hope to get your reply. Thank you! This is my code: |
try import type { Monaco } from "@monaco-editor/react"; |
it means I need to switch the version to v1.2.0 and then tweak it the way you provided ? |
Hi @suren-atoyan , I've electron app where have to load files from
Could you please help me out this, thanks! |
I have the same issue in React, Please @suren-atoyan help us! |
+1 running into this as well -- @suren-atoyan any guidance? We really want to use monaco over codemirror! |
@maxhudson @arshad-yaseen could you please try to use it as an npm package instead of CDN or file://? |
Thanks for getting back to me. I was able to resolve with a bit of a "hack": https://stackoverflow.com/questions/64243893/monaco-editor-console-error-unexpected-usage-at-editorsimpleworker-loadforeignm/78822354#78822354 In our case though, using electron, we have good access to webpack, so we switched from using this react library to using the raw We opted to just mount the editor directly for now using a ref, but I know this works well for web-browser use, and have used it before, so thank you for the package! |
Hi
thank you for this awesome component, can you make it load monaco from node modules instead of cdn?
The text was updated successfully, but these errors were encountered: