Skip to content

Commit

Permalink
Merge pull request #44 from electron-vite/v0.13.6
Browse files Browse the repository at this point in the history
V0.13.6
  • Loading branch information
caoxiemeihao authored Mar 24, 2023
2 parents 9a923e9 + f5c5e0f commit c513a7c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 49 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand All @@ -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<void | { platform: 'browser' | 'node' }>
}
}
```

Expand Down
7 changes: 7 additions & 0 deletions examples/quick-start/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
58 changes: 10 additions & 48 deletions src/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void | { platform: 'browser' | 'node' }>
}

export default function optimizer(options: optimizerOptions = {}, nodeIntegration?: boolean): VitePlugin {
Expand All @@ -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))
},
Expand Down Expand Up @@ -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<Record<string, string>>(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)) })

Expand Down

0 comments on commit c513a7c

Please sign in to comment.