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

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

index.js
1function _class_call_check(instance, Constructor) {
2  if (!(instance instanceof Constructor))
3    throw new TypeError('Cannot call a class as a function');
4}
5function _defineProperties(target, props) {
6  for (var i = 0; i < props.length; i++) {
7    var descriptor = props[i];
8    descriptor.enumerable = descriptor.enumerable || false;
9    descriptor.configurable = true;
10    if ('value' in descriptor) descriptor.writable = true;
11    Object.defineProperty(target, descriptor.key, descriptor);
12  }
13}
14function _create_class(Constructor, protoProps, staticProps) {
15  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
16  if (staticProps) _defineProperties(Constructor, staticProps);
17  return Constructor;
18}
19var src_FOO = /*#__PURE__*/ (function () {
20  'use strict';
21  function FOO() {
22    _class_call_check(this, FOO);
23  }
24  _create_class(FOO, [
25    {
26      key: 'bar',
27      get: function () {},
28    },
29  ]);
30  return FOO;
31})();
32export { 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, the highlighted code is importing helper functions:

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