All Projects → gajus → babel-plugin-transform-export-default-name

gajus / babel-plugin-transform-export-default-name

Licence: other
Babel plugin that transforms default exports to named exports.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to babel-plugin-transform-export-default-name

idomizer
An HTML template parser compiling an incremental-dom render factory.
Stars: ✭ 15 (-25%)
Mutual labels:  babel-plugin
babel-plugin-react-directives
A babel plugin that provides some directives for react(JSX), similar to directives of vue.
Stars: ✭ 80 (+300%)
Mutual labels:  babel-plugin
babel-plugin-transform-hoist-nested-functions
Babel plugin to move nested functions to outer scopes
Stars: ✭ 32 (+60%)
Mutual labels:  babel-plugin
babel-plugin-feature-flags
A babel transform for managing feature flags
Stars: ✭ 57 (+185%)
Mutual labels:  babel-plugin
babel-plugin-storybook-csf-title
A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.
Stars: ✭ 17 (-15%)
Mutual labels:  babel-plugin
babel-plugin-transform-react-qa-classes
Add component's name in `data-qa` attributes to React Components
Stars: ✭ 41 (+105%)
Mutual labels:  babel-plugin
loaders
ECMAScript Modules Loaders
Stars: ✭ 65 (+225%)
Mutual labels:  es-modules
babel-plugin-react-scope-binding
🍖 Babel plugin for React component to take event handler to bind context automatically.
Stars: ✭ 21 (+5%)
Mutual labels:  babel-plugin
babel-plugin-console-source
Add the file name and line numbers to all console logs.
Stars: ✭ 38 (+90%)
Mutual labels:  babel-plugin
sprockets-bumble d
Sprockets plugin to transpile modern javascript using Babel, useful while migrating to ES6 modules
Stars: ✭ 32 (+60%)
Mutual labels:  babel-plugin
babel-plugin-solid-labels
Simple, reactive labels for SolidJS
Stars: ✭ 127 (+535%)
Mutual labels:  babel-plugin
babel-plugin-transform-rename-properties
A Babel plugin for renaming JavaScript properties
Stars: ✭ 19 (-5%)
Mutual labels:  babel-plugin
babel-plugin-detective
Babel plugin that scans the AST for require calls and import statements
Stars: ✭ 26 (+30%)
Mutual labels:  babel-plugin
babel-plugin-transform-for-of-as-array
Transform all for-of loops into the equivalent array for loop
Stars: ✭ 14 (-30%)
Mutual labels:  babel-plugin
babel-plugin-inline-svg
Babel plugin to optimise and inline svg
Stars: ✭ 50 (+150%)
Mutual labels:  babel-plugin
penv.macro
A macro used with babel-plugin-macros to write configurations for multiple environments, and remove configurations are irrelevant with the specified environment from your codes finally.
Stars: ✭ 73 (+265%)
Mutual labels:  babel-plugin
babel-plugin-transform-amd-to-commonjs
✨ Babel plugin that transforms AMD to CommonJS
Stars: ✭ 44 (+120%)
Mutual labels:  babel-plugin
importly
import map generator
Stars: ✭ 42 (+110%)
Mutual labels:  es-modules
babel-plugin-flow-relay-query
Babel plugin which converts Flow types into Relay fragments
Stars: ✭ 38 (+90%)
Mutual labels:  babel-plugin
babel-plugin-object-styles-to-template
Babel plugin to transpile object styles to template literal
Stars: ✭ 33 (+65%)
Mutual labels:  babel-plugin

babel-plugin-transform-export-default-name

NPM version Travis build status js-canonical-style

Babel plugin that transforms export default of anonymous functions to named function export.

Plugin uses the name of the target file to create a temporary variable. Target resource (arrow function or an anonymous function) is assigned to the latter temporary variable. Temporary value is used in place of function in the export declaration.

Implementation

Values that are affected:

  • anonymous function
  • arrow function
  • anonymous class

Named function, named class and other object as well as literal values are not transformed.

Export Name

The name used for a temporary variable is derived from the name of the file (excluding .js extension). _.camelCase is used to sanitize file name (i.e. foo-bar.js becomes fooBar).

Problem

Executing a function without a name (arrow function or an anonymous function) appears as an (anonymous function) in the stack trace, e.g.

(() => {
    throw new Error('Hello, World!');
})();

Stack trace without function name

However, if an arrow function is defined on the right-hand-side of an assignment expression, the engine will take the name on the left-hand-side and use it to set the arrow function's .name, e.g.

let test;

test = () => {
    throw new Error('Hello, World!');
};

test();

Stack trace without function name

When you export an anonymous function using export default, this function will appear as an (anonymous function) the stack trace. babel-plugin-transform-export-default-name plugin transforms the code to assign function a name before it is exported.

./index.js

import foo from './foo';

foo();

./foo.js

import bar from './bar';

export default () => {
    bar();
};

./bar.js

import baz from './baz';

export default () => {
    baz();
};

./baz.js

export default () => {
    throw new Error('test');
};

Stack trace before and after export is given a name

Example

Input file is ./foo.js.

Input code:

export default () => {};

Output code:

let foo = () => {};

export default foo;

Usage

Add to .babelrc:

{
    "plugins": [
        "transform-export-default-name"
    ]
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].