question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating a Worker inside a plugin doesn't trigger the worker proccessing

See original GitHub issue

Describe the bug

This code

export default new Worker(new URL('./test.js', import.meta.url))

in a file works at it integrates the ./test.js file into the vite build proccess.

But having a plugin wich returns this code at the load hook doesn’t work.


The reproduction include is minimal exept for vite-plugin-inspect as it helped me to debug it…


There are some use cases for this (just a refrence so you see this would make things more easy):

For example my plugin vite-plugin-comlink uses a custom worker-build setup for this. But I can’t rewrite it with the 2.8 features as I need to create the worker code in a plugin.

Reproduction

https://github.com/mathe42/worker-test

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 AMD FX(tm)-8320 Eight-Core Processor
    Memory: 4.44 GB / 11.96 GB
  Binaries:
    Node: 16.14.2 - ~\AppData\Local\Volta\tools\image\node\16.14.2\node.EXE
    Yarn: 1.22.18 - ~\AppData\Local\Volta\tools\image\yarn\1.22.18\bin\yarn.CMD
    npm: 8.5.0 - ~\AppData\Local\Volta\tools\image\node\16.14.2\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (100.0.1185.44)
  npmPackages:
    vite: ^2.9.2 => 2.9.5

Used Package Manager

pnpm

Logs

vite:config bundled config file loaded in 154.22ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:optimized-deps',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite-plugin-inspect',
  vite:config     'ex',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:client-inject',
  vite:config     'vite:import-analysis'
  vite:config   ],
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   configFile: 'C:/Users/Sebastian/worker-test/vite.config.js',
  vite:config   configFileDependencies: [ 'C:/Users/Sebastian/worker-test/vite.config.js' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     server: {}
  vite:config   },
  vite:config   root: 'C:/Users/Sebastian/worker-test',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: 'C:\\Users\\Sebastian\\worker-test\\public',
  vite:config   cacheDir: 'C:\\Users\\Sebastian\\worker-test\\node_modules\\.vite',
  vite:config   command: 'serve',
  vite:config   mode: 'development',
  vite:config   isWorker: false,
  vite:config   isProduction: false,
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'development', DEV: true, PROD: false },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) {},
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   },
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object], [Object],
  vite:config       [Object]
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   }
  vite:config } +14ms

  vite v2.9.5 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 438ms.

  vite:deps scanning for dependencies... { timestamp: true } +0ms
  > Inspect: http://localhost:3000/__inspect/

  vite:deps Crawling dependencies using entries:
  vite:deps   C:/Users/Sebastian/worker-test/index.html +0ms
  vite:time 0.88ms / +0ms
  vite:resolve 1.62ms /main.js -> C:/Users/Sebastian/worker-test/main.js +0ms
  vite:deps Scan completed in 87.96ms: {} +48ms
  vite:deps dependencies found:  { timestamp: true } +91ms
  vite:deps ✨ optimized dependencies unchanged { timestamp: true } +8ms
  vite:resolve 1.88ms C:\Users\Sebastian\worker-test\node_modules\.pnpm\vite@2.9.5\node_modules\vite\dist\client\client.mjs -> C:/Users/Sebastian/worker-test/node_modules/.pnpm/vite@2.9.5/node_modules/vite/dist/client/client.mjs +0ms
  vite:resolve 2.50ms /@vite/client -> C:/Users/Sebastian/worker-test/node_modules/.pnpm/vite@2.9.5/node_modules/vite/dist/client/client.mjs +0ms
  vite:load 3.55ms [fs] /@vite/client +0ms
  vite:resolve 1.78ms C:\Users\Sebastian\worker-test\node_modules\.pnpm\vite@2.9.5\node_modules\vite\dist\client\env.mjs -> C:/Users/Sebastian/worker-test/node_modules/.pnpm/vite@2.9.5/node_modules/vite/dist/client/env.mjs +26ms
  vite:resolve 2.15ms @vite/env -> C:/Users/Sebastian/worker-test/node_modules/.pnpm/vite@2.9.5/node_modules/vite/dist/client/env.mjs +0ms
  vite:resolve 1.48ms /node_modules/.pnpm/vite@2.9.5/node_modules/vite/dist/client/env.mjs -> C:/Users/Sebastian/worker-test/node_modules/.pnpm/vite@2.9.5/node_modules/vite/dist/client/env.mjs +2ms
  vite:import-analysis 11.46ms [1 imports rewritten] node_modules\.pnpm\vite@2.9.5\node_modules\vite\dist\client\client.mjs +0ms
  vite:transform 19.37ms /@vite/client +0ms
  vite:time 37.38ms /@vite/client +213ms
  vite:load 9.06ms [fs] /node_modules/.pnpm/vite@2.9.5/node_modules/vite/dist/client/env.mjs +37ms
  vite:import-analysis 0.08ms [no imports] node_modules\.pnpm\vite@2.9.5\node_modules\vite\dist\client\env.mjs +19ms
  vite:transform 1.35ms /node_modules/.pnpm/vite@2.9.5/node_modules/vite/dist/client/env.mjs +16ms
  vite:time 9.85ms /node_modules/.pnpm/vite@2.9.5/node_modules/vite/dist/client/env.mjs +17ms
The request url "C:/" is outside of Vite serving allow list.

- C:/Users/Sebastian/worker-test

Refer to docs https://vitejs.dev/config/#server-fs-allow for configurations and more details.

  vite:time 4.27ms ..\..\.. +396ms

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
sapphi-redcommented, Apr 18, 2022

I got it. import.meta.url was replaced by this feature.

Vite will replace __filename, __dirname, and import.meta.url in CommonJS and TypeScript config files. Using these as variable names will result in an error: https://vitejs.dev/config/#config-file-resolving

This config will work.

import { defineConfig } from 'vite'
import inspect from 'vite-plugin-inspect'

export default defineConfig({
    plugins: [
        inspect(),
        {
            name: 'ex',
            enforce: 'pre',
            resolveId(id) {
                if(id === ':example') return id
            },
            load(id) {
                if(id!==':example') return

                const importMeta = 'import.meta'
                return `
                    export default new Worker(new URL('./test.js', ${importMeta}.url))
                `
            }
        }
    ]
})

But it is a bit confusing. package.json does not have type: module so vite.config.js should be treated as CommonJS. Contrary to it, vite.config.js is written in ESM, it seems it import.meta.url gets replaced.

1reaction
mathe42commented, Apr 18, 2022

Oh yes it works THANKS!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot plug-ins - Power Apps - Microsoft Learn
This error simply means that the worker process running your plug-in code crashed. The reason it crashed may be your plug-in, but it...
Read more >
Web Worker not working when importing module, even while ...
The worker is supposed to read an image and do various things. The app was created using create-react-app . Currently I have. webpack.config.js...
Read more >
Multi Process Workers - Fluentd
Some plugins do not work on multi-process workers. For example, tail input does not work because in_tail cannot be implemented with multi process....
Read more >
Gerrit Triggers stop triggering builds and the queue builds up
The Gerrit queue begins to build. No builds are started from Gerrit Triggers. The log messages do indicate that the queue has stopped...
Read more >
Using plugins - Chrome Developers
Workbox offers the following official plugins for use in your service worker: BackgroundSyncPlugin : If a network request should fail, this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found