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

V0.10.0 #89

Merged
merged 42 commits into from Oct 9, 2022
Merged

V0.10.0 #89

merged 42 commits into from Oct 9, 2022

Conversation

caoxiemeihao
Copy link
Member

@caoxiemeihao caoxiemeihao commented Oct 9, 2022

0.10.0 (2022-10-09)

Break!

This is a redesigned version of the API(Only 3 APIs). Not compatible with previous versions!

export type Configuration = {
  /**
   * Shortcut of `build.lib.entry`
   */
  entry?: import('vite').LibraryOptions['entry']
  /**
   * Triggered when Vite is built.  
   * If passed this parameter will not automatically start Electron App.  
   * You can start Electron App through the `startup` function passed through the callback function.  
   */
  onstart?: (this: import('rollup').PluginContext, startup: (args?: string[]) => Promise<void>) => void
  vite?: import('vite').InlineConfig
}

In the past few weeks, some issues have been mentioned in many issues that cannot be solved amicably. So I refactored the API to avoid design flaws. But despite this, the new version will only be easier rather than harder.

For example, some common problems in the following issues.

Multiple entry files is not support #86

Thanks to Vite@3.2.0's lib.entry supports multiple entries, which makes the configuration of the new version very simple. So the vite-plugin-electron@0.10.0 requires Vite at least v3.2.0.

e.g.

import electron from 'vite-plugin-electron'

// In plugins option
electron({
  entry: [
    'electron/entry-1.ts',
    'electron/entry-2.ts',
  ],
})

// Or use configuration array
electron([
  {
    entry: [
      'electron/entry-1.ts',
      'electron/entry-2.ts',
    ],
  },
  {
    entry: 'foo/bar.ts',
  },
])

require is not defined #48, #87

vite-plugin-electron-renderer will change output.format to cjs format by default(This is because currently Electron@21 only supports CommonJs), which will cause the built code to use require to import modules, if the user nodeIntegration is not enabled in the Electron-Main process which causes the error require is not defined to be thrown.

vite-plugin-electron-renderer@0.10.0 provides the nodeIntegration option. It is up to the user to decide whether to use Node.js(CommonJs).

e.g.

import renderer from 'vite-plugin-electron-renderer'

// In plugins option
renderer({
  nodeIntegration: true,
})

Use Worker in Electron-Main or Electron-Renderer #77, #81

You can see 👉 examples/worker

  • Use Worker in Electron-Main

    e.g. This looks the same as multiple entry

    import electron from 'vite-plugin-electron'
    
    // In plugins option
    electron({
      entry: [
        'electron/main.ts',
        'electron/worker.ts',
      ],
    })
    
    // In electron/main.ts
    new Worker(path.join(__dirname, './worker.js'))
  • Use Worker in Electron-Renderer

    e.g.

    import renderer, { worker } from 'vite-plugin-electron-renderer'
    
    export default {
      plugins: [
        renderer({
          // If you need use Node.js in Electron-Renderer process
          nodeIntegration: true,
        }),
      ],
      worker: {
        plugins: [
          worker({
            // If you need use Node.js in Worker
            nodeIntegrationInWorker: true,
          }),
        ],
      },
    }

TODO

  • There is no way to differentiate between Preload-Scripts, which will cause the entire Electron App to restart after the preload update, not the Electron-Renderer reload.

@caoxiemeihao
Copy link
Member Author

@astoilkov @jooy2 Hi here! 👋
v0.10.0 is coming, seems reasonable?

@jooy2
Copy link

jooy2 commented Oct 10, 2022

Thanks for the quick update!
I'll try it as soon as Vite 3.2.0 is released. I can't seem to try it right now because of a Dependency conflict.
(This also happens in Vite 3.2.0-beta0.)

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: myProject@1.8.5
npm ERR! Found: vite@3.1.6
npm ERR! node_modules/vite
npm ERR!   dev vite@"^3.1.6" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer vite@">=3.2.0" from vite-plugin-electron@0.10.0
npm ERR! node_modules/vite-plugin-electron
npm ERR!   dev vite-plugin-electron@"0.10.0" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.       
npm ERR!
npm ERR! See C:\Users\user\AppData\Local\npm-cache\eresolve-report.txt for a full rep
ort.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\user\AppData\Local\npm-cache\_logs\2022-10-10T04_47_39_548Z-deb
ug-0.log

For more detail, add `--debug` to the command

@astoilkov
Copy link
Contributor

The change to cjs format

What happens with await import('./path') calls? What are they transformed to and are they still async?

Worker change

Is it going to be possible to create a new Worker with URL and type: 'module'?

new Worker(new URL('./worker', import.meta.url), {
  type: 'module'
})

@caoxiemeihao
Copy link
Member Author

caoxiemeihao commented Oct 10, 2022

Thanks for the quick update!
I'll try it as soon as Vite 3.2.0 is released. I can't seem to try it right now because of a Dependency conflict.
(This also happens in Vite 3.2.0-beta0.)

vite-plugin-electron@0.10.0 requires at least Vite@3.2.0

@caoxiemeihao
Copy link
Member Author

The change to cjs format

If you need to use cjs format then you need to set option nodeIntegrationInWorker: true

image

@astoilkov
Copy link
Contributor

Sorry, I didn't understand that, but no worries, I will check it myself.

@caoxiemeihao
Copy link
Member Author

The change to cjs format

These will be the default behavior of Vite.

  • nodeIntegration: true will change output.format='cjs'
await import('./path')

await Promise.resolve(require('./path'))
  • nodeIntegration: false
await import('./path')

await import('./path')

Worker change

{ type: 'module' } has no effect on Vite's build behavior.

@caoxiemeihao
Copy link
Member Author

caoxiemeihao commented Oct 11, 2022

@astoilkov Hi! 👋 My good friend.

export default {
  plugins: [
    renderer({
+     resolve(dependencies) {
+       return dependencies
+     },
      nodeIntegration: true,
    }),
  ],
  worker: {
    plugins: [
      worker({
+       resolve(dependencies) {
+         return dependencies
+       },
        nodeIntegrationInWorker: true,
      }),
    ],
  },
}

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

3 participants