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

feat: support worker script #77

Closed
wants to merge 1 commit into from
Closed

Conversation

JE-lee
Copy link

@JE-lee JE-lee commented Sep 21, 2022

I want to usenode:worker_threads.Worker for the heavy compute cost in the main process and something like hmr of the worker script files.
similar to the main script, it will restart the electron app when developing the worker script files.

changes:

  1. add additional option of worker, similar to the preload option.
export interface Configuration {
  main: CommonConfiguration & {
    /**
     * Shortcut of `build.lib.entry`
     */
    entry: LibraryOptions['entry']
  }
  preload?: CommonConfiguration & {
    /**
     * Shortcut of `build.rollupOptions.input`
     */
    input: InputOption
  }
  worker?: CommonConfiguration & {
    input: InputOption
  }
  /**
   * Support use Node.js API in Electron-Renderer
   * @see https://github.com/electron-vite/vite-plugin-electron/tree/main/packages/electron-renderer
   */
  renderer?: Options
}
  1. add example of how to use this.(playground/usecase-in-main)

@caoxiemeihao
Copy link
Member

@JE-lee 👋
Thanks this great PR!
To support node:worker_threads we might have a better way to implementation it.

e.g.

// vite.config.ts
import electron from 'vite-plugin-elecctron'

export default {
  plugins: [electron({
    main: {
      input: {
        main: 'electron/main/index.ts',
        worker: 'electron/main/worker.ts',
      },
    },
  })],
}

usage

// electron/main/index.ts
import { Worker } from 'node:worker_threads'

const worker = new Worker(path.join(__dirname, './worker.js'))

So, we just need to add the Configuration['main']['input'] option and implement it. Do you think this is good? 😄

@JE-lee
Copy link
Author

JE-lee commented Sep 21, 2022

yes, Configuration['main']['input'] also achieves what i want.
But it will be a little confuse when putting the worker script and main script together.
It soulds like there is more than on entry of electron. 😂Maybe it doesn't matter.

@JE-lee
Copy link
Author

JE-lee commented Sep 21, 2022

Or maybe we need a new more configurable api.
something like that

export interface Configuration {
  build: Array<{
    input: string,
    output: string,
    restartWhenUpdate: boolean, // restart the electron app when saving change, like `main script` or `worker script`
    reloadWhenUpdate: boolean, // full reload the browser window, like `proload script`
  }>,
 /**
   * Support use Node.js API in Electron-Renderer
   * @see https://github.com/electron-vite/vite-plugin-electron/tree/main/packages/electron-renderer
   */
  renderer?: Options
}

@caoxiemeihao
Copy link
Member

caoxiemeihao commented Sep 21, 2022

To support node:worker_threads we might have a better way to implementation it.

In this case. Electron-Main, Preload-script and Elecctron-Renderer will use the corresponding Vite configuration to build the corresponding environment-compatible code.

How, are you interested in implementing it? :)

@caoxiemeihao
Copy link
Member

Or maybe we need a new more configurable api.

Adding additional APIs is not recommended unless Vite's configuration cannot implement it. We want to leave enought choices for users.

@caoxiemeihao caoxiemeihao mentioned this pull request Oct 9, 2022
1 task
@caoxiemeihao
Copy link
Member

Hi here! 👋
Thanks for you PR.
The feature implemented in v0.10.0 #89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants