desktop-native-package-policy.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const SUPPORTED_DESKTOP_PLATFORM_KEYS = [
  2. "darwin-arm64",
  3. "darwin-x64",
  4. "linux-arm64",
  5. "linux-x64",
  6. "win32-arm64",
  7. "win32-x64",
  8. ];
  9. function assertSupportedTargetPlatformKey(targetPlatformKey) {
  10. if (!SUPPORTED_DESKTOP_PLATFORM_KEYS.includes(targetPlatformKey)) {
  11. throw new Error(`不支持的桌面目标平台: ${targetPlatformKey}`);
  12. }
  13. }
  14. export function createDesktopNativePackagePrunePatterns(targetPlatformKey) {
  15. assertSupportedTargetPlatformKey(targetPlatformKey);
  16. return [
  17. // PDF 预览已经由 Vite 打进 renderer,pdfjs-dist 的 Canvas optional dependency
  18. // 只服务 Node 渲染;pnpm 跨平台安装的 8 套 Canvas native 不应带进桌面安装包。
  19. "!node_modules/@napi-rs/canvas/**",
  20. "!node_modules/@napi-rs/canvas-*/**",
  21. // Linux prebuild 会在 beforePack 复制进 node-pty;源平台包本身不属于桌面运行时。
  22. "!node_modules/@lydell/node-pty-*/**",
  23. // 桌面运行时统一使用目标 prebuild,禁止把安装机现场编译物或 ABI bin 缓存带进跨平台包。
  24. "!node_modules/node-pty/build/**",
  25. "!node_modules/node-pty/bin/**",
  26. ...SUPPORTED_DESKTOP_PLATFORM_KEYS.filter((key) => key !== targetPlatformKey).map(
  27. (key) => `!node_modules/node-pty/prebuilds/${key}/**`,
  28. ),
  29. ];
  30. }
  31. function normalizeAsarPath(path) {
  32. const normalized = path.trim().replaceAll("\\", "/");
  33. return normalized.startsWith("/") ? normalized : `/${normalized}`;
  34. }
  35. export function parseAsarListWithPackState(output) {
  36. return output
  37. .split("\n")
  38. .map((line) => line.trim())
  39. .filter(Boolean)
  40. .map((line) => {
  41. const match = /^(pack|unpack)\s*:\s*(.+)$/.exec(line);
  42. if (!match) {
  43. throw new Error(`无法解析 asar pack state: ${line}`);
  44. }
  45. return { packState: match[1], path: normalizeAsarPath(match[2]) };
  46. });
  47. }
  48. function isNativeRuntimeFile(path, targetPlatformKey) {
  49. if (/\.(?:node|dll|dylib|exe)$/i.test(path)) return true;
  50. return path === `/node_modules/node-pty/prebuilds/${targetPlatformKey}/spawn-helper`;
  51. }
  52. export function findDesktopNativePackageViolations(entries, targetPlatformKey) {
  53. assertSupportedTargetPlatformKey(targetPlatformKey);
  54. const violations = [];
  55. for (const entry of entries) {
  56. const { packState, path } = entry;
  57. if (
  58. path === "/node_modules/@napi-rs/canvas" ||
  59. path.startsWith("/node_modules/@napi-rs/canvas/") ||
  60. path.startsWith("/node_modules/@napi-rs/canvas-")
  61. ) {
  62. violations.push(`不应打包 renderer 无需的 Canvas native: ${path}`);
  63. continue;
  64. }
  65. if (path.startsWith("/node_modules/@lydell/node-pty-")) {
  66. violations.push(`不应打包仅用于准备 prebuild 的平台源包: ${path}`);
  67. continue;
  68. }
  69. if (
  70. path.startsWith("/node_modules/node-pty/build/") ||
  71. path.startsWith("/node_modules/node-pty/bin/")
  72. ) {
  73. violations.push(`不应打包安装机生成的 node-pty 产物: ${path}`);
  74. continue;
  75. }
  76. const nodePtyPrebuildMatch = /^\/node_modules\/node-pty\/prebuilds\/([^/]+)/.exec(path);
  77. if (nodePtyPrebuildMatch && nodePtyPrebuildMatch[1] !== targetPlatformKey) {
  78. violations.push(`node-pty 包含非目标平台 prebuild: ${path}`);
  79. continue;
  80. }
  81. if (isNativeRuntimeFile(path, targetPlatformKey) && packState !== "unpack") {
  82. violations.push(`native 文件仍作为 packed payload 留在 app.asar: ${path}`);
  83. }
  84. }
  85. return violations;
  86. }