All Projects → 59naga → Babel Plugin Add Module Exports

59naga / Babel Plugin Add Module Exports

【v0.2 no longer maintained】 Fix babel/babel#2212 - Follow the [email protected] behavior for [email protected]

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Babel Plugin Add Module Exports

Babel Plugin Import Graphql
Enables import syntax for .graphql and .gql files
Stars: ✭ 284 (-61.04%)
Mutual labels:  babel-plugin
Babel Plugin Tailwind Components
Use Tailwind with any CSS-in-JS library
Stars: ✭ 320 (-56.1%)
Mutual labels:  babel-plugin
Babel Plugin React Intl
Extracts string messages from React components that use React Intl.
Stars: ✭ 430 (-41.02%)
Mutual labels:  babel-plugin
Graphql Let
A layer to start/scale the use of GraphQL code generator.
Stars: ✭ 282 (-61.32%)
Mutual labels:  babel-plugin
Vuera
👀 Vue in React, React in Vue. Seamless integration of the two. 👯
Stars: ✭ 3,631 (+398.08%)
Mutual labels:  babel-plugin
Ts Nameof
nameof in TypeScript
Stars: ✭ 349 (-52.13%)
Mutual labels:  babel-plugin
Babel Plugin Console
Babel Plugin that adds useful build time console functions 🎮
Stars: ✭ 278 (-61.87%)
Mutual labels:  babel-plugin
Kneden
Transpile ES2017 async/await to vanilla ES6 Promise chains: a Babel plugin
Stars: ✭ 517 (-29.08%)
Mutual labels:  babel-plugin
Babel Plugin Css Modules Transform
Extract css class names from required css module files, so we can render it on server.
Stars: ✭ 318 (-56.38%)
Mutual labels:  babel-plugin
Faster.js
faster.js is a Babel plugin that compiles idiomatic Javascript to faster, micro-optimized Javascript.
Stars: ✭ 429 (-41.15%)
Mutual labels:  babel-plugin
Effectfuljs
JavaScript embedded effects compiler
Stars: ✭ 287 (-60.63%)
Mutual labels:  babel-plugin
Style9
CSS-in-JS compiler inspired by Facebook's stylex
Stars: ✭ 314 (-56.93%)
Mutual labels:  babel-plugin
Reflective Bind
Eliminate wasteful re-rendering in React components caused by inline functions
Stars: ✭ 366 (-49.79%)
Mutual labels:  babel-plugin
React Loadable
⏳ A higher order component for loading components with promises.
Stars: ✭ 16,238 (+2127.43%)
Mutual labels:  babel-plugin
Babel Plugin Sitrep
Log all assignments and the return value of a function with a simple comment
Stars: ✭ 442 (-39.37%)
Mutual labels:  babel-plugin
Babel Plugin Module Resolver
Custom module resolver plugin for Babel
Stars: ✭ 3,134 (+329.9%)
Mutual labels:  babel-plugin
Babel Plugin React Remove Properties
Babel plugin for removing React properties. 💨
Stars: ✭ 327 (-55.14%)
Mutual labels:  babel-plugin
React Svg Loader
A loader for webpack, rollup, babel that loads svg as a React Component
Stars: ✭ 570 (-21.81%)
Mutual labels:  babel-plugin
Glam
crazy good css in your js
Stars: ✭ 485 (-33.47%)
Mutual labels:  babel-plugin
I18nize React
Internationalize react apps within a lunch break
Stars: ✭ 389 (-46.64%)
Mutual labels:  babel-plugin

babel-plugin-add-module-exports

Why?

[email protected] doesn't export default module.exports any more - T2212 Kill CommonJS default export behavior.

[email protected] transforms the following file

export default 'foo'

into

'use strict';
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 'foo';

Therefore, it is a need to use the ugly .default in node.js.

require('./bundle.js') // { default: 'foo' }
require('./bundle.js').default // 'foo'

This plugin follows the [email protected] behavior - add the module.exports if only the export default declaration exists.

'use strict';
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 'foo';
module.exports = exports['default'];

Therefore, our old codes still work fine - the .default goes away. 😉

require('./bundle.js') // foo

Usage

Install this plugin from npm:

npm install babel-plugin-add-module-exports --save-dev
# or
yarn add -D babel-plugin-add-module-exports

Write the name to babelrc. It works with preset-env to output CommonJS code:

{
  "presets": ["@babel/env"],
  "plugins": ["add-module-exports"]
}

modules: false

However, the plugin doesn't change the pure-esmodule. this plugin makes changes only when exists exports.default (in other words, using commonjs).

{
  "presets": [["@babel/env", { "modules": false }]],
  "plugins": ["add-module-exports"]
}

into

export default 'foo'

1.0.0 Currently support is commonjs and umd. Doesn't support amd, systemjs modules(don't use. there are no plans to support at the moment).

with Webpack

Likewise, webpack doesn't perform commonjs transformation for codesplitting. Need to set commonjs conversion.

{
  "presets": [["@babel/env", { "modules": "commonjs" }]],
  "plugins": ["add-module-exports"]
}

Options

addDefaultProperty

If you're exporting an object and wish to maintain compatibility with code using the require('./bundle.js').default syntax, you can optionally enable the addDefaultProperty option as follows:

{
  "presets": ["env"],
  "plugins": [
    [
      "add-module-exports",
      {
        "addDefaultProperty": true
      }
    ]
  ]
}

This will cause a second line of code to be added which aliases the default name to the exported object like so:

module.exports = exports['default'];
module.exports.default = exports['default']

See also

License

MIT

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].