babel-plugin-amd-default-export

0.0.1 • Public • Published

babel-plugin-amd-default-export

Latest version Dependency status

A Babel plugin to make the AMD modules generated by @babel/plugin-transform-modules-amd export the default export directly and not wrapped in an object with a default property, which happen in Babel 6 and newer: { default: ... }. It will export just the exported value in module.exports, similarly to the return statement in an AMD module.

This plugin is supposed to be used especially with requirejs-babel7, which al,lows mixing AMD and ESM modules in one project and a consistent handling of default exports is necessary.

Table of Contents

Installation

This module can be installed in your project using NPM, PNPM or Yarn. Make sure, that you use Node.js version 6 or newer.

npm i -D babel-plugin-amd-default-export
pnpm i -D babel-plugin-amd-default-export
yarn add babel-plugin-amd-default-export

Babel Configuration Examples

Prevent the transpiler to wrap source files that are already wrapped by define or require as AMD modules:

{
  plugins: ['amd-default-export']
}

A typical configuration combined with @babel/plugin-transform-modules-amd:

{
  plugins: [
    'transform-modules-amd',
    'amd-default-export'
  ]
}

Another complete configuration example:

{
  plugins: [
    'amd-checker',
    'transform-modules-amd',
    [
      'module-resolver',
      {
        resolvePath: function (sourcePath, currentFile, opts) {
          // Avoid prefixing modules handled by other plugins.
          if (sourcePath.indexOf('!') < 0) {
            return 'es6!' + sourcePath;
          }
        }
      }
    ],
    [
      'amd-default-export',
      { addDefaultProperty: true }
    ]
  ]
}

The option addDefaultProperty enables setting the exported value to a default property in addition. It can improve the compatibility with consumers expecting the default export directly, or wrapped. It may interfere with some cases, where additional properties cannot be included in exported objects.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.

License

Copyright (c) 2022 Ferdinand Prantl

Licensed under the MIT license.

Package Sidebar

Install

npm i babel-plugin-amd-default-export

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

10.7 kB

Total Files

4

Last publish

Collaborators

  • prantlf