Skip to content

Commit

Permalink
feat(module-source): Preserve format with Babel integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Sep 3, 2024
1 parent f82d5c1 commit a0aa5ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/module-source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"test": "ava"
},
"dependencies": {
"@agoric/babel-generator": "^7.17.6",
"@babel/generator": "^7.17.6",
"@babel/parser": "^7.23.6",
"@babel/traverse": "^7.23.6",
"@babel/types": "^7.24.0",
Expand Down
28 changes: 18 additions & 10 deletions packages/module-source/src/transformSource.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck XXX Babel types
import * as babelParser from '@babel/parser';
import babelGenerate from '@agoric/babel-generator';
import babelGenerate from '@babel/generator';
import babelTraverse from '@babel/traverse';
import * as babelTypes from '@babel/types';

Expand All @@ -26,22 +26,30 @@ export const makeTransformSource = (makeModulePlugins, babel = null) => {
const { sourceUrl, sourceMapUrl, sourceType, sourceMap, sourceMapHook } =
sourceOptions;

const ast = parseBabel(source, { sourceType });
const ast = parseBabel(source, {
sourceType,
tokens: true,
createParenthesizedExpression: true,
});

traverseBabel(ast, visitorFromPlugin(analyzePlugin));
traverseBabel(ast, visitorFromPlugin(transformPlugin));

const sourceMaps = sourceOptions.sourceMapHook !== undefined;

const { code: transformedSource, map: transformedSourceMap } =
generateBabel(ast, {
sourceFileName: sourceMapUrl,
sourceMaps,
inputSourceMap: sourceMap,
retainLines: true,
compact: true,
verbatim: true,
});
generateBabel(
ast,
{
sourceFileName: sourceMapUrl,
sourceMaps,
inputSourceMap: sourceMap,
retainLines: true,
preserveFormat: true,
verbatim: true,
},
source,
);

if (sourceMaps) {
sourceMapHook(transformedSourceMap, {
Expand Down

0 comments on commit a0aa5ec

Please sign in to comment.