chor(🎯): use Node.js package comments

This commit is contained in:
草鞋没号 2022-06-25 15:20:01 +08:00
parent e0b33df336
commit d10b5630ac

View File

@ -4,12 +4,14 @@ import { defineConfig } from 'vite-plugin-electron'
import resolve, { lib2esm } from 'vite-plugin-resolve' import resolve, { lib2esm } from 'vite-plugin-resolve'
import pkg from './package.json' import pkg from './package.json'
// @ts-ignore
const dependencies = Object.keys(pkg.dependencies || {})
const external = [ const external = [
'electron', 'electron',
...builtinModules, ...builtinModules,
// @ts-ignore // (🎯-①): For use Node.js package in Electron-Main, Preload-Script
// For use Node.js package in Electron-main, Preload-script ...dependencies,
...Object.keys(pkg.dependencies || {}),
] ]
export default defineConfig({ export default defineConfig({
@ -43,39 +45,58 @@ export default defineConfig({
}, },
}) })
export function useNodeJsInElectronRenderer() {
return resolve(
/** /**
* Here you can specify other modules * ## Why?
* 🚧 You have to make sure that your module is in `dependencies` and not in the` devDependencies`, *
* which will ensure that the electron-builder can package it correctly * Many times, many people want to use the Node.js package in Electron-Renderer, but it may not work correctly in Vite by default.
*/ * Electron-Renderer 使 Node.js Vite
{ *
// If you use the following modules, the following configuration will work * e.g.
// What they have in common is that they will return - ESM format code snippets * Let's use `serialport` as an example.
* 使 `serialport` 🌰
// ESM format string *
'electron-store': 'export default require("electron-store");', * ```js
// Use lib2esm() to easy to convert ESM * // ❌ May not work correctly in Vite by default.
// Equivalent to * import serialport, { SerialPort, SerialPortMock } from 'serialport';
/** * ```
* sqlite3: () => ` *
* const _M_ = require('sqlite3'); * At this time, we need to use `vite-plugin-resolve` to convert `serialport` to ensure that it works normally.
* 使 `vite-plugin-resolve` `serialport`
*
* e.g.
*
* ```js
* // serialport
* const _M_ = require('serialport');
* const _D_ = _M_.default || _M_; * const _D_ = _M_.default || _M_;
* export { _D_ as default } * export { _D_ as default };
* ` * export const SerialPort = _M_.SerialPort;
* export const SerialPortMock = _M_.SerialPortMock;
* ```
*
* Try to use again.
*
* ```js
* // ✅ This looks like nothing has changed, but it works normally after the `vite-plugin-resolve` converted.
* import serialport, { SerialPort, SerialPortMock } from 'serialport';
* ```
*
* 🚧 It should be noted that the Node.js package, as a dependency of the project, should be placed in `dependencies`; Unless you konw how to build them with Vite.
* Node.js `dependencies` 使 Vite
*/ */
sqlite3: lib2esm('sqlite3', { format: 'cjs' }), export function useNodeJsInElectronRenderer() {
serialport: lib2esm( const entries = dependencies.reduce((memo, moduleId) => {
const members = Object.keys(require(moduleId))
const snippet_of_cjs2esm = lib2esm(
// CJS lib name // CJS lib name
'serialport', moduleId,
// export memebers // export memebers
[ members,
'SerialPort',
'SerialPortMock',
],
{ format: 'cjs' }, { format: 'cjs' },
),
}
) )
return Object.assign(memo, { [moduleId]: snippet_of_cjs2esm })
}, {} as Parameters<typeof resolve>[0])
// (🎯-②): For use Node.js package in Electron-Renderer
return resolve(entries)
} }