diff --git a/CHANGELOG.md b/CHANGELOG.md index e73df63..06a6ad2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -## 0.13.5 (2023-03-22) +## 0.13.6 (2023-03-24) + +- 562aa20 refactor: explicitly specify the module platform + +## 0.13.5 (2023-03-23) - efaf706 refactor: `options.optimizer` -> `options.optimizeDeps` - 82bc2f7 refactor: postinstall generation built-in module diff --git a/README.md b/README.md index 61248c2..504235b 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,27 @@ readFile('foo.txt') ipcRenderer.on('event-name', () => {/* something */}) ``` +3. Using the third-part C/C++ package in the Renderer process. + +```js +import renderer from 'vite-plugin-electron-renderer' + +export default { + plugins: [ + renderer({ + nodeIntegration: true, + optimizeDeps: { + resolve(args) { + if (args.path === 'serialport') { + return { platform: 'node' } // specify as `node` platform + } + }, + }, + }), + ], +} +``` + ## API *(Define)* `renderer(options: RendererOptions)` @@ -85,6 +106,18 @@ export interface RendererOptions { * @default false */ nodeIntegration?: boolean + /** + * Pre-Bundling modules for Electron Renderer process. + */ + optimizeDeps?: { + /** + * Explicitly tell the Pre-Bundling how to work. + */ + resolve?: (args: import('esbuild').OnResolveArgs) => + | void + | { platform: 'browser' | 'node' } + | Promise + } } ``` diff --git a/examples/quick-start/vite.config.ts b/examples/quick-start/vite.config.ts index d53f9eb..b976531 100644 --- a/examples/quick-start/vite.config.ts +++ b/examples/quick-start/vite.config.ts @@ -11,6 +11,13 @@ export default defineConfig({ renderer({ // Enables use of Node.js API in the Renderer-process nodeIntegration: true, + optimizeDeps: { + resolve(args) { + if (args.path === 'serialport') { + return { platform: 'node' } + } + }, + }, }), ], build: { diff --git a/src/optimizer.ts b/src/optimizer.ts index d503e5f..9caf50c 100644 --- a/src/optimizer.ts +++ b/src/optimizer.ts @@ -5,16 +5,17 @@ import libEsm from 'lib-esm' import { electronBuiltins } from './utils' const cjs_require = createRequire(import.meta.url) -const electronPackageCjsNamespace = 'electron:package-cjs' +const electronPlatformNodeNamespace = 'electron:platform-node' const bareImport = /^[\w@].*/ export interface optimizerOptions { /** * Explicitly tell the Pre-Bundling how to work. - * - * - `false` Vite's default Pre-Bundling will be used. */ - resolve?: (args: import('esbuild').OnResolveArgs) => { type: 'commonjs' | 'module' } | false | void | Promise<{ type: 'commonjs' | 'module' } | false | void> + resolve?: (args: import('esbuild').OnResolveArgs) => + | void + | { platform: 'browser' | 'node' } + | Promise } export default function optimizer(options: optimizerOptions = {}, nodeIntegration?: boolean): VitePlugin { @@ -23,7 +24,6 @@ export default function optimizer(options: optimizerOptions = {}, nodeIntegratio config(config) { config.optimizeDeps ??= {} config.optimizeDeps.esbuildOptions ??= {} - config.optimizeDeps.esbuildOptions.platform ??= 'node' config.optimizeDeps.esbuildOptions.plugins ??= [] config.optimizeDeps.esbuildOptions.plugins.push(esbuildPlugin(options)) }, @@ -72,58 +72,20 @@ export function esbuildPlugin(options: optimizerOptions): EsbuildPlugin { return } - // ---- Try to detect what type a module is ---- - let moduleType: 'commonjs' | 'module' | undefined - let packageJson: string | undefined - try { - packageJson = cjs_require.resolve(`${id}/package.json`) - } catch { } - if (packageJson) { - const json = cjs_require(packageJson) - if (json.type) { - // { "type": "module" } - moduleType = json.type === 'module' ? 'module' : 'commonjs' - } else if (json.module) { - // { "module": "main.mjs" } - moduleType = 'module' - } else if (json.exports) { - if (json.exports.import) { - // { "exports": { "import": "main.mjs" } } - moduleType = 'module' - } else { - for (const _export of Object.values>(json.exports)) { - if (_export.import) { - // { "exports": { ".": { "import": "main.mjs" } } } - moduleType = 'module' - break - } - } - } - } - moduleType ??= 'commonjs' - } - - const userType = await resolve?.(args) - if (userType === false) { - // Use Vite's default Pre-Bundling - return - } - if (userType && typeof userType === 'object') { - moduleType = userType.type - } + // TODO: Auto-detect `node` platform - // Only `cjs` modules, especially C/C++ npm-pkg, `es` modules will be use Vite's default Pre-Bundling - if (moduleType === 'commonjs') { + const resolved = await resolve?.(args) + if (resolved?.platform === 'node') { return { path: id, - namespace: electronPackageCjsNamespace, + namespace: electronPlatformNodeNamespace, } } }) build.onLoad({ filter: /.*/, - namespace: electronPackageCjsNamespace, + namespace: electronPlatformNodeNamespace, }, async ({ path: id }) => { const { exports } = libEsm({ exports: Object.getOwnPropertyNames(cjs_require(id)) })