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

Whitelist for 'Module "..." has been externalized for...' warning #19289

Closed
4 tasks done
sevenc-nanashi opened this issue Jan 26, 2025 · 5 comments
Closed
4 tasks done

Comments

@sevenc-nanashi
Copy link

sevenc-nanashi commented Jan 26, 2025

Description

I have an electron app that has helpers/logger.ts, a logger which is usable and imported from both electron and browser.
The file is like this:

let electronLog;

export function log(message: string) {
  if (isBrowser) {
    console.log(message);
  } else if (isElectron) {
    if (!electronLog) {
      electronLog = import("electron-log/main");
    }
    electronLog.then((log) => {
      log.log(message)
    });
  }
}

In this module, electron-log, a Node.js only package, is imported dynamically, and I know electron-log and its dependent Node.js modules are not used on browser.

But vite still warns about that Node.js modules are externalized.

Suggested solution

It would be great if there is option (or possible directive) to mute that warning.

// By option (vite.config.ts)
export default defineConfig({
  resolve: {
    expectNodeModule: ["electron-log"]
  },
})
// By directive
electronLog = /** vite:expect-node-module */ import("electron-log");

Alternative

Or mute all externalization warning (But this is scary)

Additional context

N/A

Validations

@sevenc-nanashi sevenc-nanashi changed the title Whitelist for "Module externalized for..." warning Whitelist for 'Module "..." has been externalized for...' warning Jan 26, 2025
Copy link

Hello @sevenc-nanashi. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs reproduction will be closed if they have no activity within 3 days.

@sevenc-nanashi
Copy link
Author

This is not a bug, but a feature request.

@hi-ogawa
Copy link
Collaborator

The reproduction of the warning would help a lot to understand the current situation and discuss the solution 🙏

@sevenc-nanashi
Copy link
Author

https://github.com/sevenc-nanashi/vite-feature-request-mute-node-warning

I made an example.

Let's say:

  • there is an application logic that is required from both renderer and electron,
    • isEven of myLogic.ts in that example
  • the logic depends on another code that depends on platform,
    • log of log.ts in that example
  • and that code uses dynamic import to switch whether to import node specific modules.
    • electron-log/main in that example

@hi-ogawa
Copy link
Collaborator

I think the underlying issue is expected to be dealt with in a different way than suppressing the warning.

I would imagine that ideally electron renderer build should tree shake out main process specific logic entirely and this can by normally achieved by build-time define. I'm not sure if vite-plugin-electron provides such mechanism, but doing this seems to work.

// vite.config.ts
export default defineConfig({
  plugins: [
    electron({
      main: {
        // Shortcut of `build.lib.entry`
        entry: "electron/main.ts",
        vite: {
          define: {
            '__ELECTRON_IS_NODE__': 'true'
          }
        }
      },
    }),
  ],
  define: {
    '__ELECTRON_IS_NODE__': 'false'
  },
});

// src/log.ts
export function log(message: string) {
  if (__ELECTRON_IS_NODE__) {
    if (!electronLog) {
      electronLog = import("electron-log/main");
    }
    electronLog.then(({ default: log }) => {
      log.log(message);
    });
    return;
  }
  console.log(message);
}

I'm closing this feature request since the suggested solution is not likely what Vite core needs to provide. I think this kind of needs can be solved at framework levels by providing a way/convention to branch out the code based on the environment.

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

No branches or pull requests

2 participants