Skip to content

Commit 645b0e8

Browse files
authored
perf: replace Babel with esbuild for ESM to CJS transformation and update dependencies (#203)
* feat: replace Babel with esbuild for ESM to CJS transformation and update dependencies * chore: update dependencies in yarn.lock - Removed outdated versions of @babel packages and updated to the latest versions. - Consolidated multiple entries for @babel/code-frame, @babel/generator, @babel/parser, and @babel/types. - Cleaned up unused dependencies and ensured compatibility with the latest Babel ecosystem. * fix: resolve TypeScript type mismatches in detector and esm-transformer
1 parent 40e71c6 commit 645b0e8

File tree

4 files changed

+69
-317
lines changed

4 files changed

+69
-317
lines changed

lib/detector.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ function reconstructSpecifiers(
8080
}
8181

8282
function reconstruct(node: babelTypes.Node) {
83+
// @ts-expect-error Type mismatch due to @babel/types version in @types/babel__generator
8384
let v = generate(node, { comments: false }).code.replace(/\n/g, '');
8485
let v2;
8586

lib/esm-transformer.ts

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import * as babel from '@babel/core';
1+
import * as babel from '@babel/parser';
22
import traverse, { NodePath } from '@babel/traverse';
3+
import * as t from '@babel/types';
4+
import * as esbuild from 'esbuild';
35
import { log } from './log';
46
import { unlikelyJavascript } from './common';
57

@@ -29,8 +31,7 @@ function detectUnsupportedESMFeatures(
2931
filename: string,
3032
): UnsupportedFeature[] | null {
3133
try {
32-
const ast = babel.parseSync(code, {
33-
filename,
34+
const ast = babel.parse(code, {
3435
sourceType: 'module',
3536
plugins: [],
3637
});
@@ -41,9 +42,10 @@ function detectUnsupportedESMFeatures(
4142

4243
const unsupportedFeatures: UnsupportedFeature[] = [];
4344

44-
traverse(ast, {
45+
// @ts-expect-error Type mismatch due to @babel/types version in @types/babel__traverse
46+
traverse(ast as t.File, {
4547
// Detect import.meta usage
46-
MetaProperty(path) {
48+
MetaProperty(path: NodePath<t.MetaProperty>) {
4749
if (
4850
path.node.meta.name === 'import' &&
4951
path.node.property.name === 'meta'
@@ -57,7 +59,7 @@ function detectUnsupportedESMFeatures(
5759
},
5860

5961
// Detect top-level await
60-
AwaitExpression(path) {
62+
AwaitExpression(path: NodePath<t.AwaitExpression>) {
6163
// Check if await is at top level (not inside a function)
6264
let parent: NodePath | null = path.parentPath;
6365
let isTopLevel = true;
@@ -86,7 +88,7 @@ function detectUnsupportedESMFeatures(
8688
},
8789

8890
// Detect for-await-of at top level
89-
ForOfStatement(path) {
91+
ForOfStatement(path: NodePath<t.ForOfStatement>) {
9092
if (path.node.await) {
9193
let parent: NodePath | null = path.parentPath;
9294
let isTopLevel = true;
@@ -129,8 +131,9 @@ function detectUnsupportedESMFeatures(
129131
}
130132

131133
/**
132-
* Transform ESM code to CommonJS using Babel
134+
* Transform ESM code to CommonJS using esbuild
133135
* This allows ESM modules to be compiled to bytecode via vm.Script
136+
* Uses Babel parser for detecting unsupported ESM features, then esbuild for fast transformation
134137
*
135138
* @param code - The ESM source code to transform
136139
* @param filename - The filename for error reporting
@@ -183,29 +186,17 @@ export function transformESMtoCJS(
183186
}
184187

185188
try {
186-
const result = babel.transformSync(code, {
187-
filename,
188-
plugins: [
189-
[
190-
'@babel/plugin-transform-modules-commonjs',
191-
{
192-
strictMode: true,
193-
allowTopLevelThis: true,
194-
},
195-
],
196-
],
197-
sourceMaps: false,
198-
compact: false,
199-
// Don't modify other syntax, only transform import/export
200-
presets: [],
201-
// Prevent Babel from loading user config files
202-
babelrc: false,
203-
configFile: false,
204-
sourceType: 'module',
189+
const result = esbuild.transformSync(code, {
190+
loader: 'js',
191+
format: 'cjs',
192+
target: 'node18',
193+
sourcemap: false,
194+
minify: false,
195+
keepNames: true,
205196
});
206197

207198
if (!result || !result.code) {
208-
log.warn(`Babel transform returned no code for ${filename}`);
199+
log.warn(`esbuild transform returned no code for ${filename}`);
209200
return {
210201
code,
211202
isTransformed: false,

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
"singleQuote": true
2323
},
2424
"dependencies": {
25-
"@babel/core": "^7.23.0",
2625
"@babel/generator": "^7.23.0",
2726
"@babel/parser": "^7.23.0",
28-
"@babel/plugin-transform-modules-commonjs": "^7.27.1",
2927
"@babel/traverse": "^7.23.0",
3028
"@babel/types": "^7.23.0",
29+
"esbuild": "^0.24.0",
3130
"@yao-pkg/pkg-fetch": "3.5.32",
3231
"into-stream": "^6.0.0",
3332
"minimist": "^1.2.6",
@@ -44,8 +43,8 @@
4443
},
4544
"devDependencies": {
4645
"@release-it/conventional-changelog": "^7.0.2",
47-
"@types/babel__core": "^7.20.5",
4846
"@types/babel__generator": "^7.6.5",
47+
"@types/babel__traverse": "^7.20.3",
4948
"@types/minimist": "^1.2.2",
5049
"@types/multistream": "^4.1.0",
5150
"@types/node": "^16.18.113",
@@ -56,7 +55,6 @@
5655
"@types/unzipper": "^0.10.10",
5756
"@typescript-eslint/eslint-plugin": "^6.7.4",
5857
"@typescript-eslint/parser": "^6.7.4",
59-
"esbuild": "^0.24.0",
6058
"esbuild-register": "^3.6.0",
6159
"eslint": "^8.50.0",
6260
"eslint-config-airbnb-base": "^15.0.0",

0 commit comments

Comments
 (0)