-
-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: external built-in modules plugin
- Loading branch information
1 parent
57a2a19
commit d98a877
Showing
3 changed files
with
43 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { builtinModules } from 'node:module'; | ||
|
||
import type { Plugin } from 'vite'; | ||
|
||
/** | ||
* `electron` and Node.js built-in modules should always be externalize. | ||
*/ | ||
export function externalBuiltins() { | ||
return <Plugin>{ | ||
name: '@electron-forge/plugin-vite:external-builtins', | ||
config(config) { | ||
const builtins = [ | ||
'electron', | ||
...builtinModules.filter((e) => !e.startsWith('_')), | ||
...builtinModules.filter((e) => !e.startsWith('_')).map((m) => `node:${m}`), | ||
]; | ||
|
||
config.build ??= {}; | ||
config.build.rollupOptions ??= {}; | ||
|
||
let external = config.build.rollupOptions.external; | ||
if (Array.isArray(external) || typeof external === 'string' || external instanceof RegExp) { | ||
external = builtins.concat(external as string[]); | ||
} else if (typeof external === 'function') { | ||
const original = external; | ||
external = function (source, importer, isResolved) { | ||
if (builtins.includes(source)) { | ||
return true; | ||
} | ||
return original(source, importer, isResolved); | ||
}; | ||
} else { | ||
external = builtins; | ||
} | ||
config.build.rollupOptions.external = external; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters