This code provides a Babel plugin that modifies notebook code before transpilation, likely for a specific environment, by applying a series of transformations to the code.
npm run import -- "babel transpiler"
var babel = require('babel-core')
var toBabel = require('estree-to-babel');
var importer = require('../Core')
var {selectAst} = importer.import("select code tree")
var {selectAcorn} = importer.import("select acorn tree")
var {htmlToTree} = importer.import("html to tree")
var {addImports} = importer.import("add missing imports")
var {replaceCore} = importer.import("replace core requirement")
var {replaceImports} = importer.import("replace notebook import")
// TODO: convert these to transpiler tools like Add, Replace, Remove
var transpilePlugins = [
addImports,
replaceImports,
replaceCore,
]
function transpileNotebook() {
return {
manipulateOptions(opts, parserOpts) {
opts.parserOpts = {
parser(code, opts) {
code = transpilePlugins.reduce((code, plugin) => {
return plugin(code, opts.sourceFileName)
}, code)
var ast = toBabel(code)
return ast
},
};
},
};
}
function babelTranspile() {
return {
plugins: [
transpileNotebook
]
}
}
module.exports = babelTranspile
// Import required modules
const babel = require('babel-core');
const toBabel = require('estree-to-babel');
const { selectAst, selectAcorn } = require('../Core').import('code-tree-selection');
const { htmlToTree } = require('../Core').import('html-to-tree');
const { addImports, replaceImports, replaceCore } = require('../Core').import([
'add-missing-imports',
'replace-notebook-import',
'replace-core-requirement',
]);
// Define transpiler plugins
const transpilePlugins = [
addImports,
replaceImports,
replaceCore,
];
// Define the transpiler function
function transpileNotebook(opts, code) {
return transpilePlugins.reduce((code, plugin) => plugin(code, opts.sourceFileName), code);
}
// Define the babel transpiler configuration
function babelTranspile() {
return {
// Define the babel parser options
parser: {
// Transpile the code before parsing it
transpile(code, opts) {
return transpileNotebook(opts, code);
},
// Transform the code into an AST using estree-to-babel
transform(code) {
return toBabel(code);
},
},
};
}
// Export the babel transpiler configuration
module.exports = babelTranspile;
This code defines a Babel plugin for transpiling notebook code, likely for a specific environment or framework.
Here's a breakdown:
Imports:
babel-core
: The core Babel library for transpilation.estree-to-babel
: A utility for converting ESTree (Abstract Syntax Tree) to Babel's AST format.importer
: A module for importing other modules.importer
for selecting nodes in ASTs, converting HTML to ASTs, adding imports, and replacing core requirements.transpilePlugins
:
transpileNotebook
Function:
transpilePlugins
to the code before parsing it into an AST.babelTranspile
Function:
transpileNotebook
plugin.Module Exports:
babelTranspile
function, which can be used to configure Babel for transpiling notebook code.