@endo/import-bundle

中文文档
1.7.0发布于 2026/06/12Apache-2.0ES Module近 30 天下载 2.5万

load modules created by @endo/bundle-source

原始使用文档
文档目录8 节
  1. Module Formats
  2. endoZipBase64
  3. endoScript
  4. getExport
  5. nestedEvaluate
  6. test
  7. Options
  8. Source maps

import-bundle

importBundle is an async function that evaluates the bundles created by bundle-source, turning them back into callable functions:

const bundle = await bundleSource('path/to/bundle.js');
// 'bundle' is JSON-serializable
const options = {}; // filePrefix, endowments, other compartment options
const namespace = await importBundle(bundle);
const { default, namedExport1, namedExport2 } = namespace;

This must be run in a SES environment: you must install SES before importing @endo/import-bundle. The conventional way to do this is to import a module (e.g. @endo/init) which does import 'ses'; lockdown();.

The bundle will be loaded into a new Compartment, which does not have access to platform globals like document or Fetch or require. The bundle is isolated to only having access to powerless JavaScript facilities and whatever endowments you provide.

Each call to importBundle creates a new Compartment. The globals of the new Compartment are frozen before any bundle code is evaluated, to enforce ocap rules.

Module Formats

The source can be bundled in a variety of "formats".

endoZipBase64

By default, bundleSource uses a format named endoZipBase64, in which the source modules and a "compartment map" are captured in a Zip file and base-64 encoded. The compartment map describes how to construct a set of Hardened JavaScript compartments and how to load and link the source modules between them.

endoScript

The endoScript format captures the sources as a single JavaScript program that completes with the entry module's namespace object.

getExport

The getExport format captures the sources as a single CommonJS-style string, and wrapped in a callable function that provides the exports and module.exports context to which the exports can be attached.

nestedEvaluate

More sophisticated than getExport is named nestedEvaluate. In this mode, the source tree is converted into a table of evaluable strings, one for each original module. This table is then encoded and wrapped as before. The evaluation process uses a separate evaluator call for each module, providing an opportunity to attach a distinct sourceMap to each one. This preserves relative filenames in subsequent debugging information and stack traces.

To set a base prefix for these relative filenames, provide the filePrefix option.

Note that the nestedEvaluate format receives a global endowment named require, although it will only be called if the source tree imported one of the few modules on the bundle-source "external" list.

test

The test format is useful for mocking a bundle locally for a test and is deliberately not serializable or passable. Use this format in tests to avoid the need to generate a bundle from source, providing instead just the exports you need returned by importBundle.

import { importBundle, bundleTestExports } from '@endo/import-bundle';

test('who tests the tests', async t => {
  const bundle = bundleTestExports({ a: 10 });
  const namespace = await importBundle(bundle);
  t.is(namespace.a, 10);
  t.is(Object.prototype.toString.call(ns), '[object Module]');
});

Options

importBundle() takes an options bag and optional additional powers.

const namespace = await importBundle(bundle, options, powers);

The most common option is filePrefix, which can be provided for nestedEvaluate-format bundles. This sets the source filename of the top-level module inside the bundle, as used in debugging messages (like the stack traces displayed in errors). The other modules will append a suffix to this filename, based upon their location within the original source tree.

Another common option is endowments, which provides names that will be available everywhere in the evaluated sources. By default, the bundle will only get access to the standard JavaScript primordials (Array, Object, Map, etc). It will not get document, window, Request, process, require, or even console unless you provide them as endowments, giving you full control over what the loaded bundle can do.

The bundle-source tool has a small number of module names marked as "external". These modules are not bundled into the source (copied from the filesystem where bundleSource was called). Instead, the bundler injects a call to require() for each external module that was imported from somewhere in original source graph. This let the final evaluation environment control what these imports get, rather than the original source tree.

To support these "external" imports, you will need to provide a require endowment that can honor any such names. In addition, the nestedEvaluate format always needs a require endowment (although it will only be called if the original sources imported one of the "external" names).

For debugging purposes, you should probably provide a console endowment. See makeConsole.js in the SwingSet source tree for inspiration.

The rest of the options are passed through to the Compartment constructor, which currently only accepts transforms. For more information, see the compartment-shim docs in the SES repository. Note that transforms is defined to be an array of objects which each have a rewrite method.

Note that sloppyGlobalsMode is only accepted by the Compartment's evaluate method, not the Compartment constructor itself, and thus cannot be supplied to importBundle. To use sloppyGlobalsMode, you will probably want to create a Compartment directly (and not freeze its globals).

Source maps

For an Endo (zip, base64) bundle, bundleSource will add source maps to a per-user cache so they can be debugged if imported on the same host. To use this facility, pass a computeSourceMapLocation capability into powers.

import 'ses';
import bundleSource from '@endo/bundle-source';
import { importBundle } from '@endo/import-bundle';
import { computeSourceMapLocation } from '@endo/import-bundle/source-map-node.js';

lockdown();
const bundle = await bundleSource('debugme.js');
await importBundle(
  bundle,
  { endowments: { console } },
  { computeSourceMapLocation },
);

Use node --inspect-brk and debugger statements.

版本与发布记录

@endo/import-bundle 共发布 52 个版本,首次发布于 2022/01/23;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.7.0latest2026/06/125 个月前
1.6.12026/04/176 个月前
1.6.02026/02/278 个月前
1.5.22025/07/121.3 年前
1.5.12025/06/181.4 年前
1.5.02025/06/031.4 年前

关于 @endo/import-bundle 的常见问题

@endo/import-bundle 是什么?

load modules created by @endo/bundle-source

如何安装 @endo/import-bundle?

在项目目录运行 npm install @endo/import-bundle,或使用 pnpm add / yarn add / bun add。

@endo/import-bundle 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/endo__import-bundle 是否存在。

@endo/import-bundle 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@endo/import-bundle 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@endo/import-bundle 可以商用吗?

@endo/import-bundle 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@endo/import-bundle 的下载量有多少?

根据 npm 官方下载统计,@endo/import-bundle 在 2026/10/14 – 2026/11/12 这 30 天内被下载 24,570 次,日均约 819 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@endo/import-bundle 还在维护吗?

最新版本 1.7.0 发布于 2026/06/12(5 个月前);至今共发布 52 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/01/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。