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.

NOTE

Make sure to declare the @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
1export default {
2  lib: [
3    // ...
4    syntax: 'es5'
5  ],
6  externalHelpers: false,
7};

Below is the output JavaScript file:

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
1export default {
2  lib: [
3    // ...
4    syntax: 'es5'
5  ],
6  externalHelpers: true,
7};

Below is the output JavaScript file:

index.js
1import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__ from '@swc/helpers/_/_class_call_check';
2import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__ from '@swc/helpers/_/_create_class';
3var src_FOO = /*#__PURE__*/ (function () {
4  'use strict';
5  function FOO() {
6    (0, __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__._)(this, FOO);
7  }
8  (0, __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__._)(FOO, [
9    {
10      key: 'bar',
11      get: function () {},
12    },
13  ]);
14  return FOO;
15})();
16export { src_FOO as default };
ON THIS PAGE