# babel-plugin-transform-import-as-amd

> Simplified imports and exports

Latest version **0.0.12** (published 2021-06-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-transform-import-as-amd
pnpm add babel-plugin-transform-import-as-amd
yarn add babel-plugin-transform-import-as-amd
bun add babel-plugin-transform-import-as-amd
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.12 |
| Published | 2021-06-25 |
| First published | 2019-11-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 712.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | eprincev.egor |
| Keywords | babel-plugin, babel, plugin, amd, import, export, define, export_all |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-transform-import-as-amd
- Repository: https://github.com/eprincev-egor/babel-plugin-transform-es2015-modules-simple-amd
- Homepage: https://github.com/eprincev-egor/babel-plugin-transform-es2015-modules-simple-amd#readme
- Issues: https://github.com/eprincev-egor/babel-plugin-transform-es2015-modules-simple-amd/issues
- npm.io page: https://npm.io/package/babel-plugin-transform-import-as-amd

## Dependencies (3)

- [better-log](https://npm.io/package/better-log.md) ^1.3.1
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.6.3
- [@babel/template](https://npm.io/package/@babel/template.md) ^7.6.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.12 (latest) — 2021-06-25
- 0.0.11 — 2021-06-19
- 0.0.10 — 2021-02-08
- 0.0.9 — 2020-01-13
- 0.0.8 — 2020-01-13
- 0.0.7 — 2020-01-13
- 0.0.6 — 2019-11-07
- 0.0.5 — 2019-11-07
- 0.0.4 — 2019-11-06
- 0.0.3 — 2019-11-06
- 0.0.2 — 2019-11-06
- 0.0.1 — 2019-11-06

## README

# babel-plugin-transform-import-as-amd

Limited transformer for ECMAScript 2015 modules (AMD)

Converts this code:
```js
import x from '/path/to/x';
import y from '/path/to/y';
doSomething();
export default x + y;
```

Into this one:
```js
define(['/path/to/x', '/path/to/y'], function (x, y) {
  "use strict";
  var _exports = {};
  doSomething();
  _exports.default = x + y;
  return _exports.default;
});
```

Instead of this one (generated with ``babel-plugin-transform-es2015-modules-amd``):
```js
define(['exports', '/path/to/x', '/path/to/y'], function (exports, _x, _y) {
  Object.defineProperty(exports, "__esModule", {
    value: true
  });

  var _x2 = _interopRequireDefault(_x);

  var _y2 = _interopRequireDefault(_y);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      'default': obj
    };
  }

  doSomething();
  exports.default = _x2.default + _y2.default;
});
```

Supported features:
- ``import SPECIFIER from 'PATH'``
- ``import 'PATH'``
- ``import {SPECIFIER1, SPECIFIER2 as SPECIFIER3} from 'PATH'``
- ``export default NODE``
- ``export {item as name}``
- ``export * from 'PATH'``
- ``auto named modules: define("file", [], func...)``

Other features aren't supported.

**Warning**. If no ``import`` or ``export`` are presented in JavaScript file, the plugin does nothing (means it doesn't wrap code with ``define``).

## Installation

```sh
$ npm install --save-dev babel-plugin-transform-import-as-amd
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
  "plugins": ["transform-import-as-amd"]
}
```
## Usage auto names

### Via `.babelrc` (Recommended)

**.babelrc**

```js
{
  "plugins": [
    ["transform-import-as-amd", {
      // convert full file paths to module name
      moduleName: true,
      // module name is relative path at basePath
      basePath: __dirname,
      // paths to some modules, like are require-config.js
      paths: {
        module: "relative/path"
      }
    }]
  ]
}
```

**/some/module.js**
Converts this code:
```js
export default function MyModule() {}
```
Into this one (with moduleName option):
```js
define("some/module", [], function (x) {
  "use strict";
  var _exports = {};
  _exports.default = function MyModule() {};
  return _exports;
});
```
Into this one (withOUT moduleName option):
```js
define([], function (x) {
  "use strict";
  var _exports = {};
  _exports.default = function MyModule() {};
  return _exports;
});
```

## More examples
---------------
Converts this code:
```js
import * as x from 'PATH'
```

Into this one:
```js
define(['PATH'], function (x) {
  "use strict";
});
```
---------------

---------------
Converts this code:
```js
import {x, y} from 'PATH'
```

Into this one:
```js
define(['PATH'], function (_PATH) {
  "use strict";
  var x = _PATH.x;
  var y = _PATH.y;
});
```
---------------

---------------
Converts this code:
```js
import "css!style.css"
import js from "js"
```

Into this one:
```js
define(["css!style.css", "js"], function (_cssStyleCss, js) {
  "use strict";
});
```
---------------

---------------
Converts this code:
```js
export function test() {}
```

Into this one:
```js
define([], function () {
  "use strict";
  var _exports = {};
  function test() {}
  _exports.test = test;
  return _exports;
});
```
---------------

---------------
Converts this code:
```js
export default function test() {}
```

Into this one:
```js
define([], function () {
  "use strict";
  var _exports = {};
  _exports.default = function test() {};
  return _exports.default;
});
```
---------------

---------------
Converts this code:
```js
export * from "module"
```

Into this one:
```js
define(["module"], function (_module) {
  "use strict";
  var _exports = {};
  for (var _key in _module) {
    _exports[_key] = _module[key];
  }
  return _exports;
});
```
---------------

---------------
with enabled moduleName option,
Converts this code (some/module.js):
```js
define([], function() {});
```

Into this one:
```js
define("some/module", [], function() {});
```
---------------

---------------
with enabled moduleName option,
Converts this code (some/module.js):
```js
define("another-name", [], function() {});
```

Into this one (no modifications):
```js
define("another-name", [], function() {});
```
---------------
  
---------------  
with enabled paths option
```js
// options = {
//   basePath: __dirname,
//   paths: {
//     helpers: "lib/helpers"
//   }
// }
// filename = "some/file.js"

import {isString} from "../lib/helpers"
```

Into this one:
```js
define(["helpers"], function(_libHelpers) {
  "use strict";
  var isString = _libHelpers.isString;
});
```
---------------
[The same thing for CommonJS](https://github.com/finom/babel-plugin-transform-es2015-modules-simple-commonjs).

Thanks to [RReverser](https://github.com/RReverser/babel-plugin-hello-world).  
Thanks to [finom](https://github.com/finom/babel-plugin-transform-es2015-modules-simple-amd).  
Thanks to [dcleao](https://github.com/dcleao/babel-plugin-transform-es2015-modules-simple-amd/tree/8025a44c37e4163a526ad3d3741830ad26ed2708/test/fixtures).

---
_Source: https://npm.io/package/babel-plugin-transform-import-as-amd · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
