Skip to content

Commit

Permalink
Merge pull request #38 from electron-vite/v0.13.3
Browse files Browse the repository at this point in the history
V0.13.3
  • Loading branch information
caoxiemeihao authored Mar 21, 2023
2 parents caffb1e + 79d0100 commit 2df2014
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.13.3 (2023-03-21)

- de584cc fix: filter `virtual-module:`

## 0.13.2 (2023-03-21)

- 7cc1abd refactor: better module type detect
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-electron-renderer",
"version": "0.13.2",
"version": "0.13.3",
"description": "Support use Node.js API in Electron-Renderer",
"main": "index.mjs",
"types": "types",
Expand Down
16 changes: 9 additions & 7 deletions src/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { node_modules as find_node_modules } from 'vite-plugin-utils/function'
import { electronBuiltins, ensureDir } from './utils'

const cjs_require = createRequire(import.meta.url)
const electronNpmCjsNamespace = 'electron:npm-cjs'
const electronNpmPkgCjsNamespace = 'electron:npm-pkg-cjs'
const bareImport = /^[\w@].*/
const CACHE_DIR = '.vite-electron-renderer'
let node_modules_path: string
Expand Down Expand Up @@ -104,6 +104,10 @@ export function esbuildPlugin(options: optimizerOptions): EsbuildPlugin {
// https://github.com/vitejs/vite/blob/v4.2.0/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L15-L20
return
}
if (id.startsWith('virtual-module:')) {
// https://github.com/vitejs/vite/blob/v4.2.0/packages/vite/src/node/optimizer/scan.ts#L436-L438
return
}

// ---- Try to detect what type a module is ----
let moduleType: 'commonjs' | 'module' | undefined
Expand All @@ -130,6 +134,7 @@ export function esbuildPlugin(options: optimizerOptions): EsbuildPlugin {
}
}
}
moduleType ??= 'commonjs'
}

const userType = await resolve?.(args)
Expand All @@ -141,21 +146,18 @@ export function esbuildPlugin(options: optimizerOptions): EsbuildPlugin {
moduleType = userType.type
}

// Assign default value
moduleType ??= 'commonjs'

// Only `cjs` modules, especially C/C++ npm-pkg, `es` modules will be use Vite's default Pre-Bundling
if (moduleType === 'commonjs') {
return {
path: id,
namespace: electronNpmCjsNamespace,
namespace: electronNpmPkgCjsNamespace,
}
}
})

build.onLoad({
filter: /.*/,
namespace: electronNpmCjsNamespace,
namespace: electronNpmPkgCjsNamespace,
}, async ({ path: id }) => {
const { exports } = libEsm({ exports: Object.getOwnPropertyNames(cjs_require(id)) })

Expand All @@ -169,7 +171,7 @@ const __cjs_require = require;
// Especially it is a C/C++ module, this can avoid a lot of trouble.
const _M_ = __cjs_require("${id}");
${exports}
`.trim(),
`.trim(),
}
})
},
Expand Down

0 comments on commit 2df2014

Please sign in to comment.