-
-
Notifications
You must be signed in to change notification settings - Fork 58
feat: esm support #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: esm support #192
Changes from all commits
25aa92b
98be81c
2169840
00aca37
b2ed8ba
0a37535
908b75b
1df6258
43db4e5
bb242da
90bfda2
dcec27f
00fd845
2e58f69
f7c0159
33a27bf
971b572
5247ed5
ef3b2e9
d390f7c
759d80b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import * as babel from '@babel/core'; | ||
| import { log } from './log'; | ||
|
|
||
| export interface TransformResult { | ||
| code: string; | ||
| isTransformed: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Transform ESM code to CommonJS using Babel | ||
| * This allows ESM modules to be compiled to bytecode via vm.Script | ||
| * | ||
| * @param code - The ESM source code to transform | ||
| * @param filename - The filename for error reporting | ||
| * @returns Object with transformed code and success flag | ||
| */ | ||
| export function transformESMtoCJS( | ||
| code: string, | ||
| filename: string, | ||
| ): TransformResult { | ||
| try { | ||
| const result = babel.transformSync(code, { | ||
| filename, | ||
| plugins: [ | ||
| [ | ||
| '@babel/plugin-transform-modules-commonjs', | ||
| { | ||
| strictMode: true, | ||
| allowTopLevelThis: true, | ||
| }, | ||
| ], | ||
| ], | ||
| sourceMaps: false, | ||
| compact: false, | ||
| // Don't modify other syntax, only transform import/export | ||
| presets: [], | ||
| // Prevent Babel from loading user config files | ||
| babelrc: false, | ||
| configFile: false, | ||
| sourceType: 'module', | ||
| }); | ||
robertsLando marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+22
to
+41
|
||
|
|
||
| if (!result || !result.code) { | ||
| log.warn(`Babel transform returned no code for ${filename}`); | ||
| return { | ||
| code, | ||
| isTransformed: false, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| code: result.code, | ||
| isTransformed: true, | ||
| }; | ||
| } catch (error) { | ||
| log.warn( | ||
| `Failed to transform ESM to CJS for ${filename}: ${ | ||
| error instanceof Error ? error.message : String(error) | ||
| }`, | ||
| ); | ||
| return { | ||
| code, | ||
| isTransformed: false, | ||
| }; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.