-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add support for JSX #264
Comments
Looks like Monaco doesn't support JSX right now. I'm a little bit confusing cause Monaco support Typescript and Javascript Syntax,and they both came from monaco-typescript;But if now I want to implement JSX Support based on Monaco API, seems like I have to write those tokenize provider and auto complete things again(include Javascript support) ? Looks like a bit duplicate works. I don't know if I get wrong with something... |
Spent some time trying to get this working and it seems like monaco-typescript doesn't expose a few options that might enable JSX support. Don't have enough experience with the codebase to create a pull request, but I'll document what I found incase it helps someone else - or if @alexandrudima or someone else who regularly works on monaco could give some help, I could give this a shot later in the week. Looking through monaco-typescript, it seems like https://github.com/Microsoft/monaco-typescript/blob/master/src/monaco.contribution.ts has a ScriptKind enum defined for JSX and TSX support, but it doesn't look like diagnostic or compiler options takes ScriptKind as a parameter. According to https://github.com/Microsoft/monaco-typescript/blob/master/lib/typescriptServices.d.ts , there seems to be a ScriptKind and a LanguageVariant which could be set to support JSX files. LanguageVariant is used by a Scanner class which doesn't seem to be exposed via monaco-typescript. Using the existing API, I could at least disable the warnings that the diagnostics were displaying on JSX, but couldn't get syntax highlighting support (and this isn't a great solution): monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: true, // This line disables errors in jsx tags like <div>, etc.
});
// I don't think the following makes any difference
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
// jsx: 'react',
jsx: monaco.languages.typescript.JsxEmit.React,
jsxFactory: 'React.createElement',
reactNamespace: 'React',
allowNonTsExtensions: true,
allowJs: true,
target: monaco.languages.typescript.ScriptTarget.Latest,
}); Similarly, setting for monaco.languages.typescript.javascriptDefaults didn't get very far either. The other (admittedly bad) option would be to update a monarch syntax definition to add JSX support, then use that instead of using monaco-typescript: https://microsoft.github.io/monaco-editor/monarch.html (essentially start with javascript or typescript and add jsx support). I didn't even bother pursuing that since I think it would be far better to get proper react support from monaco-typescript. I think this would be a great feature to have to support editing react components in a browser. It would be great to get some support on getting it implemented - or if its already being worked on, some timeframe on implementation / testing / release (I'm not sure when monaco-editor is updated with respect to vs-code). |
@Flux159 Any Idea how VSCode does it? |
This seems to suggest with extensions we can do it, but I can't find how to add extensions in Monaco. |
Basarat got JSX/TSX working with Alm.tools. Looking into it might help. https://twitter.com/Raathigesh/status/798145470803701760 |
Had some time to look a bit more into this... unfortunately it seems like this would require some significant changes to monaco-typescript's tokenization.ts which would be better done by a maintainer of monaco-typescript. @abhishiv VSCode probably uses a typescript compiler API that monaco editor doesn't expose (look at the createScanner function in the typescript compiler - one of the arguments is languageVariant which is usually passed via a ts.sourceFile object). In monaco-typescript's tokenization.ts, the classifier internally calls createScanner but doesn't expose anything around the a sourceFile object (I don't think that monaco-typescript has a concept of a sourceFile since its based in a browser). For more info, have a look at createClassifier and createScanner in (warning: large text file) https://raw.githubusercontent.com/Microsoft/monaco-typescript/master/lib/typescriptServices.js which I believe is a single js file that contains the entire typescript compiler. Monaco doesn't support extensions as far as I know since its a subset of the code in vscode that removes things like local file handling, electron support, and extension support. @Raathigesh Basarat ended up forking monaco-typescript to get JSX support in ALM but didn't make a pull request into monaco-typescript: Looking through @basarat code, I don't think he's setting anything related to sourceFile.languageVariant anywhere and is manually parsing the JSX tokens. |
I think i have this working. It was simply a matter of ensuring there's a
|
Hey @joewood Awesome, thanks! Seems to working! But now I'm getting this Did you encounter it as well? |
Remember to set Jsx to
Also, I had to wrap the react declaration file with an ambient module definition. e.g.:
At least until somebody answers this: http://stackoverflow.com/questions/43058191/how-to-use-addextralib-in-monaco-with-an-external-type-definition One thing that I haven't tried is to set-up file URIs like a typescript project. That may solve the ambient/external module import issue. |
I worked out the solution to the external declaration file issue. The File URI solves that problem too. Here's an example that works in the playground. This should work equally well with the
|
is there any progress? |
@jasonHzq - did you not manage to get it working? |
@joewood if i want to set the value of a new component in jsx syntax,how can i make it work? |
Hi @dandingol03. Those squiggles look like it's parsing that file as JavaScript and not JSX. Did you set the filename URL as per my comment above, when you create the model? |
@joewood editor=monaco.editor.create(containerElement, {
model: monaco.editor.createModel(jsCode,"javascript",new monaco.Uri.file("./Containre.jsx")),
}); it works well,thanks.By the way,do you have any idea about navigating between modules when i click the module name.For example,there is a statement like |
Are there any official docs or references on how to implement this? |
In the meanwhile... TL;DR:For syntax recognition: monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
jsx: "react"
});
function isApplePlatform() {
return (window && window.navigator && window.navigator.platform) ?
window.navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i) ? true : false
: true;
}
monaco.editor.create(DOMElement, {
model: monaco.editor.createModel(text, "javascript",
isApplePlatform() ?
new monaco.Uri.file('./editor_name.jsx')
: new monaco.Uri('./editor_name.jsx')
);
}); For syntax highlighting: alm-tools/alm#421 |
I've worked out the solution by using prismjs as a tokenlizer to support jsx syntax highlight, see the live demo at: http://demo.rekit.org/element/src%2Ffeatures%2Fhome%2FElementPage.js/code And here is the web worker to do tokenlize: |
@supnate that second link that you posted is a 404. Do you have another link to the source code? (Also, is that project open source / on GitHub?) |
Satya wrote a blog that shows how they got JSX syntax highlighting working for snack. https://blog.expo.io/building-a-code-editor-with-monaco-f84b3a06deaf |
Besides syntax highlighting I was able to make it work perfectly, 100% type checked thanks to comments in this issue and make a HOWTO document here: https://github.com/cancerberoSgx/jsx-alone/blob/master/jsx-explorer/HOWTO_JSX_MONACO.md Thanks! |
I can't get this working, but I'm not sure what I'm doing wrong. I copied your code example. I'm using webpack to replace <> with the contents of https://cdn.jsdelivr.net/npm/@types/[email protected]/index.d.ts I get this error: "Error in event handler: ReferenceError: Component is not defined" from this code ${Component.displayName||Component.name} that's in the index.d.ts file. I don't have any file at Update: Just manually created the file path with that file in it, but that didn't seem to do anything. Any help on how to get this working? Update: Got it working by including Component from React in the file in which I was creating the editor. |
Monaco-editor can use monaco-textmate to highlight jsx import cssGrammar from 'raw-loader!./grammars/css.json.tmLanguage'
import htmlGrammar from 'raw-loader!./grammars/html.json.tmLanguage'
import tsGrammar from 'raw-loader!./grammars/TypeScriptReact.tmLanguage'
import { loadWASM } from 'onigasm'
import { Registry } from 'monaco-textmate'
import { wireTmGrammars } from './set-gammars'
let grammarsLoaded = false
export async function liftOff(monaco) {
if (grammarsLoaded) {
return
}
grammarsLoaded = true
await loadWASM(`/onigasm.wasm`) // See https://www.npmjs.com/package/onigasm#light-it-up
const registry = new Registry({
getGrammarDefinition: async (scopeName) => {
if (scopeName === 'source.css') {
return {
format: 'json',
content: cssGrammar,
}
}
if (scopeName === 'text.html.basic') {
return {
format: 'json',
content: htmlGrammar,
}
}
return {
format: 'plist',
content: tsGrammar,
}
}
})
const grammars = new Map()
grammars.set('css', 'source.css')
grammars.set('html', 'text.html.basic')
grammars.set('vue', 'text.html.basic')
grammars.set('typescript', 'source.tsx')
grammars.set('javascript', 'source.js')
await wireTmGrammars(monaco, registry, grammars)
}``` |
Use this package https://www.npmjs.com/package/monaco-jsx-syntax-highlight |
This missing JSX lang support issue has been ongoing for 7 years I would like to hear from @microsoft proper to address why this hasn't/can't be done. It seems everyone wants this, and now more than ever with WebXR VR coding exploding with all the pushes for headsets. Why is this such an issue? |
FYI, my solution was It supports languages other than JSX as well. |
Love open source for this exact reason thank you! |
That guys just made my day |
How did you use Shiki in |
Maybe I'm missing something, but why is everyone in this thread trying to implement this either by hacking textmate parsers into this, or by hacking the ast in the web workers? I definitely implemented this a few years ago using nothing but the vanilla Monaco tokenizer (#1609), although I don't know where the code for that is, so I'll have to do it again this week. Is there a reason to need to go beyond this method? |
@sdegutis Could you share the code if you figure it out? I think the issue is none of us have figured it out. |
@ansh it was a few years ago, and I don't have the code anymore, but I'm working on relearning Monarch and rewriting it based on the clues I left myself in that issue. Maybe this time MS will accept a PR in VS Code proper for out of the box JSX support, who knows. |
I got JSX working in Monaco, you can try it out at https://vanillajsx.com/ Please try to break it everyone, and let me know what looks weird, so that it's battle proofed before the PR. Thanks. |
@sdegutis how did you get it to work? mind sharing the code? |
@ansh Yeah it's just a Monaco parser: https://github.com/sdegutis/vanillajsx.com/blob/main/site/token-provider.ts |
@sdegutis Your code example runs successfully, would you mind sharing the following code? |
@sdegutis thanks |
Sent a Highlight-JSX PR. |
Here is my sample. |
I used it in March, it only supports syntax highlighting but not type hints |
Does monaco support Jsx? I couldn't find any reference or samples. Any pointers would be appreciated.
The text was updated successfully, but these errors were encountered: