Skip to content

Commit c5fb25c

Browse files
authored
feat: support externalHelpers (#171)
1 parent 1556f05 commit c5fb25c

File tree

18 files changed

+435
-15
lines changed

18 files changed

+435
-15
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default class FOO {
2+
get bar() {
3+
return;
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./"
5+
},
6+
"include": ["src"]
7+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`should external @swc/helpers when externalHelpers is true 1`] = `
4+
"import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__ from \\"@swc/helpers/_/_class_call_check\\";
5+
import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__ from \\"@swc/helpers/_/_create_class\\";
6+
var _class_call_check_namespaceObject = __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__;
7+
var _create_class_namespaceObject = __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__;
8+
var src_FOO = /*#__PURE__*/ function() {
9+
\\"use strict\\";
10+
function FOO() {
11+
(0, _class_call_check_namespaceObject._)(this, FOO);
12+
}
13+
(0, _create_class_namespaceObject._)(FOO, [
14+
{
15+
key: \\"bar\\",
16+
get: function() {}
17+
}
18+
]);
19+
return FOO;
20+
}();
21+
export { src_FOO as default };
22+
"
23+
`;
24+
25+
exports[`should external @swc/helpers when externalHelpers is true 2`] = `
26+
"import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__ from \\"@swc/helpers/_/_class_call_check\\";
27+
import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__ from \\"@swc/helpers/_/_create_class\\";
28+
var _class_call_check_namespaceObject = __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__;
29+
var _create_class_namespaceObject = __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__;
30+
var src_FOO = /*#__PURE__*/ function() {
31+
\\"use strict\\";
32+
function FOO() {
33+
(0, _class_call_check_namespaceObject._)(this, FOO);
34+
}
35+
(0, _create_class_namespaceObject._)(FOO, [
36+
{
37+
key: \\"bar\\",
38+
get: function() {}
39+
}
40+
]);
41+
return FOO;
42+
}();
43+
export { src_FOO as default };
44+
"
45+
`;
46+
47+
exports[`should not external @swc/helpers by default 1`] = `
48+
"function _class_call_check(instance, Constructor) {
49+
if (!(instance instanceof Constructor)) throw new TypeError(\\"Cannot call a class as a function\\");
50+
}
51+
function _defineProperties(target, props) {
52+
for(var i = 0; i < props.length; i++){
53+
var descriptor = props[i];
54+
descriptor.enumerable = descriptor.enumerable || false;
55+
descriptor.configurable = true;
56+
if (\\"value\\" in descriptor) descriptor.writable = true;
57+
Object.defineProperty(target, descriptor.key, descriptor);
58+
}
59+
}
60+
function _create_class(Constructor, protoProps, staticProps) {
61+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
62+
if (staticProps) _defineProperties(Constructor, staticProps);
63+
return Constructor;
64+
}
65+
var src_FOO = /*#__PURE__*/ function() {
66+
\\"use strict\\";
67+
function FOO() {
68+
_class_call_check(this, FOO);
69+
}
70+
_create_class(FOO, [
71+
{
72+
key: \\"bar\\",
73+
get: function() {}
74+
}
75+
]);
76+
return FOO;
77+
}();
78+
export { src_FOO as default };
79+
"
80+
`;
81+
82+
exports[`should respect user override externalHelpers config 1`] = `
83+
"function _class_call_check(instance, Constructor) {
84+
if (!(instance instanceof Constructor)) throw new TypeError(\\"Cannot call a class as a function\\");
85+
}
86+
function _defineProperties(target, props) {
87+
for(var i = 0; i < props.length; i++){
88+
var descriptor = props[i];
89+
descriptor.enumerable = descriptor.enumerable || false;
90+
descriptor.configurable = true;
91+
if (\\"value\\" in descriptor) descriptor.writable = true;
92+
Object.defineProperty(target, descriptor.key, descriptor);
93+
}
94+
}
95+
function _create_class(Constructor, protoProps, staticProps) {
96+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
97+
if (staticProps) _defineProperties(Constructor, staticProps);
98+
return Constructor;
99+
}
100+
var src_FOO = /*#__PURE__*/ function() {
101+
\\"use strict\\";
102+
function FOO() {
103+
_class_call_check(this, FOO);
104+
}
105+
_create_class(FOO, [
106+
{
107+
key: \\"bar\\",
108+
get: function() {}
109+
}
110+
]);
111+
return FOO;
112+
}();
113+
export { src_FOO as default };
114+
"
115+
`;
116+
117+
exports[`should respect user override externalHelpers config 2`] = `
118+
"import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__ from \\"@swc/helpers/_/_class_call_check\\";
119+
import * as __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__ from \\"@swc/helpers/_/_create_class\\";
120+
var _class_call_check_namespaceObject = __WEBPACK_EXTERNAL_MODULE__swc_helpers_class_call_check__;
121+
var _create_class_namespaceObject = __WEBPACK_EXTERNAL_MODULE__swc_helpers_create_class__;
122+
var src_FOO = /*#__PURE__*/ function() {
123+
\\"use strict\\";
124+
function FOO() {
125+
(0, _class_call_check_namespaceObject._)(this, FOO);
126+
}
127+
(0, _create_class_namespaceObject._)(FOO, [
128+
{
129+
key: \\"bar\\",
130+
get: function() {}
131+
}
132+
]);
133+
return FOO;
134+
}();
135+
export { src_FOO as default };
136+
"
137+
`;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "external-helpers-config-override-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"dependencies": {
7+
"@swc/helpers": "0.5.13"
8+
}
9+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
syntax: 'es5',
8+
externalHelpers: true,
9+
tools: {
10+
swc: {
11+
jsc: {
12+
externalHelpers: false,
13+
},
14+
},
15+
},
16+
output: {
17+
distPath: {
18+
root: './dist/1',
19+
},
20+
},
21+
}),
22+
generateBundleEsmConfig({
23+
syntax: 'es5',
24+
tools: {
25+
swc: {
26+
jsc: {
27+
externalHelpers: true,
28+
},
29+
},
30+
},
31+
output: {
32+
distPath: {
33+
root: './dist/2',
34+
},
35+
},
36+
}),
37+
],
38+
source: {
39+
entry: {
40+
index: '../__fixtures__/src/index.ts',
41+
},
42+
},
43+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "external-helpers-default-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
syntax: 'es5',
8+
}),
9+
],
10+
source: {
11+
entry: {
12+
index: '../__fixtures__/src/index.ts',
13+
},
14+
},
15+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { join } from 'node:path';
2+
import { buildAndGetResults, proxyConsole } from '@e2e/helper';
3+
import stripAnsi from 'strip-ansi';
4+
import { expect, test } from 'vitest';
5+
6+
test('should not external @swc/helpers by default', async () => {
7+
const fixturePath = join(__dirname, 'default');
8+
const { entries } = await buildAndGetResults(fixturePath);
9+
10+
expect(entries.esm).toMatchSnapshot();
11+
});
12+
13+
test('should throw error when @swc/helpers is not be installed when externalHelpers is true', async () => {
14+
const { logs, restore } = proxyConsole();
15+
16+
const fixturePath = join(__dirname, 'no-deps');
17+
try {
18+
await buildAndGetResults(fixturePath);
19+
} catch {}
20+
21+
const logStrings = logs.map((log) => stripAnsi(log));
22+
23+
expect(logStrings).toMatchInlineSnapshot(`
24+
[
25+
"error externalHelpers is enabled, but the @swc/helpers dependency declaration was not found in package.json.",
26+
]
27+
`);
28+
29+
restore();
30+
});
31+
32+
test('should external @swc/helpers when externalHelpers is true', async () => {
33+
const fixturePath = join(__dirname, 'true');
34+
const { entries } = await buildAndGetResults(fixturePath);
35+
36+
// autoExternal is true
37+
expect(entries.esm0).toMatchSnapshot();
38+
// autoExternal is false
39+
expect(entries.esm1).toMatchSnapshot();
40+
});
41+
42+
test('should respect user override externalHelpers config', async () => {
43+
const fixturePath = join(__dirname, 'config-override');
44+
const { entries } = await buildAndGetResults(fixturePath);
45+
46+
// override externalHelpers false
47+
expect(entries.esm0).toMatchSnapshot();
48+
// override externalHelpers true
49+
expect(entries.esm1).toMatchSnapshot();
50+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "external-helpers-no-deps-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
syntax: 'es5',
8+
externalHelpers: true,
9+
}),
10+
],
11+
source: {
12+
entry: {
13+
index: '../__fixtures__/src/index.ts',
14+
},
15+
},
16+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "external-helpers-true-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"dependencies": {
7+
"@swc/helpers": "0.5.13"
8+
}
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
syntax: 'es5',
8+
externalHelpers: true,
9+
output: {
10+
distPath: {
11+
root: './dist/1',
12+
},
13+
},
14+
}),
15+
generateBundleEsmConfig({
16+
syntax: 'es5',
17+
externalHelpers: true,
18+
autoExternal: false,
19+
output: {
20+
distPath: {
21+
root: './dist/2',
22+
},
23+
},
24+
}),
25+
],
26+
source: {
27+
entry: {
28+
index: '../__fixtures__/src/index.ts',
29+
},
30+
},
31+
});

e2e/cases/syntax/__snapshots__/index.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export { Foo };
1212
`;
1313

1414
exports[`should downgrade class private method with output.syntax config 1`] = `
15-
"function _class_private_method_get(receiver, privateSet, fn) {
15+
"function _check_private_redeclaration(obj, privateCollection) {
16+
if (privateCollection.has(obj)) throw new TypeError(\\"Cannot initialize the same private elements twice on an object\\");
17+
}
18+
function _class_private_method_get(receiver, privateSet, fn) {
1619
if (!privateSet.has(receiver)) throw new TypeError(\\"attempted to get private field on non-instance\\");
1720
return fn;
1821
}
19-
function _check_private_redeclaration(obj, privateCollection) {
20-
if (privateCollection.has(obj)) throw new TypeError(\\"Cannot initialize the same private elements twice on an object\\");
21-
}
2222
function _class_private_method_init(obj, privateSet) {
2323
_check_private_redeclaration(obj, privateSet);
2424
privateSet.add(obj);

0 commit comments

Comments
 (0)