File: README.md

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (91 lines) | stat: -rw-r--r-- 3,004 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# babel-plugin-transform-inline-imports-commonjs

**This plugin should be used instead of [`babel-plugin-transform-es2015-modules-commonjs`](http://babeljs.io/docs/plugins/transform-es2015-modules-commonjs/)**

[![Build Status](https://travis-ci.org/zertosh/babel-plugin-transform-inline-imports-commonjs.svg?branch=master)](https://travis-ci.org/zertosh/babel-plugin-transform-inline-imports-commonjs)

## Installation

```sh
$ npm install babel-plugin-transform-inline-imports-commonjs
```

## Details

This plugin transforms [ES modules](http://exploringjs.com/es6/ch_modules.html) ([`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)), into CommonJS [`require` and `module.exports`](https://nodejs.org/api/modules.html). `import`s are transformed into lazily loaded memoized `require`s. So the `require` call is deferred until the imported identifier is referenced. This allows you to write idiomatic code without the performance costs of loading code up-front (I/O, parsing, and executing).

### Transform example

**Before:**

```js
import bigModule from 'big-module';

export default function(val) {
  return bigModule.doExpensiveThing(val);
}
```

**After:**

```js
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

exports.default = function (val) {
  return (_bigModule || _bigModule2()).default.doExpensiveThing(val);
};

var _bigModule;

function _bigModule2() {
  return _bigModule = _interopRequireDefault(require('big-module'));
}

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

## Usage

### Configuration

The same settings that are available for [`babel-plugin-transform-es2015-modules-commonjs`](http://babeljs.io/docs/plugins/transform-es2015-modules-commonjs/) are available for `babel-plugin-transform-inline-imports-commonjs`:

```js
// without options
{
  "plugins": ["transform-inline-imports-commonjs"]
}

// with options
{
  "plugins": [
    ["transform-inline-imports-commonjs", {
      "allowTopLevelThis": true,
      "strict": false,
      "loose": true
    }]
  ]
}
```

#### Additional settings

* `excludeModules`:
  - An array of strings that correspond to module IDs that should not be "inline-import"'ed. For the config `"excludeModules": ["atom"]`:

  ```js
  import {TextEditor} from 'atom'; // transforms to plain `require` with interop
  import foo from 'bar'; // transforms to inline import
  ```

* `excludeNodeBuiltins` (default: `false`)
  - Do not apply "inline-imports" to [Node builtin modules](https://github.com/sindresorhus/builtin-modules/blob/v1.1.1/builtin-modules.json). These modules are usually already in the module cache, so there may be no need to lazily load them.

  ```js
  import * as path from 'path'; // transforms to plain `require` with interop
  import foo from 'bar'; // transforms to inline import
  ```