close
logo
Rslib
Guide
Config
Blog
English
简体中文
Guide
Config
Blog
English
简体中文
logo
Rslib
Config Overview

Lib Configurations

lib.format
lib.bundle
lib.autoExtension
lib.autoExternal
lib.redirect
lib.syntax
lib.externalHelpers
lib.banner
lib.footer
lib.dts
lib.shims
lib.id
lib.umdName
lib.outBase

Rsbuild Configurations

logLevel
resolve
source
output
tools
plugins
📝 Edit this page on GitHub
Previous Pagelib.syntax
Next Pagelib.banner

#lib.externalHelpers

  • Type: boolean
  • Default: false

Whether to import SWC helper functions from @swc/helpers instead of inlining them.

By default, the output JavaScript file may depend on helper functions to support the target environment or output format, and these helper functions will be inlined in the file that requires it.

When externalHelpers set to true, the output JavaScript will import helper functions from the external module @swc/helpers. This helps reduce duplicate helper code in the final bundles, and reduces the bundle size.

NOTE

Make sure to declare and install @swc/helpers in dependencies field of package.json.

#Example

Take the following code as an example:

index.ts
export default class FOO {
  get bar() {
    return;
  }
}

When externalHelpers is disabled, the output JavaScript will inline helper functions.

rslib.config.ts
export default {
  lib: [
    {
      syntax: 'es5',
      externalHelpers: false,
    },
  ],
};

Below is the output JavaScript file, the highlighted code is the inlined helper functions:

index.js
function _class_call_check(instance, Constructor) {
  if (!(instance instanceof Constructor))
    throw new TypeError('Cannot call a class as a function');
}
function _defineProperties(target, props) {
  for (var i = 0; i < props.length; i++) {
    var descriptor = props[i];
    descriptor.enumerable = descriptor.enumerable || false;
    descriptor.configurable = true;
    if ('value' in descriptor) descriptor.writable = true;
    Object.defineProperty(target, descriptor.key, descriptor);
  }
}
function _create_class(Constructor, protoProps, staticProps) {
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  if (staticProps) _defineProperties(Constructor, staticProps);
  return Constructor;
}
var src_FOO = /*#__PURE__*/ (function () {
  'use strict';
  function FOO() {
    _class_call_check(this, FOO);
  }
  _create_class(FOO, [
    {
      key: 'bar',
      get: function () {},
    },
  ]);
  return FOO;
})();
export { src_FOO as default };

When externalHelpers is enabled, the output JavaScript will import helper functions from the external module @swc/helpers.

rslib.config.ts
export default {
  lib: [
    {
      syntax: 'es5',
      externalHelpers: true,
    },
  ],
};

Below is the output JavaScript file, the highlighted code is importing helper functions:

index.js
import { _ } from '@swc/helpers/_/_class_call_check';
import { _ as _create_class_ } from '@swc/helpers/_/_create_class';
var src_FOO = /*#__PURE__*/ (function () {
  'use strict';
  function FOO() {
    _(this, FOO);
  }
  _create_class_(FOO, [
    {
      key: 'bar',
      get: function () {},
    },
  ]);
  return FOO;
})();
export { src_FOO as default };