@beyonk/async-script-loader

中文文档
2.3.2发布于 2026/10/22MITES Module零依赖构建来源已验证近 30 天下载 3600

Loads scripts cleanly and asynchronously in SPAs

原始使用文档
文档目录4 节
  1. Async Script Loader
  2. Reasoning
  3. Usage
  4. Script Tags




Async Script Loader

js-standard-style

Allows asynchronous loading of scripts and styles in a Single Page Application (or anything else, in fact):

  • Using a test to ensure that the code is only loaded once
  • Running a callback once the script is loaded
  • Running a callback if the script is already loaded
  • Not blocking the main thread

Reasoning

Having integrated a multitude of third-party SDKs from large, well known providers, I've come to the conclusion that not having a standard interface turns the whole thing into a minefield of callbacks, timers, random library-specific loader modules, and global objects on the window, resulting in XSS risks and all sort of other undesirable behaviour. This module aims to provide a standard way of loading third-party dependencies.

Usage

You pass a list of urls to the loader, along with a method for checking that your page is ready, and a callback to call when it is.

Urls can be scripts or stylesheets.

Script Tags

You can use the module like so, for a library loaded from example.com, which, when loaded, adds an attribute called PROVIDER to the global window object.

<script>
  import loader from '@beyonk/async-script-loader'

  const url = '//example.com/sdk/1.0.0/lib.js'

  function test () {
    return !!window.PROVIDER
  }

  function callback () {
    window.PROVIDER.someFunction()
  }

  loader([
    { type: 'script', url }
  ], test, callback)
</script>

You can pass options for script tags.

<script>
  loader([
    { type: 'script', url, options: { async: true, defer: true } } // these are the default options
  ], test, callback)
</script>

Style Tags

You can include any number of tags, including style tags.

When the last one has loaded, the callback will be called.

<script>
  import loader from '@beyonk/async-script-loader'

  loader([
    { type: 'script', url: '//example.com/sdk/1.0.0/lib.js' },
    { type: 'script', url: '//example.com/sdk/1.0.0/lib2.js' },
    { type: 'style', url: '//example.com/sdk/1.0.0/style.css' }
  ], () => {
    return !!window.PROVIDER
  }, () => {
    window.PROVIDER.someFunction()
  })
</script>

No more tears!

Inline scripts / Inline css

You can use inline content for either type of tag by passing the configuration attribute content instead of url. This will write the content passed into the tag's body rather than setting it as an href or src attribute url will always take prescidence, so leave it out for content to work.

<script>
  import loader from '@beyonk/async-script-loader'

  loader([
    { type: 'script', content: 'console.log("foo");' },
    { type: 'style', content: '* { color: red; }' }
  ],
  () => false, // always load
  () => () // no-op
)
</script>

版本与发布记录

@beyonk/async-script-loader 共发布 7 个版本,首次发布于 2019/05/24;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.3.2latest2026/10/2217 天前
2.3.02022/11/123 年前
2.2.22022/05/234 年前
2.2.12022/04/134 年前
2.0.02020/05/056 年前
1.0.32019/05/267 年前

关于 @beyonk/async-script-loader 的常见问题

@beyonk/async-script-loader 是什么?

Loads scripts cleanly and asynchronously in SPAs

如何安装 @beyonk/async-script-loader?

在项目目录运行 npm install @beyonk/async-script-loader,或使用 pnpm add / yarn add / bun add。

@beyonk/async-script-loader 支持 TypeScript 吗?

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

@beyonk/async-script-loader 支持 ESM 还是 CommonJS?

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

@beyonk/async-script-loader 需要什么版本的 Node.js?

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

@beyonk/async-script-loader 可以商用吗?

@beyonk/async-script-loader 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@beyonk/async-script-loader 的下载量有多少?

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

@beyonk/async-script-loader 还在维护吗?

最新版本 2.3.2 发布于 2026/10/22(17 天前);至今共发布 7 个版本,其中过去 12 个月发布了 1 个;首次发布于 2019/05/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。