蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

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

JAVASCRIPTWindows x64、Node.js v24.22.3 本地运行环境。explore-jxcore-packaging.mjs
explore-jxcore-packaging.mjs141 行
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);
});
本例需在 Windows x64、Node.js v24.22.3 本地运行环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、Node.js v24.22.3 本地运行环境。

入口文件:projects/nodejs-jxcore-packaging-lab/explore-jxcore-packaging.mjs

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-jxcore-packaging.mjs,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、Node.js v24.22.3 本地运行环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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