Skip to content

Commit 1928d66

Browse files
committed
Use swc instead of Babel
1 parent e9115d4 commit 1928d66

File tree

8 files changed

+58
-149
lines changed

8 files changed

+58
-149
lines changed

lib/bundle/babel.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/bundle/config.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable object-curly-newline */
22
"use strict";
33

4-
let generateTranspiler = require("./babel");
54
let { generateSWCConfig } = require("./swc");
65
let { loadExtension, abort, repr } = require("faucet-pipeline-core/lib/util");
76
let commonjs = require("@rollup/plugin-commonjs");
@@ -53,7 +52,7 @@ function generateConfig({ externals, format, exports, // eslint-disable-next-lin
5352
let plugins = [];
5453
let extensions = [".js"];
5554

56-
if(typescript || jsx) {
55+
if(esnext || typescript || jsx) {
5756
// TODO: Improve error message (correct extension name)
5857
let { default: swc } = loadExtension("rollup-plugin-swc",
5958
"failed to activate SWC");
@@ -64,24 +63,16 @@ function generateConfig({ externals, format, exports, // eslint-disable-next-lin
6463
if(typescript) {
6564
extensions.push(".ts");
6665
}
67-
let swcConfig = generateSWCConfig({ typescript, jsx, sourcemaps });
66+
let swcConfig = generateSWCConfig({
67+
esnext,
68+
typescript,
69+
jsx,
70+
sourcemaps,
71+
browsers
72+
});
6873
plugins.push(swc(swcConfig));
6974
}
7075

71-
if(esnext) {
72-
let transpiler = Object.assign({}, esnext);
73-
transpiler.esnext = true;
74-
75-
let { browserslist } = transpiler;
76-
browsers = browserslist === false ? null : browsers[browserslist || "defaults"];
77-
if(browsers && browsers.length) {
78-
console.error("transpiling JavaScript for", browsers.join(", "));
79-
}
80-
81-
let { plugin } = generateTranspiler(transpiler, { browsers });
82-
plugins.push(plugin);
83-
}
84-
8576
plugins = plugins.concat([
8677
nodeResolve({ extensions }),
8778
commonjs({ include: "node_modules/**" })

lib/bundle/swc.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
exports.generateSWCConfig = function({ typescript, jsx, sourcemaps }) {
1+
exports.generateSWCConfig = function({ esnext, typescript, jsx, sourcemaps, browsers }) {
2+
let env;
3+
let rollup = {};
24
let jsc = {
35
parser: {
46
syntax: typescript ? "typescript" : "ecmascript"
57
},
6-
// Disable es3 / es5 / es2015 transforms
7-
target: "es2016"
8+
target: esnext ? "es5" : "es2016"
89
};
910

1011
if(jsx) {
@@ -30,7 +31,28 @@ exports.generateSWCConfig = function({ typescript, jsx, sourcemaps }) {
3031
jsc.sourcemaps = "inline";
3132
}
3233

34+
let exclude = esnext?.exclude ?? [];
35+
let browserslist = esnext?.browserslist;
36+
37+
let targets = browserslist === false ? null : browsers[browserslist || "defaults"];
38+
if(targets && targets.length) {
39+
console.error("transpiling JavaScript for", targets.join(", "));
40+
delete jsc.target;
41+
env = { targets };
42+
}
43+
44+
rollup.exclude = exclude.map(pkg => {
45+
// distinguish paths from package identifiers - as per Node's
46+
// resolution algorithm <https://nodejs.org/api/modules.html>, a
47+
// string is a path if it begins with `/`, `./` or `../`
48+
// FIXME: duplicates faucet-core's `resolvePath`, resulting in
49+
// inconsistency WRT working directory
50+
return /^\.{0,2}\//.test(pkg) ? pkg : `node_modules/${pkg}/**`;
51+
});
52+
3353
return {
54+
rollup,
55+
env,
3456
jsc
3557
};
3658
};

pkg/faucet-pipeline-esnext/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
"url": "https://github.com/faucet-pipeline/faucet-pipeline-js/issues"
1414
},
1515
"dependencies": {
16-
"@babel/core": "~7.17.5",
17-
"@babel/preset-env": "~7.16.11",
18-
"@rollup/plugin-babel": "~5.3.1",
19-
"faucet-pipeline-js": "2.1.8"
16+
"@swc/core": "^1.2.145",
17+
"rollup-plugin-swc": "^0.2.1"
2018
}
2119
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
class Util {}
2-
const FOO = "lorem ipsum";
1+
class Util {
2+
}const FOO = "lorem ipsum";
33
const BAR = "dolor sit amet";
44

55
console.log(`~~ ${Util} ~~ ${FOO} ~~ ${BAR} ~~`);

test/cli/test_browserslist/expected_legacy.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
function _classCallCheck(instance, Constructor) {
2-
if (!(instance instanceof Constructor)) {
3-
throw new TypeError("Cannot call a class as a function");
4-
}
2+
if (!(instance instanceof Constructor)) {
3+
throw new TypeError("Cannot call a class as a function");
4+
}
55
}
6-
7-
function _defineProperties(target, props) {
8-
for (var i = 0; i < props.length; i++) {
9-
var descriptor = props[i];
10-
descriptor.enumerable = descriptor.enumerable || false;
11-
descriptor.configurable = true;
12-
if ("value" in descriptor) descriptor.writable = true;
13-
Object.defineProperty(target, descriptor.key, descriptor);
14-
}
15-
}
16-
17-
function _createClass(Constructor, protoProps, staticProps) {
18-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
19-
if (staticProps) _defineProperties(Constructor, staticProps);
20-
Object.defineProperty(Constructor, "prototype", {
21-
writable: false
22-
});
23-
return Constructor;
24-
}
25-
26-
var Util = /*#__PURE__*/_createClass(function Util() {
27-
_classCallCheck(this, Util);
28-
});
6+
var Util = function Util() {
7+
_classCallCheck(this, Util);
8+
};
299
var FOO = "lorem ipsum";
3010
var BAR = "dolor sit amet";
3111

test/cli/test_jsx/expected.js

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
function _classCallCheck(instance, Constructor) {
2-
if (!(instance instanceof Constructor)) {
3-
throw new TypeError("Cannot call a class as a function");
4-
}
2+
if (!(instance instanceof Constructor)) {
3+
throw new TypeError("Cannot call a class as a function");
4+
}
55
}
6+
var MyComponent = function MyComponent() {
7+
_classCallCheck(this, MyComponent);
8+
};
69

7-
function _defineProperties(target, props) {
8-
for (var i = 0; i < props.length; i++) {
9-
var descriptor = props[i];
10-
descriptor.enumerable = descriptor.enumerable || false;
11-
descriptor.configurable = true;
12-
if ("value" in descriptor) descriptor.writable = true;
13-
Object.defineProperty(target, descriptor.key, descriptor);
14-
}
15-
}
16-
17-
function _createClass(Constructor, protoProps, staticProps) {
18-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
19-
if (staticProps) _defineProperties(Constructor, staticProps);
20-
Object.defineProperty(Constructor, "prototype", {
21-
writable: false
22-
});
23-
return Constructor;
24-
}
25-
26-
var MyComponent = /*#__PURE__*/_createClass(function MyComponent() {
27-
_classCallCheck(this, MyComponent);
28-
});
29-
30-
var el = /*#__PURE__*/createElement(MyComponent, {
31-
type: "dummy"
32-
}, /*#__PURE__*/createElement("my-element", null, "lorem ipsum", /*#__PURE__*/createElement(Fragment, null, /*#__PURE__*/createElement("mark", null, "666"), "dolor sit amet")));
10+
var el = /*#__PURE__*/ createElement(MyComponent, {
11+
type: "dummy"
12+
}, /*#__PURE__*/ createElement("my-element", null, "lorem ipsum", /*#__PURE__*/ createElement(Fragment, null, /*#__PURE__*/ createElement("mark", null, "666"), "dolor sit amet")));
3313
console.log(el);

test/cli/test_transpilation/expected.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
function _classCallCheck(instance, Constructor) {
2-
if (!(instance instanceof Constructor)) {
3-
throw new TypeError("Cannot call a class as a function");
4-
}
2+
if (!(instance instanceof Constructor)) {
3+
throw new TypeError("Cannot call a class as a function");
4+
}
55
}
6-
7-
function _defineProperties(target, props) {
8-
for (var i = 0; i < props.length; i++) {
9-
var descriptor = props[i];
10-
descriptor.enumerable = descriptor.enumerable || false;
11-
descriptor.configurable = true;
12-
if ("value" in descriptor) descriptor.writable = true;
13-
Object.defineProperty(target, descriptor.key, descriptor);
14-
}
15-
}
16-
17-
function _createClass(Constructor, protoProps, staticProps) {
18-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
19-
if (staticProps) _defineProperties(Constructor, staticProps);
20-
Object.defineProperty(Constructor, "prototype", {
21-
writable: false
22-
});
23-
return Constructor;
24-
}
25-
26-
var Util = /*#__PURE__*/_createClass(function Util() {
27-
_classCallCheck(this, Util);
28-
});
6+
var Util = function Util() {
7+
_classCallCheck(this, Util);
8+
};
299
var FOO = "lorem ipsum";
3010
var BAR = "dolor sit amet";
3111

0 commit comments

Comments
 (0)