|
4 | 4 | - **Default:** `false` |
5 | 5 |
|
6 | 6 | Whether to import SWC helper functions from `@swc/helpers` instead of inlining them. |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +When `externalHelpers` set to `true`, the output JavaScript will import helper functions from the external module `@swc/helpers`. |
| 11 | + |
| 12 | +::: tip |
| 13 | + |
| 14 | +Make sure to declare the `@swc/helpers` in `dependencies` field of `package.json`. |
| 15 | + |
| 16 | +::: |
| 17 | + |
| 18 | +## Example |
| 19 | + |
| 20 | +Take the following code as an example: |
| 21 | + |
| 22 | +```ts title="index.ts" |
| 23 | +export default class FOO { |
| 24 | + get bar() { |
| 25 | + return; |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +When `externalHelpers` is disabled, the output JavaScript will inline helper functions. |
| 31 | + |
| 32 | +```ts title="rslib.config.ts" {6} |
| 33 | +export default { |
| 34 | + lib: [ |
| 35 | + // ... |
| 36 | + syntax: 'es5' |
| 37 | + ], |
| 38 | + externalHelpers: false, |
| 39 | +}; |
| 40 | +``` |
| 41 | + |
| 42 | +Below is the output JavaScript file: |
| 43 | + |
| 44 | +```js title="index.js" |
| 45 | +function _class_call_check(instance, Constructor) { |
| 46 | + if (!(instance instanceof Constructor)) |
| 47 | + throw new TypeError('Cannot call a class as a function'); |
| 48 | +} |
| 49 | +function _defineProperties(target, props) { |
| 50 | + for (var i = 0; i < props.length; i++) { |
| 51 | + var descriptor = props[i]; |
| 52 | + descriptor.enumerable = descriptor.enumerable || false; |
| 53 | + descriptor.configurable = true; |
| 54 | + if ('value' in descriptor) descriptor.writable = true; |
| 55 | + Object.defineProperty(target, descriptor.key, descriptor); |
| 56 | + } |
| 57 | +} |
| 58 | +function _create_class(Constructor, protoProps, staticProps) { |
| 59 | + if (protoProps) _defineProperties(Constructor.prototype, protoProps); |
| 60 | + if (staticProps) _defineProperties(Constructor, staticProps); |
| 61 | + return Constructor; |
| 62 | +} |
| 63 | +var src_FOO = /*#__PURE__*/ (function () { |
| 64 | + 'use strict'; |
| 65 | + function FOO() { |
| 66 | + _class_call_check(this, FOO); |
| 67 | + } |
| 68 | + _create_class(FOO, [ |
| 69 | + { |
| 70 | + key: 'bar', |
| 71 | + get: function () {}, |
| 72 | + }, |
| 73 | + ]); |
| 74 | + return FOO; |
| 75 | +})(); |
| 76 | +export { src_FOO as default }; |
| 77 | +``` |
| 78 | + |
| 79 | +When `externalHelpers` is enabled, the output JavaScript will import helper functions from the external module `@swc/helpers`. |
| 80 | + |
| 81 | +```ts title="rslib.config.ts" {6} |
| 82 | +export default { |
| 83 | + lib: [ |
| 84 | + // ... |
| 85 | + syntax: 'es5' |
| 86 | + ], |
| 87 | + externalHelpers: true, |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +Below is the output JavaScript file: |
| 92 | + |
| 93 | +```js title="index.js" |
| 94 | +import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__ from '@swc/helpers/_/_class_call_check'; |
| 95 | +import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__ from '@swc/helpers/_/_create_class'; |
| 96 | +var src_FOO = /*#__PURE__*/ (function () { |
| 97 | + 'use strict'; |
| 98 | + function FOO() { |
| 99 | + (0, __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__._)(this, FOO); |
| 100 | + } |
| 101 | + (0, __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__._)(FOO, [ |
| 102 | + { |
| 103 | + key: 'bar', |
| 104 | + get: function () {}, |
| 105 | + }, |
| 106 | + ]); |
| 107 | + return FOO; |
| 108 | +})(); |
| 109 | +export { src_FOO as default }; |
| 110 | +``` |
0 commit comments