Skip to content

Commit 6869a9f

Browse files
committed
remove babel dep
1 parent a97aa71 commit 6869a9f

File tree

6 files changed

+36
-243
lines changed

6 files changed

+36
-243
lines changed

package-lock.json

Lines changed: 1 addition & 98 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
},
4141
"dependencies": {
4242
"@actions/core": "^1.11.1",
43-
"@babel/generator": "^7.27.5",
44-
"@babel/types": "^7.27.7",
4543
"@clack/prompts": "^0.11.0",
4644
"@heroicons/react": "^2.2.0",
4745
"@minify-html/node": "^0.16.4",
@@ -56,7 +54,7 @@
5654
"dedent": "^1.6.0",
5755
"esbuild": "^0.25.5",
5856
"esbuild-style-plugin": "^1.6.3",
59-
"estree-to-babel": "^11.0.3",
57+
"estree-util-to-js": "^2.0.0",
6058
"estree-util-value-to-estree": "^3.4.0",
6159
"estree-util-visit": "^2.0.0",
6260
"github-slugger": "^2.0.0",

src/generators/web/build/__tests__/generate.test.mjs

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
1-
import { generate } from '@babel/generator';
2-
import * as t from '@babel/types';
3-
41
import { JSX_IMPORTS } from '../constants.mjs';
52

63
/**
7-
* Creates an import declaration AST node
4+
* Creates an import statement as a string
85
* @param {string|null} importName - The import identifier name, or null for side-effect imports
96
* @param {string} source - The module path to import from
107
* @param {boolean} [useDefault=true] - Whether to use a default import or a named import
8+
* @returns {string} The import statement
119
*/
1210
export const createImportDeclaration = (
1311
importName,
1412
source,
1513
useDefault = true
1614
) => {
17-
const specifiers = importName
18-
? [
19-
useDefault
20-
? t.importDefaultSpecifier(t.identifier(importName))
21-
: t.importSpecifier(
22-
t.identifier(importName),
23-
t.identifier(importName)
24-
),
25-
]
26-
: [];
27-
28-
return t.importDeclaration(specifiers, t.stringLiteral(source));
15+
if (!importName) {
16+
return `import "${source}";`;
17+
}
18+
if (useDefault) {
19+
return `import ${importName} from "${source}";`;
20+
}
21+
return `import { ${importName} } from "${source}";`;
2922
};
3023

3124
/**
32-
* Generates code from an AST
33-
* @param {import('@babel/types').Node} ast - The AST node to generate code from
34-
* @returns {string} The generated code
35-
*/
36-
const generateCode = ast => generate(ast).code;
37-
38-
/**
39-
* Creates an AST builder with configuration for React SSR/hydration using Preact
25+
* Creates a code builder with configuration for React SSR/hydration using Preact
4026
*/
4127
export default () => {
4228
// Create base imports from JSX_IMPORTS configuration
@@ -48,66 +34,35 @@ export default () => {
4834

4935
/**
5036
* Generates code that hydrates a server-rendered React component on the client
51-
* @param {import('@babel/types').Expression} component - The React component AST node to hydrate
37+
* @param {string} componentCode - The React component code as a string to hydrate
5238
* @returns {string} The generated client-side hydration code
5339
*/
54-
const buildClientProgram = component => {
55-
const program = t.program([
40+
const buildClientProgram = (componentCode) => {
41+
return [
5642
...baseImports,
57-
createImportDeclaration(
58-
null,
59-
// Relative to ESBUILD_RESOLVE_DIR
60-
'./index.css'
61-
),
43+
createImportDeclaration(null, './index.css'),
6244
createImportDeclaration('hydrate', 'preact', false),
63-
64-
// hydrate(component, document.getElementById("root"));
65-
t.expressionStatement(
66-
t.callExpression(t.identifier('hydrate'), [
67-
component,
68-
t.callExpression(
69-
t.memberExpression(
70-
t.identifier('document'),
71-
t.identifier('getElementById')
72-
),
73-
[t.stringLiteral('root')]
74-
),
75-
])
76-
),
77-
]);
78-
79-
return generateCode(program);
45+
'',
46+
`hydrate(${componentCode}, document.getElementById("root"));`,
47+
].join('\n');
8048
};
8149

8250
/**
8351
* Generates code that renders a React component to string on the server
84-
* @param {import('@babel/types').Expression} component - The React component AST node to render
52+
* @param {string} componentCode - The React component code as a string to render
8553
* @returns {string} The generated server-side rendering code
8654
*/
87-
const buildServerProgram = component => {
88-
const program = t.program([
55+
const buildServerProgram = (componentCode) => {
56+
return [
8957
...baseImports,
90-
createImportDeclaration(
91-
'renderToStringAsync',
92-
'preact-render-to-string',
93-
false
94-
),
95-
96-
// code = renderToStringAsync(component);
97-
t.expressionStatement(
98-
t.assignmentExpression(
99-
'=',
100-
t.identifier('code'),
101-
t.callExpression(t.identifier('renderToStringAsync'), [component])
102-
)
103-
),
104-
]);
105-
106-
return generateCode(program);
58+
createImportDeclaration('renderToStringAsync', 'preact-render-to-string', false),
59+
'',
60+
`const code = renderToStringAsync(${componentCode});`,
61+
].join('\n');
10762
};
10863

10964
return {
11065
buildClientProgram,
11166
buildServerProgram,
11267
};
113-
};
68+
};

src/generators/web/index.mjs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createRequire } from 'node:module';
33
import { join } from 'node:path';
44

55
import HTMLMinifier from '@minify-html/node';
6-
import { estreeToBabel } from 'estree-to-babel';
6+
import { jsx, toJs } from 'estree-util-to-js'
77
import Mustache from 'mustache';
88

99
import bundleCode from './build/bundle.mjs';
@@ -43,15 +43,15 @@ export async function processEntry(
4343
require,
4444
output
4545
) {
46-
// Convert JSX AST to Babel AST
47-
const { program } = estreeToBabel(entry);
46+
47+
const { value: code } = toJs(entry, { handlers: jsx });
4848

4949
// Generate and execute server-side code for SSR
50-
const serverCode = buildServerProgram(program);
50+
const serverCode = buildServerProgram(code);
5151
const dehydrated = await executeServerCode(serverCode, require);
5252

5353
// Generate and bundle client-side code
54-
const clientCode = buildClientProgram(program);
54+
const clientCode = buildClientProgram(code);
5555
const clientBundle = await bundleCode(clientCode);
5656

5757
// Render the final HTML using the template
@@ -63,8 +63,7 @@ export async function processEntry(
6363
dehydrated,
6464
javascript: clientBundle.js,
6565
})
66-
),
67-
{}
66+
)
6867
);
6968

7069
// Write HTML file if output directory is specified

0 commit comments

Comments
 (0)