Skip to content

Commit f32c51f

Browse files
authored
Update README with create-react-app and YAML help (#8188)
I ran into some trouble using the code editor in my project. The main problems were that I had to do extra work that the docs didn't cover because I bootstrapped my project with `create-react-app` and I needed YAML highlighting support which required an extra plugin. This doc update should make it easier for anyone who's in the same boat :)
1 parent 8d5d8dc commit f32c51f

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

packages/react-code-editor/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,62 @@ Install peer deps
5353
```
5454

5555
To properly install the library `monaco-editor-webpack-plugin` be sure to follow the [plugin instructions](https://github.com/microsoft/monaco-editor/tree/main/webpack-plugin)
56+
57+
#### With create-react-app Projects
58+
If you created your project with `create-react-app` you'll have some extra work to do, or you wont have syntax highlighting. Using the webpack plugin requires updating your webpack config, which `create-react-app` abstracts away. You can `npm eject` your project, but you may not want to do that. To keep your app set up in the `create-react-app` style but to get access to your webpack config you can use `react-app-rewired`.
59+
60+
First, install `react-app-rewired` as a development dependency:
61+
```sh
62+
$ npm install -D react-app-rewired
63+
```
64+
65+
Next, replace all of the `react-script` references in your `package.json` `scripts` section with `react-app-required`:
66+
```json
67+
"scripts": {
68+
"start": "react-app-rewired start",
69+
"build": "react-app-rewired build",
70+
"test": "react-app-rewired test",
71+
"eject": "react-app-rewired eject"
72+
}
73+
```
74+
75+
Next, create a `config-overries.js` file at the root of your project and add the following:
76+
77+
```javascript
78+
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
79+
80+
module.exports = function override(config, env) {
81+
config.plugins.push(new MonacoWebpackPlugin({
82+
languages: ['json', 'yaml', 'shell']
83+
}));
84+
return config;
85+
}
86+
```
87+
88+
Note: You should change the `languages` array based on your needs.
89+
90+
You can now start your app with `npm start` and syntax highlighting should work.
91+
92+
#### Enable YAML Syntax Highlighting
93+
The Monaco editor doesn't ship with full YAML support. You can configure your code editor with `Languages.yaml` but there will be no highlighting, even i you have the webpack plugin working correctly. To enable YAML support you need to do the following:
94+
95+
First, install `monaco-yaml`:
96+
```shell
97+
$ npm install --save monaco-yaml
98+
```
99+
100+
Next, at the entrypoint of your app enable it:
101+
```javascript
102+
import { setDiagnosticsOptions } from 'monaco-yaml';
103+
104+
setDiagnosticsOptions({
105+
enableSchemaRequest: true,
106+
hover: true,
107+
completion: true,
108+
validate: true,
109+
format: true,
110+
schemas: [],
111+
});
112+
```
113+
114+
The `monaco-yaml` plugin has a lot of options so check out their docs to see what else you may want to add.

0 commit comments

Comments
 (0)