Skip to content
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

Unable to integrate with monaco-yaml successfully #3820

Closed
6 tasks done
JounQin opened this issue Jun 16, 2021 · 9 comments
Closed
6 tasks done

Unable to integrate with monaco-yaml successfully #3820

JounQin opened this issue Jun 16, 2021 · 9 comments

Comments

@JounQin
Copy link
Contributor

JounQin commented Jun 16, 2021

Describe the bug

image

languageFeatures.js:85 Error: Unexpected usage
    at EditorSimpleWorker.loadForeignModule (editorSimpleWorker.js:450)
    at webWorker.js:38

I don't understand what does this mean.

Reproduction

You can check https://github.com/JounQin/test/tree/monaco-yaml

Just run yarn dev

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: macOS 12.0
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 18.90 GB / 64.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.22.1 - ~/.nvm/versions/node/v12.22.1/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.12 - ~/.nvm/versions/node/v12.22.1/bin/npm
  npmPackages:
    @vitejs/plugin-vue: ^1.2.3 => 1.2.3 
    vite: ^2.3.7 => 2.3.7

Used package manager: yarn

Logs

languageFeatures.js:85 Error: Unexpected usage
    at EditorSimpleWorker.loadForeignModule (editorSimpleWorker.js:450)
    at webWorker.js:38
(anonymous)	@	languageFeatures.js:85
Promise.then (async)		
_doValidate	@	languageFeatures.js:84
onModelAdd	@	languageFeatures.js:25
fire	@	event.js:480
createModel	@	modelServiceImpl.js:324
doCreateModel	@	standaloneCodeEditor.js:322
createTextModel	@	standaloneCodeEditor.js:316
StandaloneEditor2	@	standaloneCodeEditor.js:179
(anonymous)	@	standaloneEditor.js:62
withAllStandaloneServices	@	standaloneEditor.js:49
create	@	standaloneEditor.js:61
(anonymous)	@	MonacoEditor.vue:28
async function (async)		
(anonymous)	@	MonacoEditor.vue:26
callWithErrorHandling	@	runtime-core.esm-bundler.js:154
callWithAsyncErrorHandling	@	runtime-core.esm-bundler.js:163
hook.__weh.hook.__weh	@	runtime-core.esm-bundler.js:2908
flushPostFlushCbs	@	runtime-core.esm-bundler.js:357
render	@	runtime-core.esm-bundler.js:5776
mount	@	runtime-core.esm-bundler.js:4070
app.mount	@	runtime-dom.esm-bundler.js:1288
(anonymous)	@	main.ts:5

Before submitting the issue, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Provide a description in this issue that describes the bug.
  • Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.

The webpack version works just fine.

https://github.com/JounQin/test/tree/monaco-editor

related remcohaszing/monaco-yaml#53


I don't know if it is related to vite or monaco-yaml itself for now. But it is very strange that when I change node_modules/monaco-yaml/lib/esm/yaml.worker.js for debugging, no changes is working, logs for example. So I raise this issue for tracking.

@dejour
Copy link
Contributor

dejour commented Jun 17, 2021

add following to your vite.config.ts

optimizeDeps: {
    include: ["js-yaml", "yaml-ast-parser-custom-tags"]
  }

@JounQin
Copy link
Contributor Author

JounQin commented Jun 18, 2021

@dejour Thank you very much, it's working now! But why and what is happening? How can I understand and know to add these configs by myself?

@YunYouJun
Copy link
Contributor

Did you solve it? I have seen your repository, but the version is 2.5.1.

I tried monaco-yaml 3.2.1 and vite and the problem still occurs. (only when i use setDiagnosticsOptions

import { setDiagnosticsOptions } from 'monaco-yaml')

Uncaught Error: Unexpected usage

Error: Unexpected usage
    at EditorSimpleWorker.loadForeignModule (editorSimpleWorker.js:450)
    at webWorker.js:38

@remcohaszing
Copy link

I got it working in remcohaszing/monaco-yaml#116.

@JounQin
Copy link
Contributor Author

JounQin commented Sep 18, 2021

@remcohaszing Can you try to import monaco-editor and monaco-yaml lazily?

It seems not working for me when upgrading to monaco-yaml@3.

image

export const setupMonaco = async () => {
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  if (window.monaco) {
    return window.monaco
  }

  const [{ default: EditorWorker }, { default: YamlWorker }] =
    await Promise.all([
      import('monaco-editor/esm/vs/editor/editor.worker?worker'),
      import('monaco-yaml/lib/esm/yaml.worker?worker'),
    ])

  window.MonacoEnvironment = {
    globalAPI: true,
    getWorker(_: string, label: string) {
      if (label === 'yaml') {
        return new YamlWorker()
      }
      return new EditorWorker()
    },
  }

  const monaco = await import('monaco-editor/esm/vs/editor/edcore.main')

  window.monaco = monaco

  const [, { setDiagnosticsOptions }] = await Promise.all([
    import('monaco-editor/esm/vs/basic-languages/yaml/yaml.contribution'),
    import('monaco-yaml'),
  ])

  setDiagnosticsOptions({
    enableSchemaRequest: true,
    format: true,
    validate: true,
  })

  return monaco
}

@JounQin
Copy link
Contributor Author

JounQin commented Sep 18, 2021

@remcohaszing I upgraded all deps at JounQin/test@bab5eaa for reproduction.

@remcohaszing
Copy link

I suspect you’re running into this.

You can probably try something like this:

// Load monaco-editor and all cherry-picked features.
// This way all monaco features are available in the same chunk and only a single monaco module is loaded.
// See https://github.com/microsoft/monaco-editor/blob/main/monaco-editor-samples/browser-esm-webpack-small/index.js
import 'monaco-editor/esm/vs/basic-languages/yaml/yaml.contribution'

// Expose the editor API
export { editor } from 'monaco-editor/esm/vs/editor/editor.api'
// Expose monaco-yaml
export { setDiagnosticsOptions } from 'monaco-yaml'
// Load workers.
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
import YamlWorker from 'monaco-yaml/lib/esm/yaml.worker?worker'

window.MonacoEnvironment = {
  getWorker(moduleId, label) {
    switch (label) {
      case 'editorWorkerService':
        return new EditorWorker()
      case 'yaml':
        return new YamlWorker()
      default:
        throw new Error(`Unknown label ${label}`)
    }
  },
}

export async function setupMonaco() {
  const { editor, setDiagnosticsOptions } = await import('./my-monaco')

  // Configure monaco editor.
}

@JounQin
Copy link
Contributor Author

JounQin commented Nov 16, 2021

@remcohaszing I want to load all monaco related deps lazily including worker, and did you try my reproduction?

https://github.com/JounQin/test/tree/monaco-yaml

@bluwy
Copy link
Member

bluwy commented Mar 11, 2022

I've tested this in Vite 2.8.6 and the repro seems to be working fine, so it looks like this is fixed now. Closing this, but if there's still an issue with it, feel free to let us know with an updated repro.

@bluwy bluwy closed this as completed Mar 11, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Mar 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants