babel-plugin-transform-export-function

0.1.0 • Public • Published

babel-plugin-transform-export-function

A Babel transform that wraps the module code inside a function, that it exports.

const {capitalize} = require('./helpers');
const {name} = require('./data.json');
 
// Return written explicitely, but you could use
// babel-plugin-transform-last-statement to
// return it automatically
return `<h1>${capitalize(name)}</h1>`
 
function doSomething() {
  // Do something, but I'm unused
}

Will become

const {capitalize} = require('./helpers');
const {name} = require('./data.json');
 
module.exports = function(data) {
  return `<h1>${capitalize(name)}</h1>`
}
 
function doSomething() {
  // Do something, but I'm unused
}

TODO

  • Basic implementation
  • Only activate if there's no top module.exports call

Future

  • Hoist imports via require, ignore dynamic require
  • Ignore functions & classes in top-level
  • Configure function arguments (default data)
  • Configure function name (default: anonymous)
  • Named vs default export (default: default export)
  • ES6 exports through option
  • Automatic detection of ES6 vs CJS (existing require => CJS, existing import or import() => ES6)

Readme

Keywords

none

Package Sidebar

Install

npm i babel-plugin-transform-export-function

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

11.7 kB

Total Files

28

Last publish

Collaborators

  • rhumaric