Skip to content

bin.js

ee-bin: v5.0.0

ee-core: v5.0.0

开发环境使用的各种命令及工具配置。

文件位置

bash
./cmd/bin.js

配置说明

javascript
/**
 * ee-bin 配置
 * 仅适用于开发环境
 */
module.exports = {
  /**
   * 命令:ee-bin dev
   * 
   * 开发模式服务配置
   */
  dev: {
    // frontend:前端服务
    // 说明:该配置的意思是,进入 frontend 目录,执行 npm run dev
    // 运行后的服务为 http://localhost:8080 
    // 如果 protocol 属性为 'file://' 那么不会执行命令,项目直接加载 indexPath 对应的文件。
    frontend: {
      directory: './frontend', // frontend 目录
      cmd: 'npm',              // 命令
      args: ['run', 'dev'],    // 参数
      protocol: 'http://',     // 协议:'http://' 'file://'
      hostname: 'localhost',   // hostname
      port: 8080,              // port
      indexPath: 'index.html'  // 'file://'协议时有效,入口文件
      force: false,            // 强制加载前端服务
      sync: false,            // (非常住进程才能使用) 是否串行执行命令
    },
    // electron:主进程服务
    // 说明:该配置的意思是,在根目录,执行 electron . --env=local
    electron: {
      directory: './',
      cmd: 'electron',
      args: ['.', '--env=local'], // --env: local|prod; '--color=always' 控制台颜色
      // 是否监听文件变化,如果监听,那么每次文件变化都会重新执行命令
      watch: true,     
      delay: 0,                // 延迟启动时间,单位:毫秒          
      loadingPage: '/public/html/loading.html', // 如果前端启动时间过长,可先加载一个loading页
      sync: false,            // (非常住进程才能使用) 是否串行执行命令
    }
  },

  /**
   * 构建
   * 命令:ee-bin build
   * 说明:用来构建可执行程序、前端、主进程、go、或者自定义命令
   * 
   * 可执行程序
   * 举例:ee-bin build --cmds=win64      构建exe
   * 
   * 前端
   * 举例:ee-bin build --cmds=frontend (构建 前端 dist资源)
   * 
   * 主进程 ./electron
   * 举例:ee-bin build --cmds=electron (构建 主进程./electron目录代码)
   * 
   * 自定义
   * 举例:ee-bin build --cmds=go_w (构建 go windows平台程序)
   * 举例:ee-bin build --cmds=go_m (构建 go macos平台程序)
   * 举例:ee-bin build --cmds=go_l (构建 go linux平台程序)
   */
  build: {
    // 构建 前端代码,根据实际 frontend 项目配置
    frontend: {
      directory: './frontend',
      cmd: 'npm',
      args: ['run', 'build'],
    },
    // 构建 主进程代码 ./electron
    electron: {
      /**
      * 构建方式
      * 'bundle' - 用 esbuild 打包成单文件(默认)
      * 'copy'   - 原样复制 electron/ 目录,不做打包(不推荐使用)
      */
      bundleType: 'bundle',
      /**
      * 开发者自定义 external 包
      * 框架已内置: ee-core, ee-bin, electron, better-sqlite3, proxy-agent, pino-roll, pino-pretty
      * 如果你的项目引入了无法打包的库(如 native 模块),在此添加包名
      * 示例: external: ['sharp', 'node-gyp']
      */
      external: [],
      /**
      * Source map 配置(用于断点调试)
      * true | 'inline'  - 源码嵌入 main.js,DevTools/VS Code 开箱即用(开发推荐)
      * 'external'       - 生成 main.js.map 文件,生产环境可单独删除
      * false            - 不生成 sourcemap(默认:dev→inline,prod→off)
      */
      sourcemap: false,
      /**
      * 压缩代码
      * true  - 压缩空白、标识符、语法
      * false - 不压缩(默认)
      */
      minify: false,
      /**
      * 移除 console 和 debugger 语句(生产环境推荐)
      * 示例: drop: ['console', 'debugger']
      */
      drop: [],
      /**
      * 压缩时保留原始函数/类名(便于定位错误)
      * true  - 保留名称(minify 时推荐开启)
      * false - 不保留(默认)
      */
      keepNames: false,
      /**
      * 第三方库 license 注释处理
      * 'inline'  - 注释内联到每个文件
      * 'eof'     - 注释移到文件末尾
      * 'none'    - 移除所有注释(默认)
      */
      legalComments: 'none',
      /**
      * 自定义全局常量(编译时替换)
      * 示例: define: { 'process.env.MY_VERSION': '"1.0.0"' }
      */
      define: {},
      /**
      * 额外复制 electron/ 下的目录或文件到输出目录(不打包进 main.js,保持目录结构)
      * 框架已内置复制: jobs/, preload/bridge.js(不可移除)
      * 智能处理: .ts/.js/.mts/.cts/.tsx/.jsx 源码会被编译成 Node 可直接 require 的 CJS .js
      *           (bundle:false,相对导入和 ee-core/* 仍保留为运行时 require);
      *           其它文件(如 .json、图片)原样复制
      * 适用场景: 静态资源,或不打包进 main.js、但需在运行时被 require()/child_process.fork() 加载的源码
      * 目录示例: copy: ['assets']        → electron/assets/ → public/electron/assets/(原样复制)
      *           copy: ['workers']       → 编译 electron/workers/ → public/electron/workers/
      * 文件示例: copy: ['data/db.json']   → electron/data/db.json → public/electron/data/db.json(原样复制)
      *           copy: ['scripts/task.ts']→ 编译 electron/scripts/task.ts → public/electron/scripts/task.js
      */
      copy: [],
      /**
      * 输出格式
      * 'cjs' - CommonJS(推荐,Electron 主进程最稳定的方式)
      * 'esm' - ES Module(需确保业务代码兼容 ESM,如 controller/service 的 registry require() 调用)
      * 默认: 'cjs'
      */
      format: 'cjs',
    },
    // 可执行程序,可以根据 electron-builder 官方自行扩展
    // 构建32位exe
    win32: {
      cmd: 'electron-builder',
      directory: './',
      args: ['--config=./cmd/builder.json', '-w=nsis', '--ia32'],
    },
    // 构建64位exe
    win64: {
      cmd: 'electron-builder',
      directory: './',
      args: ['--config=./cmd/builder.json', '-w=nsis', '--x64'],
    },
    // 构建便携包
    win_e: {
      cmd: 'electron-builder',
      directory: './',
      args: ['--config=./cmd/builder.json', '-w=portable', '--x64'],
    },
    // 构建压缩包
    win_7z: {
      cmd: 'electron-builder',
      directory: './',
      args: ['--config=./cmd/builder.json', '-w=7z', '--x64'],
    },
    // 构建macOS inter芯片 dmg
    mac: {
      cmd: 'electron-builder',
      directory: './',
      args: ['--config=./cmd/builder-mac.json', '-m'],
    },
    // 构建macOS M芯片 dmg
    mac_arm64: {
      cmd: 'electron-builder',
      directory: './',
      args: ['--config=./cmd/builder-mac-arm64.json', '-m', '--arm64'],
    },
    // 构建 exe
    linux: {
      cmd: 'electron-builder',
      directory: './',
      args: ['--config=./cmd/builder-linux.json', '-l=deb', '--x64'],
    },
    // 构建 exe
    linux_arm64: {
      cmd: 'electron-builder',
      directory: './',
      args: ['--config=./cmd/builder-linux.json', '-l=deb', '--arm64'],
    },    
    //(可选)go
    go_w: {
      directory: './go',
      cmd: 'go',
      args: ['build', '-o=../build/extraResources/goapp.exe'],
    },
    go_m: {
      directory: './go',
      cmd: 'go',
      args: ['build', '-o=../build/extraResources/goapp'],
    },
    go_l: {
      directory: './go',
      cmd: 'go',
      args: ['build', '-o=../build/extraResources/goapp'],
    }
    // (可选)python
    python: {
      directory: './python',
      cmd: 'python',
      args: ['./setup.py', 'build'],
    },
  },

  /**
   * 移动资源
   * 
   * 命令:ee-bin move
   * 说明:移动前端、go、配置等静态资源到指定目录,供生产环境使用。支持文件、目录。
   * 
   * 举例1:ee-bin move --flag=frontend_dist (移动 前端 dist资源)
   * 举例2:ee-bin move --flag=go_static,go_config,go_package,go_images (移动 go 资源)
   */
  move: {
    frontend_dist: {
      src: './frontend/dist',
      dest: './public/dist'
    },
    go_static: {
      src: './frontend/dist',
      dest: './go/public/dist'
    },
    go_config: {
      src: './go/config',
      dest: './go/public/config'
    },
    go_package: {
      src: './package.json',
      dest: './go/public/package.json'
    },
    go_images: {
      src: './public/images',
      dest: './go/public/images'
    },
    python_dist: {
      src: './python/dist',
      dest: './build/extraResources/py'
    },
  },

  /**
   * 预发布模式(prod)
   * 命令:ee-bin start
   * 说明:该配置的意思是,在根目录,执行 electron . --env=prod
   */
  start: {
    directory: './',
    cmd: 'electron',
    args: ['.', '--env=prod']
  },

  /**
   * 加密
   * 命令:ee-bin encrypt
   * 说明:多种加密功能,支持对主进程和 前端代码的加密,保护您的源码安全。
   */  
  encrypt: {
    frontend: {
      // 支持: none - 不加密, confusion - 压缩混淆加密
      type: 'none',
      // 文件匹配
      // ! 符号开头的意思是过滤
      files: [
        './public/dist/**/*.(js|json)',
      ],
      // 需要加密的文件后缀,只支持js
      fileExt: ['.js'],
      cleanFiles: ['./public/dist'],
      // 混淆加密配置
      confusionOptions: {
        // 压缩成一行
        compact: true, 
        // 删除字符串文字并将其放置在一个特殊数组中     
        stringArray: true,
        // 对stringArray的所有字符串文字进行编码,值:'none' | 'base64' | 'rc4'
        stringArrayEncoding: ['none'],
        // 注入死代码,注:影响性能
        deadCodeInjection: false, 
        // 死代码比例
        deadCodeInjectionThreshold: 0.1,  
        // 调用的所有参数都可以提取到不同的对象中
        stringArrayCallsTransform: true,
        // 数字转表达式
        numbersToExpressions: true,
        target: 'browser',
      },
      // 在混淆期间抑制javascript混淆器的宣传横幅
      silent: true,
    },
    electron: {
      // none - 不加密
      // confusion - 压缩混淆加密
      // bytecode - 字节码加密
      // strict - 先混淆加密,然后字节码加密 
      type: 'confusion',
      files: [
        './public/electron/**/*.(js|json)',
      ],
      fileExt: ['.js'],
      cleanFiles: ['./public/electron'],
      // bridge.js 是 BrowserWindow 的 preload 脚本,必须保持可读格式,
      // 因此专门列出,使用混淆加密而非字节码加密。
      specificFiles: ['./public/electron/preload/bridge.js'],
      // Electron 的包入口必须保持 main.js;在 bytecode/strict 模式下,
      // 它会变成一个微小的 bytenode 加载器外壳,用于 require main.jsc。
      entryFiles: ['./public/electron/main.js'],
      encryptDir: './',
      // 混淆加密时隐藏 javascript-obfuscator 的宣传横幅
      silent: false,
      confusionOptions: {
        compact: true,      
        stringArray: true,
        stringArrayEncoding: ['rc4'],
        deadCodeInjection: false,
        stringArrayCallsTransform: true,
        numbersToExpressions: true,
        target: 'node',
      },
      bytecodeOptions: {
        electron: true,
      },   
    }
  },

  /**
   * 执行自定义命令
   * ee-bin exec
   */
  exec: {
    node_v: {
      directory: './',
      cmd: 'node',
      args: ['-v'],
      sync: false,  // (非常住进程才能使用) 是否同步执行命令
    },
    // 单独调试,air 实现 go 热重载
    go: {
      directory: './go',
      cmd: 'air',
      args: ['-c=config/.air.toml' ],
    },
    // windows 单独调试,air 实现 go 热重载 
    go_w: {
      directory: './go',
      cmd: 'air',
      args: ['-c=config/.air.windows.toml' ],
    },    
    // 单独调试,以基础方式启动 go
    go2: {
      directory: './go',
      cmd: 'go',
      args: ['run', './main.go', '--env=dev','--basedir=../', '--port=7073'],
    },    
    python: {
      directory: './python',
      cmd: 'python',
      args: ['./main.py', '--port=7074'],
      stdio: "inherit", // ignore
    },
  },  
};