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.

Electron support for vite

See original GitHub issue

Feature Request: Electron support for vite

This is my vite.config.ts.

import { defineConfig } from 'vite';
import reactRefresh from '@vitejs/plugin-react-refresh';
import builtinModules from 'builtin-modules';
import { escapeRegExp } from 'lodash';

const externalPackages = [
  ...builtinModules,
  ...Object.keys(require('./package.json').dependencies),
  'electron',
];

export default defineConfig({
  plugins: [reactRefresh()],
  build: {
    rollupOptions: {
      output: {
        format: 'commonjs',
      },
      external: externalPackages.map(
        name => new RegExp('^' + escapeRegExp(name) + '(/.+)?$')
      ),
    },
  },
});

This will transform import * as electron from 'electron'; to const electron = require('electron');

This will work in prod mode, not in dev mode.


Is this should provide in vite or I should write a third part plugin?

At the moment I wrote a plugin by imitating the implementation of importAnalysisPlugin to do this.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
cawa-93commented, Mar 3, 2021

I do not understand this issue a bit. Converting esm syntax to cjs syntax is very simple:

https://github.com/cawa-93/vite-electron-builder/blob/2e303ab225f1f418371be4ef2de46dc2a9f63717/config/main.vite.js#L19-L22

Just set lib.formats: ["cjs"]

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create an Electron application with Vite
So yes, even if our application is finished, Electron doesn't support esm syntaxes, so we have to modify the import/export in require/module: // ......
Read more >
cawa-93/vite-electron-builder: Secure boilerplate for ... - GitHub
Secure boilerplate for Electron app based on Vite. ... for GitHub public repository, but electron-builder also supports other update distribution servers.
Read more >
electron-vite: Next Generation Electron Build Tooling
Next generation Electron build tooling based on Vite. ... The main process and preload scripts support hot reloading.
Read more >
electron-vite - npm
Electron build tooling based on Vite. Latest version: 1.0.16, last published: 13 days ago. Start using electron-vite in your project by ...
Read more >
Building an Electron App with VueJS and Vite | Cody Bontecou
With 3.5k Github Stars and about the same amount of support as electron-vue, I decided to give it a shot. Their claim is...
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