Skip to content

Commit

Permalink
fix(spotlight): Fix compatibility issues with earlier Node versions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
BYK authored Nov 27, 2024
1 parent 8c183d6 commit 5fb478d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-jeans-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@spotlightjs/spotlight': patch
---

Fix compatibility issues with Node 20.11-
29 changes: 19 additions & 10 deletions packages/spotlight/bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ import { setupSidecar } from '@spotlightjs/sidecar';
import { inflateRawSync } from 'node:zlib';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import * as sea from 'node:sea';
import { fileURLToPath } from 'node:url';
import Module from 'node:module';
const require = Module.createRequire(import.meta.url);
let sea = null;
try {
// This is to maintain compatibility with Node 20.11- as
// the `node:sea` module is added in Node 20.12+
sea = require('node:sea');
} catch {
sea = { isSea: () => false };
}

const readAsset = sea.isSea()
? name => Buffer.from(sea.getRawAsset(name))
: (() => {
const ASSET_DIR = join(fileURLToPath(import.meta.url), '../../dist/overlay/');

return name => readFileSync(join(ASSET_DIR, name));
})();

const MAX_COLS = process.stdout.columns;
if (process.stdout.isTTY && MAX_COLS > 34) {
Expand Down Expand Up @@ -56,21 +73,13 @@ if (process.stdout.isTTY && MAX_COLS > 34) {
process.stdout.write(NL);
}

const readAsset = sea.isSea()
? name => Buffer.from(sea.getRawAsset(name))
: (() => {
const ASSET_DIR = join(fileURLToPath(import.meta.url), '../../dist/overlay/');

return name => readFileSync(join(ASSET_DIR, name));
})();

const port = process.argv.length >= 3 ? Number(process.argv[2]) : undefined;
const MANIFEST_NAME = 'manifest.json';
const ENTRY_POINT_NAME = 'src/index.html';
const basePath = process.cwd();
const filesToServe = Object.create(null);

// Following the guide here: https://vite.dev/guide/backend-integration.html
// Following the guide here: https://vite.dev/guide/backend-integration.html
const manifest = JSON.parse(readAsset(MANIFEST_NAME));
filesToServe[ENTRY_POINT_NAME] = readAsset(ENTRY_POINT_NAME);
const entries = Object.values(manifest);
Expand Down

0 comments on commit 5fb478d

Please sign in to comment.