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 use web workers using a JS entry point #9523

Open
7 tasks done
DarkGhostHunter opened this issue Aug 4, 2022 · 2 comments
Open
7 tasks done

Unable to use web workers using a JS entry point #9523

DarkGhostHunter opened this issue Aug 4, 2022 · 2 comments
Labels
feat: web workers p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@DarkGhostHunter
Copy link

Describe the bug

Using the backend integration for a web server (http://localhost:80), trying to use an external Web Worker (monaco-editor) returns 404 errors.

import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"

export default defineConfig({
    plugins: [
        vue(),
    ],
    build: {
        polyfillModulePreload: false,
        manifest: true,
        rollupOptions: {
            input: 'resources/js/app.js',
        },
    },
});
<script setup>
import {onMounted, onUnmounted, ref, defineEmits} from "vue"
import * as monaco from "monaco-editor";
import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker";
import cssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker";
import htmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker";
import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker";

self.MonacoEnvironment = {
    getWorker(_, label) {
        switch (label) {
            case 'json':
                return new jsonWorker();
            case 'css':
            case 'scss':
            case 'less':
                return new cssWorker();
            case 'html':
            case 'handlebars':
            case 'razor':
                return new htmlWorker();
            case 'typescript':
            case 'javascript':
                return new tsWorker();
            case 'yaml':
                return new yamlWorker();
            default:
                return new editorWorker()
        }
    }
};

monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true);

const props = defineProps({
    modelValue: {
        type: String,
        default: '{"foo":"bar"}'
    },
});

const emit = defineEmits(['update:modelValue']);

const container = ref()

let instance = null

onMounted(() => {
    instance = monaco.editor.create(container.value, {
        model: monaco.editor.createModel(props.modelValue, 'json'),
        tabSize: 2,
    })

    instance.onDidChangeModelContent(() => {
        emit('update:modelValue', instance.getValue());
    });
})

onUnmounted(() => {
    instance?.dispose()
})
</script>

<template>
    <div ref="container"></div>
</template>

Loading the developer server, with the web server already running on port 80:

npm run dev

VITE v3.0.4  ready in 1392 ms

➜  Local:   http://localhost:5173/
➜  Network: use --host to expose

Once done, the browser opens at http://localhost. The network tab shows it 404 for loading the web worker:

HTTP 1.0 GET http://localhost/node_modules/monaco-editor/esm/vs/editor/editor.worker.js?type=module&worker_file

404

If I try adding the worker manually by replacing return new EditorWorker() with:

return new Worker(
    "http://localhost:5173/node_modules/monaco-editor/esm/vs/editor/editor.worker.js?type=module&worker_file"
)

Chrome will report that the script in http://localhost:5173 cannot be accessed by http://localhost.

Reproduction

https://github.com/DarkGhostHunter/bug-vite-no-web-worker

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (4) x64 Intel(R) Core(TM) i5 CPU 750  @ 2.67GHz
    Memory: 851.92 MB / 7.97 GB
  Binaries:
    Node: 17.2.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.15.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.77), ChromiumDev (105.0.1336.2)
  npmPackages:
    @vitejs/plugin-vue: ^3.0.0 => 3.0.1
    vite: ^3.0.0 => 3.0.4

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

related #13680

@sapphi-red
Copy link
Member

Same workaround with #13680 would work: #13680 (comment)

@sapphi-red sapphi-red added p2-edge-case Bug, but has workaround or limited in scope (priority) and removed p3-minor-bug An edge case that only affects very specific usage (priority) labels Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: web workers p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

No branches or pull requests

2 participants