fix(static-module-record)!: remove dependencies on `@babel/standalone`
MODIFY
packages/static-module-record/DESIGN.md
packages/static-module-record/DESIGN.md
@@ -12,8 +12,7 @@ record's metadata, evaluate the functor, call the functor with linkage.
```js
import { makeModuleAnalyzer } from './src/transform-analyze.js';
-import * as babel from '@agoric/babel-standalone';
-const analyze = makeModuleAnalyzer(babel.default);
+const analyze = makeModuleAnalyzer();
const moduleAnalysis = analyze(moduleSource);
const moduleFunctor = evaluateModuleFunctor(moduleAnalysis.functorSource, /* ... */);
moduleFunctor({

MODIFY
packages/static-module-record/package.json
packages/static-module-record/package.json
"test": "ava"
},
"dependencies": {
- "@agoric/babel-standalone": "^7.14.3",
+ "@agoric/babel-generator": "^7.17.6",
+ "@babel/parser": "^7.17.3",
+ "@babel/traverse": "^7.17.3",
+ "@babel/types": "^7.17.0",
"ses": "^0.15.10"
},
"devDependencies": {

MODIFY
packages/static-module-record/src/static-module-record.js
packages/static-module-record/src/static-module-record.js
/* eslint no-underscore-dangle: ["off"] */
-// import babel from '@agoric/babel-standalone';
-import * as babelStar from '@agoric/babel-standalone';
import { makeModuleAnalyzer } from './transform-analyze.js';
const { freeze, keys } = Object;
@@ -30,7 +28,7 @@ const { freeze, keys } = Object;
// babel: linker error: no export named default
// babelStar: exports
-const analyzeModule = makeModuleAnalyzer(babelStar.default || babelStar);
+const analyzeModule = makeModuleAnalyzer();
/**
* StaticModuleRecord captures the effort of parsing and analyzing module text

MODIFY
packages/static-module-record/src/transform-analyze.js
packages/static-module-record/src/transform-analyze.js
+import * as babelParser from '@babel/parser';
+import babelGenerate from '@agoric/babel-generator';
+import babelTraverse from '@babel/traverse';
+import babelTypes from '@babel/types';
+
import * as h from './hidden.js';
import makeModulePlugins from './babelPlugin.js';
const { freeze } = Object;
-export const getParserPlugins = sourceType => {
- // Transform the script/expression source for import expressions.
- const parserPlugins = [];
- if (sourceType === 'module') {
- parserPlugins.push(
- 'exportDefaultFrom',
- 'exportNamespaceFrom',
- 'importMeta',
- );
- }
+const parseBabel = babelParser.default
+ ? babelParser.default.parse
+ : babelParser.parse || babelParser;
- // This list taken and amended from:
- // https://github.com/prettier/prettier/tree/master/src/language-js/parser-babylon.js#L21
- parserPlugins.push(
- 'eventualSend',
- 'doExpressions',
- 'objectRestSpread',
- 'classProperties',
- // 'exportDefaultFrom', // only for modules, above
- // 'exportNamespaceFrom', // only for modules, above
- 'asyncGenerators',
- 'functionBind',
- 'functionSent',
- 'dynamicImport', // needed for our rewrite
- 'numericSeparator',
- // 'importMeta', // only for modules, above
- 'optionalCatchBinding',
- 'optionalChaining',
- 'classPrivateProperties',
- ['pipelineOperator', { proposal: 'minimal' }],
- 'nullishCoalescingOperator',
- 'bigInt',
- 'throwExpressions',
- 'logicalAssignment',
- 'classPrivateMethods',
- 'classPrivateProperties',
- // 'v8intrinsic', // we really don't want people to rely on platform powers
- 'partialApplication',
- ['decorators', { decoratorsBeforeExport: false }],
- );
- return parserPlugins;
-};
+const visitorFromPlugin = plugin => plugin({ types: babelTypes }).visitor;
-const makeTransformSource = babel =>
- function transformSource(code, sourceOptions = {}) {
- const parserPlugins = getParserPlugins(sourceOptions.sourceType);
+const traverseBabel = babelTraverse.default || babelTraverse;
+const generateBabel = babelGenerate.default || babelGenerate;
+const makeTransformSource = (babel = null) => {
+ if (babel !== null) {
+ throw new Error(
+ `transform-analyze.js no longer allows injecting babel; use \`null\``,
+ );
+ }
+
+ const transformSource = (code, sourceOptions = {}) => {
// console.log(`transforming`, sourceOptions, code);
const { analyzePlugin, transformPlugin } = makeModulePlugins(sourceOptions);
- // TODO top-level-await https://github.com/endojs/endo/issues/306
- const allowAwaitOutsideFunction = false;
- babel.transform(code, {
- parserOpts: {
- allowAwaitOutsideFunction,
- plugins: parserPlugins,
- },
- plugins: [analyzePlugin],
- ast: false,
- code: false,
- });
- const { code: transformedCode } = babel.transform(code, {
- parserOpts: {
- allowAwaitOutsideFunction,
- plugins: parserPlugins,
- },
- generatorOpts: {
+
+ const ast = parseBabel(code, { sourceType: sourceOptions.sourceType });
+
+ traverseBabel(ast, visitorFromPlugin(analyzePlugin));
+ traverseBabel(ast, visitorFromPlugin(transformPlugin));
+
+ const { code: transformedCode } = generateBabel(ast, {
retainLines: true,
compact: true,
- },
- plugins: [transformPlugin],
+ verbatim: true,
});
-
- // console.log(`transformed to`, output.code);
+ // console.log(`transformed`, transformedCode);
return transformedCode;
};
+ return transformSource;
+};
+
const makeCreateStaticRecord = transformSource =>
function createStaticRecord(moduleSource, url) {
// Transform the Module source code.

fix(static-module-record)!: remove dependencies on `@babel/standalone`