
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
babel-plugin-func-wrap
Advanced tools
Wrap the whole script in a function — export as CommonJS, ES Modules, IIFE, or a global variable
babel-plugin-func-wrap is a Babel plugin that lets you wrap the whole script in a function, which can export as CommonJS, ES Modules, IIFE, or a global variable. This can be helpful when transforming scripts with immediately-executable code into something evocable.
window.a = 1;
/* becomes (with args: ['window']) */
export default function (window) {
window.a = 1;
}
/* becomes (with name: 'foo', args: ['window']) */
export function foo (window) {
window.a = 1;
}
/* becomes (with format: 'cjs', args: ['window']) */
module.exports = function (window) {
window.a = 1;
}
/* becomes (with format: 'cjs', name: 'foo', args: ['window']) */
exports.foo = function (window) {
window.a = 1;
}
Add babel-plugin-func-wrap to your project:
npm install babel-plugin-func-wrap --save-dev
Add babel-plugin-func-wrap to your Babel configuration:
// babel.config.js
module.exports = {
plugins: [
'func-wrap'
]
}
Alternative, configure transformations within your Babel configuration:
module.exports = {
plugins: [
['func-wrap', {
/* use a named export */
name: 'library',
/* assign arguments to the function */
args: ['window'],
/* export as CommonJS */
format: 'cjs'
}]
]
}
The args
option defines argument parameters passed into the wrapping function.
{
/* export default function (argA, argB, ...argC) {} */
args: ['argA', 'argB', '...argC']
}
The format
option defines how the function is exported. The available options
are esm
(default), cjs
, iife
, and global
.
{
/* export default function () {} */
format: 'esm'
}
{
/* module.exports = function () {} */
format: 'cjs'
}
{
/* (function () {})() */
format: 'iife'
}
{
/* window.$ = function () {} */
format: 'global',
name: 'window.$'
}
When using global
, a name
must always be specified.
The name
option defines the name of the export, which is otherwise default
.
{
/* export function foo () {} */
name: 'foo'
}
{
/* exports.foo = function () {} */
format: 'cjs',
name: 'foo'
}
1.1.0 (May 2, 2019)
FAQs
Wrap the whole script in a function — export as CommonJS, ES Modules, IIFE, or a global variable
We found that babel-plugin-func-wrap demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.