蓝色天空.知识 · 实践 · 创造
我的收藏
Node.js 教程目录 38章
Node.js / LEARN BY DOING

Node.js JXcore 打包

核对历史发行版 JXcore 的安装与 jx package 两阶段打包机制,明确其已停止维护的迁移背景,并离线实测清单打包契约、CLI 参数透传与 Node.js v24 Single Executable Applications 配置。

学完这一章,你能够

  • 核对历史发行版 JXcore 的安装与 jx package 两阶段打包机制,明确其已停止维护的迁移背景,并离线实测清单打包契约、CLI 参数透传与 Node.js v24 Single Executable Applications 配置。

JXcore 历史定位、安装步骤与停止维护说明

Node.js 是开源跨平台的服务端与网络应用运行环境,而 JXcore 曾是早期社区推出的一个支持多线程执行与项目打包的 Node.js 衍生发行版(基于 Node.js v0.10 时期分支),通过解压安装包提供的 `jx` 可执行文件运行。历史上 Windows 平台从发布仓库下载 `jx_win64v8.zip` 解压,Linux/macOS 通过安装脚本 `curl ... | bash`(权限不足时加 `sudo`)部署,随后以 `jx --version` 核验版本号(如 `v0.10.32`)。需要特别说明:JXcore 官方仓库早已归档停止维护,不再适配现代 Node.js 运行时与安全更新;新项目应优先选用 Node.js 官方内置 Single Executable Applications(SEA)或 `esbuild`、`pkg` 等现代打包工具。原页 Windows 安装包下载链接未发起网络请求,本站已构建离线契约实验替代核验。

两阶段打包产物、命令执行与 Node.js v24 SEA 迁移实测

在经典 JXcore 工作流中,当项目目录包含入口脚本 `index.js`、`index.htm`、`images/`、`scripts/`、`style/` 与 `node_modules/` 等内容时,执行 `jx package index.js index` 会依次生成两个产物:中间件描述文件 `index.jxp`(记录待编译项目的完整文件清单与入口元数据)和单文件二进制包 `index.jx`(整合项目源码与资源以供客户端分发运行)。本站 Node.js v24.22.3 离线实验模拟该目录结构生成带 SHA-256 摘要的清单文件 `index.jxp`,实测校验 4 个项目文件摘要均匹配 12 位十六进制指纹且入口精确绑定 `index.js`。

运行阶段,原生 Node.js 通过 `node index.js command_line_arguments` 启动项目,而 JXcore 打包后通过 `jx index.jx command_line_arguments` 透传同样参数执行(历史文档及安装说明见 JXcore GitHub 归档仓库)。为对齐现代迁移路径,本站在 Node.js v24.22.3 上同步实测了三项核心能力:直接调用 `index.js` 精确收到透传参数 `["--mode","prod","alpha"]`;验证 Node.js 官方 SEA 配置文件 `sea-config.json`(含 `main`、`output`、`useCodeCache` 与 `assets`)并确认内置模块 `node:sea` 可正常加载(非 SEA 启动时 `sea.isSea()` 返回 `false`);将多文件源码与静态 HTML 内联为单文件 `app-bundle.mjs` 后移至独立目录执行,仍准确输出 `{"label":"hello","html":"<h1>Demo</h1>"}`。

运行历史打包清单契约、CLI 参数透传、Node v24 SEA 配置与单文件离线打包实验

含完整源码 · 141 行
展开查看收起
javascript
import { execFileSync } from 'node:child_process';
import crypto from 'node:crypto';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import sea from 'node:sea';

