Skip to content

Commit 2ce3e71

Browse files
author
Loïc Mangeonjean
committed
doc: update readme
1 parent 745b6c0 commit 2ce3e71

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

README.md

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ npm install -D @types/vscode
2626
}
2727
```
2828

29-
If you use Vite, add in your vite.config.js:
30-
```js
31-
assetsInclude: ['node_modules/vscode-oniguruma/**/*.wasm']
32-
```
33-
3429
### Why?
3530

3631
Monaco-editor is a library that is constructed using code from vscode and goes through an intense treeshaking process.
@@ -39,6 +34,58 @@ However, due to the inclusion of additional code from VSCode in this library tha
3934

4035
To **tree-mend** (to **un**treeshake it) monaco-editor, this library provides a script that will apply a patch on the local installation of monaco-editor, restoring all the code that was treeshaken during the monaco-editor build process
4136

37+
## Troubleshooting
38+
39+
### If you use Vite
40+
41+
This library uses a lot the `new URL('asset.extension', import.meta.url)` syntax which [is supported by vite](https://vitejs.dev/guide/assets.html#new-url-url-import-meta-url)
42+
43+
While it works great in `build` mode (because rollup is used), there is some issues in `watch`` mode:
44+
- import.meta.url is not replaced while creating bundles, it is an issue when the syntax is used inside a dependency
45+
- vite is still trying to inject/transform javascript assets files, breaking the code by injecting ESM imports in commonjs files
46+
47+
There are workarounds for both:
48+
49+
- We can help vite by replacing `import.meta.url` by the original module path:
50+
```typescript
51+
{
52+
...
53+
optimizeDeps: {
54+
esbuildOptions: {
55+
plugins: [{
56+
name: 'import.meta.url',
57+
setup ({ onLoad }) {
58+
// Help vite that bundles/move files in dev mode without touching `import.meta.url` which breaks asset urls
59+
onLoad({ filter: /.*\.js/, namespace: 'file' }, args => {
60+
let code = fs.readFileSync(args.path, 'utf8')
61+
code = code.replace(
62+
/\bimport\.meta\.url\b/g,
63+
`new URL('/@fs${args.path}', window.location.origin)`
64+
)
65+
return { contents: code }
66+
})
67+
}
68+
}]
69+
}
70+
}
71+
}
72+
```
73+
- we can serialize and eval the code to prevent vite from touching it:
74+
```typescript
75+
{
76+
plugins: [{
77+
// prevent vite from trying to inject code into an extension file du to an `import()` in that file
78+
name: 'hack-prevent-transform-javascript',
79+
apply: 'serve',
80+
load (source) {
81+
if (source.includes('tsserver.web.js')) {
82+
return `eval(${JSON.stringify(fs.readFileSync(source).toString('utf-8'))})`
83+
}
84+
}
85+
}]
86+
}
87+
```
88+
4289
# Usage
4390

4491
## Monaco standalone services
@@ -180,10 +227,12 @@ import { registerExtension, initialize } from 'vscode/extensions'
180227

181228
await initialize()
182229

183-
const { registerFile: registerExtensionFile, api: vscodeApi } = registerExtension(defaultThemesExtensions)
230+
const { registerFile: registerExtensionFile, getApi } = registerExtension(defaultThemesExtensions)
184231

185232
registerExtensionFile('/file.json', async () => fileContent)
186-
vscodeApi.languages.registerCompletionItemProvider(...)
233+
234+
getApi().then(vscodeApi => vscodeApi.languages.registerCompletionItemProvider(...))
235+
187236
```
188237

189238
### Default vscode extensions

0 commit comments

Comments
 (0)