# amdextract

> Uses AST to extract AMD modules, their parts and an optimized output without unused dependencies while keeping the original format.

Latest version **3.0.0** (published 2019-12-16) · MIT license · 0 weekly downloads

## Install

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

## 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 | 3.0.0 |
| Published | 2019-12-16 |
| First published | 2014-01-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 27.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Mehdi Shojaei |
| Maintainers | mehdishojaei |
| Keywords | AMD, dependency, unused, useless, extract, detect, parse, optimize, analyze, source |

## Links

- npm: https://www.npmjs.com/package/amdextract
- Repository: https://github.com/mehdishojaei/amdextract
- Issues: https://github.com/mehdishojaei/amdextract/issues
- npm.io page: https://npm.io/package/amdextract

## Dependencies (2)

- [esprima](https://npm.io/package/esprima.md) ^4.0.1
- [estraverse](https://npm.io/package/estraverse.md) ^4.3.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2019-12-16
- 2.1.14 — 2015-12-27
- 2.1.13 — 2015-12-26
- 2.1.12 — 2015-12-26
- 2.1.11 — 2015-12-26
- 2.1.10 — 2015-07-25
- 2.1.9 — 2015-07-25
- 2.1.8 — 2015-04-15
- 2.1.6 — 2014-08-21
- 2.1.5 — 2014-08-18
- 2.1.4 — 2014-08-17
- 2.1.3 — 2014-08-17
- 2.1.2 — 2014-08-16
- 2.1.1 — 2014-08-16
- 2.0.3 — 2014-07-22
- … 16 more at https://npm.io/package/amdextract/versions

## README

# amdextract [![Build Status](https://travis-ci.org/mehdishojaei/amdextract.png)](https://travis-ci.org/mehdishojaei/amdextract) [![Known Vulnerabilities](https://snyk.io/test/github/mehdishojaei/amdextract/badge.svg)](https://snyk.io/test/github/mehdishojaei/amdextract)

Uses AST to extract AMD modules, their parts and an optimized output without unused dependencies while keeping the original format.

## example

source.js
```js
define('module1', ['p1', 'p2'], function (a, b) {
  /**
   * b is not used in this scope.
   */
  return (function(b) {
    return b;
  })(a);
});

define('module2', ['p1', 'p2', 'p3', 'p4'], function (a, b, c) {
	return b;
});
```

example.js
```js
var fs = require('fs');
var amdextract = require('amdextract');

var content = fs.readFileSync('source.js');
var result = amdextract.parse(content);

console.log(result.results.length + ' modules detected.');

result.results.forEach(function (r) {
	console.log('Unused paths: ' + r.unusedPaths.join(', '));
});

console.log('\nOptimized output:');
console.log(result.optimizedContent);
```

output
```
2 modules detected.
Unused paths: p2
Unused paths: p1, p3, p4

Optimized output:

define('module1', ['p1'], function (a) {
  /**
   * b is not used in this scope.
   */
  return (function(b) {
    return b;
  })(a);
});

define('module2', ['p2'], function (b) {
	return b;
});
```

# methods

## parse(content[, options])

### content
Type: string
JavaScript source for parsing.

### options

#### excepts
Type: Array
Default value: []

An array of strings or RegExps that represent dependency names that should not take into account.

#### exceptsPaths
Type: Array
Default value: []

An array of strings or RegExps that represent dependency paths that should not take into account.

NOTE: `exceptsPaths` can also be declared before each module definition as a comment of strings of module paths separated by commas. This only applies on the underlying module definition.

source.js
```js
/* exceptsPaths: p3 */
define(['p1', 'p2', 'p3'], function (a, b, c) {
	return b;
});
```

optimized-source.js
```js
/* exceptsPaths: p3 */
define(['p2', 'p3'], function (b, c) {
	return b;
});
```
#### removeUnusedDependencies
Type: Boolean
Default value: false

Removes unused dependencies from `content` and returns optimized content as `optimizedContent` property of result.

### returns

Returns an object with the following properties:

#### results
Type: Array

An array of hash objects witch have this properties for each AMD module detected in `content`:

- `moduleId`
- `paths`
- `dependencies`
- `unusedPaths`
- `unusedDependencies`

#### optimizedContent
Type: String

Optimized `content` (original content without unused dependencies).
This property is available when the value of option `removeUnusedDependencies` is true.

## Release History
 * 2015-12-27   v2.1.14   Remove "for in" loop in traverse.
 * 2015-12-26   v2.1.13   Remove unused function "extendRange".
 * 2015-12-26   v2.1.12   Fixed indentation of samples in README.md.
 * 2015-12-26   v2.1.11   Fixed a bug in "optimizedContent" property.
 * 2015-07-25   v2.1.10   Update travis CI.
 * 2015-07-25   v2.1.9   Update dependencies.
 * 2015-04-15   v2.1.8   Update README.
 * 2014-08-22   v2.1.6   Fix a bug when there are comments between paths and dependencies.
 * 2014-08-16   v2.1.0   Entirely uses AST.
 * 2014-07-21   v2.0.3   Fix an issue related to comment detection.
 * 2014-07-21   v2.0.2   Fix an issue related to RegExp literals.
 * 2014-07-20   v2.0.0   Uses AST to find out unused dependencies.
 * 2014-04-19   v1.1.0   Keeps the original separators of paths and dependencies.
 * 2014-04-09   v1.0.4   Fix a bug when dependencies length is grater than paths length.
 * 2014-03-29   v1.0.3   Fix a bug when specifying exceptsPaths in options only.
 * 2014-03-17   v1.0.0   First release.

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