async function main() {
  const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'mbsky-jxcore-'));
  const projDir = path.join(tmpDir, 'sample-app');
  const distDir = path.join(tmpDir, 'dist');
  await fs.mkdir(path.join(projDir, 'images'), { recursive: true });
  await fs.mkdir(path.join(projDir, 'scripts'), { recursive: true });
  await fs.mkdir(path.join(projDir, 'style'), { recursive: true });
  await fs.mkdir(distDir, { recursive: true });

  await fs.writeFile(
    path.join(projDir, 'index.js'),
    'console.log(JSON.stringify({ args: process.argv.slice(2), entry: "index.js" }));\n',
    'utf8',
  );
  await fs.writeFile(path.join(projDir, 'index.htm'), '<h1>Demo</h1>\n', 'utf8');
  await fs.writeFile(path.join(projDir, 'scripts', 'helper.mjs'), 'export const tag = "v1";\n', 'utf8');
  await fs.writeFile(path.join(projDir, 'style', 'app.css'), 'body{margin:0}\n', 'utf8');

  const cases = [];
  try {
    // Case 1: legacy_jxcore_manifest_contract
    const projectFiles = ['index.js', 'index.htm', 'scripts/helper.mjs', 'style/app.css'];
    const digests = {};
    for (const rel of projectFiles) {
      const buf = await fs.readFile(path.join(projDir, rel));
      digests[rel] = crypto.createHash('sha256').update(buf).digest('hex').slice(0, 12);
    }
    const jxpManifest = {
      project: 'index',
      startup: 'index.js',
      files: projectFiles,
      artifacts: ['index.jxp', 'index.jx'],
      archivedStatus: 'discontinued_historical_fork',
    };
    await fs.writeFile(path.join(projDir, 'index.jxp'), JSON.stringify(jxpManifest, null, 2), 'utf8');
    const loaded = JSON.parse(await fs.readFile(path.join(projDir, 'index.jxp'), 'utf8'));
    const actual1 = {
      startup: loaded.startup,
      fileCount: loaded.files.length,
      artifacts: loaded.artifacts,
      allDigestsHex12: Object.values(digests).every((h) => /^[0-9a-f]{12}$/.test(h)),
    };
    const expected1 = {
      startup: 'index.js',
      fileCount: 4,
      artifacts: ['index.jxp', 'index.jx'],
      allDigestsHex12: true,
    };
    cases.push({
      name: 'legacy_jxcore_manifest_contract',
      passed: JSON.stringify(actual1) === JSON.stringify(expected1),
      expected: expected1,
      actual: actual1,
    });

    // Case 2: cli_argument_forwarding_parity
    const rawOut = execFileSync(
      process.execPath,
      [path.join(projDir, 'index.js'), '--mode', 'prod', 'alpha'],
      { encoding: 'utf8' },
    ).trim();
    const parsedOut = JSON.parse(rawOut);
    const actual2 = { entry: parsedOut.entry, args: parsedOut.args };
    const expected2 = { entry: 'index.js', args: ['--mode', 'prod', 'alpha'] };
    cases.push({
      name: 'cli_argument_forwarding_parity',
      passed: JSON.stringify(actual2) === JSON.stringify(expected2),
      expected: expected2,
      actual: actual2,
    });

    // Case 3: node_v24_sea_config_contract
    const seaConfig = {
      main: 'dist/bundle.cjs',
      output: 'dist/sea-prep.blob',
      disableExperimentalSEAWarning: true,
      useSnapshot: false,
      useCodeCache: true,
      assets: { 'index.htm': 'index.htm' },
    };
    const seaConfigPath = path.join(projDir, 'sea-config.json');
    await fs.writeFile(seaConfigPath, JSON.stringify(seaConfig, null, 2), 'utf8');
    const loadedSea = JSON.parse(await fs.readFile(seaConfigPath, 'utf8'));
    const actual3 = {
      hasRequiredKeys: Boolean(loadedSea.main && loadedSea.output),
      useCodeCache: loadedSea.useCodeCache,
      assetKeys: Object.keys(loadedSea.assets),
      isSeaRuntime: sea.isSea(),
    };
    const expected3 = {
      hasRequiredKeys: true,
      useCodeCache: true,
      assetKeys: ['index.htm'],
      isSeaRuntime: false,
    };
    cases.push({
      name: 'node_v24_sea_config_contract',
      passed: JSON.stringify(actual3) === JSON.stringify(expected3),
      expected: expected3,
      actual: actual3,
    });

    // Case 4: offline_single_file_bundle_check
    const embeddedAsset = await fs.readFile(path.join(projDir, 'index.htm'), 'utf8');
    const bundleCode = [
      `const ASSETS = ${JSON.stringify({ 'index.htm': embeddedAsset.trim() })};`,
      'const label = process.argv[2] || "default";',
      'console.log(JSON.stringify({ label, html: ASSETS["index.htm"] }));',
    ].join('\n');
    const bundlePath = path.join(distDir, 'app-bundle.mjs');
    await fs.writeFile(bundlePath, bundleCode, 'utf8');
    const bundleOut = JSON.parse(
      execFileSync(process.execPath, [bundlePath, 'hello'], { cwd: distDir, encoding: 'utf8' }).trim(),
    );
    const actual4 = bundleOut;
    const expected4 = { label: 'hello', html: '<h1>Demo</h1>' };
    cases.push({
      name: 'offline_single_file_bundle_check',
      passed: JSON.stringify(actual4) === JSON.stringify(expected4),
      expected: expected4,
      actual: actual4,
    });
  } finally {
    await fs.rm(tmpDir, { recursive: true, force: true });
  }

  console.log(JSON.stringify({ version: process.version, cases }));
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
核对 .jxp 项目清单与 SHA-256 摘要、CLI 参数透传 ["--mode","prod","alpha"]、node:sea 模块 isSea()=false 与 sea-config.json 契约,以及单文件 bundle 独立目录运行结果。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a1041.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Node.js

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a1041.zip

下载课程关联工程包:Node.js

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1057.zip

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

`.jxp` 是包含项目文件清单与编译配置的中间描述文件,`.jx` 是打包完成的完整可执行分发包;由于 JXcore 已停止维护,现代 Node.js v24 推荐使用官方内置的 Single Executable Applications(SEA,配合 `sea-config.json` 与 `node:sea`)或 `esbuild` 等活跃打包方案。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习