@loganfsmyth provided the missing link at BabelJS.slack - parserOpts.allowReturnOutsideFunction
. Here is the code I came up with:
const babel = require('babel-core'); let script = 'return ((selector, callback) => Array.prototype.map.call(document.querySelectorAll(selector), callback))("#main table a.companylist",a => a.innerText)'; let transform = babel.transform(script, { ast: false, code: true, comments: false, compact: true, minified: true, presets: [ ['env', { targets: { browsers: ['> 1%', 'last 2 Firefox versions', 'last 2 Chrome versions', 'last 2 Edge versions', 'last 2 Safari versions', 'Firefox ESR', 'IE >= 8'], } }] ], parserOpts: { allowReturnOutsideFunction: true, } }); let code = `return function(){${transform.code}}()`;
Output:
return function(){"use strict";return function(selector,callback){return Array.prototype.map.call(document.querySelectorAll(selector),callback)}("#main table a.companylist",function(a){return a.innerText});}()
My script entry looks a little funny just because of how I generated it, but you can see that it turned these arrow expressions into regular functions, and this is still an expression.
NB you can change this last line to something like
let code = `(function(){${transform.code}})()`;
depending on your needs. I needed my return
ed.
source share