Skip to content

Commit

Permalink
Merge pull request #1 from mradbourne/allow-loading-of-cjs-modules
Browse files Browse the repository at this point in the history
Allow loading of .cjs modules
  • Loading branch information
pipobscure authored Jun 7, 2024
2 parents ceae79a + 98b1080 commit 2188299
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as VM from 'node:vm';
import * as OS from 'node:os';
import * as FS from 'node:fs';
import { Module } from 'node:module';
import assert from 'node:assert';

const VERSION = 1;
const COMPRESSION = {
Expand All @@ -25,7 +26,9 @@ const HASH = {
const PROTO = 'sea:';

const resolutions: Record<string, Record<string, string>> = JSON.parse(SEA.getAsset('resolv', 'utf8'));
const blobs = extractBlobs(SEA.getRawAsset('bundle'));
const bundle = SEA.getRawAsset('bundle');
assert(typeof bundle === 'object', 'bundle is not an ArrayBuffer');
const blobs = extractBlobs(bundle);

function extractBlobs(data: ArrayBuffer) {
const index: Record<string, ReturnType<typeof blobData>> = {};
Expand Down Expand Up @@ -351,7 +354,8 @@ function load(parent?: string, specifier?: string) {
switch (Path.extname(name).toLowerCase()) {
case '.json':
return (module.exports = JSON.parse(asset(id, 'utf-8')));
case '.js': {
case '.js':
case '.cjs': {
const dirname = new URL('./', url).toString();
const exec = new VM.Script(`(function module(module,exports,require,__dirname,__filename) {\n${asset(id, 'utf-8')}\n})`, { filename: id, lineOffset: -1 }).runInThisContext();
const main = modules[resolve().toString()];
Expand Down

0 comments on commit 2188299

Please sign in to comment.