# cjs2

> Recursive CommonJS loader plugin for RequireJS

Latest version **1.0.0** (published 2018-03-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install cjs2
pnpm add cjs2
yarn add cjs2
bun add cjs2
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-03-04 |
| First published | 2018-03-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Boris Seang |
| Maintainers | ghybs |
| Keywords | requirejs, amd, plugin |

## Links

- npm: https://www.npmjs.com/package/cjs2
- Repository: https://github.com/ghybs/cjs2
- Homepage: https://github.com/ghybs/cjs2#readme
- Issues: https://github.com/ghybs/cjs2/issues
- npm.io page: https://npm.io/package/cjs2

## Alternatives

- [@mdxeditor/editor](https://npm.io/package/@mdxeditor/editor.md) — 962.4K weekly downloads
- [mmdb-lib](https://npm.io/package/mmdb-lib.md) — 680.9K weekly downloads
- [playcanvas](https://npm.io/package/playcanvas.md) — 36.2K weekly downloads
- [@glw907/cairn-cms](https://npm.io/package/@glw907/cairn-cms.md) — 967 weekly downloads
- [markdown-to-confluence](https://npm.io/package/markdown-to-confluence.md) — 103 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-03-04

## README

# cjs2: Recursive CommonJS Loader Plugin for RequireJS

Load CommonJS modules without needing to convert them to AMD.
Any dependency will also be loaded through cjs2,
unless specified otherwise in the [require configuration](#use-global-require-for-sub-dependencies).

Adapted from the  [CommonJS Loader Plugin for RequireJS (cjs)](https://github.com/guybedford/cjs).

AMD and CommonJS are actually incredibly similar formats so this plugin is very short.

[![GitHub releases](https://img.shields.io/github/release/ghybs/cjs2.svg?label=GitHub)](https://github.com/ghybs/cjs2/releases)
[![npm](https://img.shields.io/npm/v/cjs2.svg)](https://www.npmjs.com/package/cjs2)


## Usage

To load a CommonJS module in RequireJS simply do:

```javascript
require(['cjs2!my-commonjs-module'], function(mymodule) {
  // Do something
});
```

### How it works

If your module looks like:

```javascript
var someDep = require('a-dependency');
exports.out = 'asdf';
```

…then it is dynamically converted into:

```javascript
define(function(require, exports, module) {
(function() {
  var define = undefined; // ensures any amd detection is disabled
  var someDep = require('cjs2!a-dependency');
  exports.out = 'asdf';
})();
});
```

…which is the AMD module format.

Dependencies are naturally handled by referring back to the plugin.

Supports:
* Cross-origin dynamic loading
* Builds
* Precompilation with the `optimizeAllPluginResources` r.js build option
* Amazingness

Built with the [AMD-Loader plugin helper](https://github.com/guybedford/amd-loader).


### Use global require for sub-dependencies

In some cases, we replace some sub-dependencies by modules that are not in CommonJS format.
In such situations, we can have cjs2 plugin use the global `require` instead of recursively using `cjs2` to load those modules.
Use the `require.config.cjs2config.globalRequire` array to list those modules:
```javascript
require.config({
  cjs2config: {
    globalRequire: [
        'path', // List of modules (sub-dependencies) that should be loaded through global require instead of cjs2
        'unfetch'
    ]
  }
});
```


## Install

```shell
volo add ghybs/cjs2
```

If not using package management, ensure that the [AMD-Loader](https://github.com/guybedford/amd-loader) plugin is installed.


## License

[![license](https://img.shields.io/github/license/ghybs/cjs2.svg)](LICENSE)

The cjs2 loader plugin is distributed under the [MIT License](http://choosealicense.com/licenses/mit/) (Expat type),
like the  [CommonJS Loader Plugin for RequireJS (cjs)](https://github.com/guybedford/cjs).

---
_Source: https://npm.io/package/cjs2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
