alex8088 / electron-vite

Next generation Electron build tooling based on Vite 新一代 Electron 开发构建工具,支持源代码保护
https://electron-vite.org
MIT License
3.57k stars 153 forks source link

fix: don't handle file ids that begin with a null byte #530

Closed jcarrus closed 5 months ago

jcarrus commented 5 months ago

Description

Rollup has a convention of prefixing some paths with a null byte (\0). Most plugins should just ignore files prefixed with the null byte, and this PR adds the proper handling to the electron-vite asset plugin.

Additional context

This bug is seen in issues like #524 where there's an error:

[vite:node-asset] Could not load C:/Users/krystian/Desktop/test/main/node_modules/test-module/build/package/module.node?commonjs-proxy (imported by main/node_modules/test-module/index.js): The argument 'path' must be a string or Uint8Array without null bytes. Received '\\x00C:/Users/krystian/Desktop/test/main/node_modules/test-module/build/package/module.node'
TypeError [PLUGIN_ERROR]: Could not load C:/Users/krystian/Desktop/test/main/node_modules/test-module/build/package/module.node?commonjs-proxy (imported by main/node_modules/test-module/index.js): The argument 'path' must be a string or Uint8Array without null bytes. Received '\\x00C:/Users/krystian/Desktop/test/main/node_modules/test-module/build/package/module.node'
    at open (node:internal/fs/promises:586:10)
    at Object.readFile (node:internal/fs/promises:1025:20)
    at Object.load (file:///C:/Users/krystian/Desktop/test/node_modules/electron-vite/dist/chunks/lib-iT7MOERj.mjs:634:47)
    at Object.handler (file:///C:/Users/krystian/Desktop/test/node_modules/vite/dist/node/chunks/dep-BKbDVx1T.js:68900:19)
    at file:///C:/Users/krystian/Desktop/test/node_modules/rollup/dist/es/shared/node-entry.js:19774:40
    at async PluginDriver.hookFirstAndGetPlugin (file:///C:/Users/krystian/Desktop/test/node_modules/rollup/dist/es/shared/node-entry.js:19674:28)
    at async file:///C:/Users/krystian/Desktop/test/node_modules/rollup/dist/es/shared/node-entry.js:18845:33
    at async Queue.work (file:///C:/Users/krystian/Desktop/test/node_modules/rollup/dist/es/shared/node-entry.js:19884:32)

As noted in Rollups "conventions" page:

If your plugin uses 'virtual modules' (e.g. for helper functions), prefix the module ID with \0. This prevents other plugins from trying to process it.

image

https://rollupjs.org/plugin-development/#conventions

In this case, the electron-vite plugin is not using virtual modules, but if other plugins are using it, electron-vite should not try to process the file.

Similar handling is done in vite's built-in asset plugin:

https://github.com/vitejs/vite/blob/a826a08736075049842c8addff66fe385008572a/packages/vite/src/node/plugins/asset.ts#L173-L182

What is the purpose of this pull request?

Before submitting the PR, please make sure you do the following

alex8088 commented 5 months ago

@jcarrus Thanks, it will be merged after testing without problems.