@atlaspack/optimizer-inline-requires

中文文档
2.16.11发布于 2026/09/16(MIT OR Apache-2.0)TypeScriptCommonJS近 30 天下载 1746

This plugin is currently experimental / unstable, however that said it has been used in a very large web application with great success in reducing runtime startup time of the application.

原始使用文档
文档目录5 节
  1. Quick usage
  2. Background and motivation
  3. Caveats
  4. Overhead
  5. Side-effects

EXPERIMENTAL inline requires plugin

This plugin is currently experimental / unstable, however that said it has been used in a very large web application with great success in reducing runtime startup time of the application.

Quick usage

Add this optimizer to run first (before minification), for JS bundles.

{
  "optimizers": {
    "*.js": ["@atlaspack/optimizer-inline-requires", "..."]
  }
}

Background and motivation

When Atlaspack produces modules in bundles, where a dependency wasn't brought in by scope hoisting, it includes calls to require those dependencies at the top of the module function. For example, prior to minification, a module might look something like this:

atlaspackRegister('abc123', function (require, module, exports) {
  var $def456 = require('def456');
  var $ghi789 = require('ghi789');

  exports.someFunction = function () {
    if ($def456.someOperation()) {
      return $ghi789.anotherOperation();
    }
    return null;
  };
});

When the module is first initialised (i.e. the "module factory" is called), this will get the def456 and ghi789 modules, immediately calling their module factories, and so on down. However, the two modules don't have any of their code called until the someFunction export is called. If, for example, the call site looks like this:

// other code..
var $abc123 = require('abc123');
element.addEventListener('click', () => {
  element.innerText = $abc123.someFunction();
});

.. then the code in abc123 might not be called until much later, or never at all. In a large enough application, the evaluation / execution time of all these factory functions can be noticeable in performance metrics for the application.

What this plugin does, is it turns those require calls into "lazy" or "deferred" evaluation requires - that is, the factory function will only be executed when the module is first used. For the first example, the resulting code (pre-minification) will look like this:

atlaspackRegister('abc123', function (require, module, exports) {
  var $def456;
  var $ghi789;

  exports.someFunction = function () {
    if ((0, require('def456')).someOperation()) {
      return (0, require('ghi789')).anotherOperation();
    }
    return null;
  };
});

The minifier will remove the uninitialised variables, and it will also simplify the (0, ...) sequence expressions where possible.

It is important to note, that Atlaspack will only execute the factory function once. Subseqent calls to require will return the module as returned from the original factory. See the require code in packages/packagers/js/src/helpers.js for how this works.

Caveats

Overhead

So the main caveat here is that we're now turning a simple variable access into a function call everytime the module is referenced. For smaller applications, this overhead may not be worth the tradeoff, however for larger applications it might. That's why this plugin is optional, as you need to try it to determine whether or not it works for you.

Side-effects

A module may have side-effects when it is initialised (e.g. setting an event handler on the window for example). For assets that Atlaspack has identified as having side-effects (whether through package.json sideEffects field, or other heuristics), these modules will not have their require calls deferred.

版本与发布记录

@atlaspack/optimizer-inline-requires 共发布 1,061 个版本,首次发布于 2024/08/20;过去 12 个月发布了 253 个版本。

版本发布日期距今
2.16.11latest2026/09/161 个月前
2.14.5-canary.5002026/09/161 个月前
2.14.5-canary.4992026/09/151 个月前
2.14.5-canary.4932026/07/163 个月前
2.14.5-canary.4922026/07/153 个月前
2.16.102026/06/224 个月前

其他发布通道:alshdevreleasescript → 2.14.15-alshdevreleasescript.0,alshintegrationtestslib → 2.14.15-alshintegrationtestslib.0,unified → 2.14.18-unified-f165f7d09.0,noselfbuild → 2.14.18-noselfbuild-63bde801d.0,typescript → 2.14.22-typescript-17c3d1dec.0,dev-inline-requires-reuse → 2.14.26-dev-inline-requires-reuse-bdc9ca9c3.0。

关于 @atlaspack/optimizer-inline-requires 的常见问题

@atlaspack/optimizer-inline-requires 是什么?

This plugin is currently experimental / unstable, however that said it has been used in a very large web application with great success in reducing runtime startup time of the application.

如何安装 @atlaspack/optimizer-inline-requires?

在项目目录运行 npm install @atlaspack/optimizer-inline-requires,或使用 pnpm add / yarn add / bun add。

@atlaspack/optimizer-inline-requires 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@atlaspack/optimizer-inline-requires 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@atlaspack/optimizer-inline-requires 需要什么版本的 Node.js?

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

@atlaspack/optimizer-inline-requires 可以商用吗?

@atlaspack/optimizer-inline-requires 使用 (MIT OR Apache-2.0) 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@atlaspack/optimizer-inline-requires 的下载量有多少?

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

@atlaspack/optimizer-inline-requires 还在维护吗?

最新版本 2.16.11 发布于 2026/09/16(1 个月前);至今共发布 1061 个版本,其中过去 12 个月发布了 253 个;首次发布于 2024/08/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。